ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Category
/
DeleteCategoryCommandHandler.php
AddCategoryCommand.php
1 year ago
AddCategoryCommandHandler.php
6 months ago
DeleteCategoryCommand.php
1 year ago
DeleteCategoryCommandHandler.php
6 months ago
GetCategoriesCommand.php
1 year ago
GetCategoriesCommandHandler.php
6 months ago
GetCategoryCommand.php
1 year ago
GetCategoryCommandHandler.php
6 months ago
GetCategoryDeleteEffectCommand.php
6 months ago
GetCategoryDeleteEffectCommandHandler.php
3 months ago
UpdateCategoriesPositionsCommand.php
1 year ago
UpdateCategoriesPositionsCommandHandler.php
6 months ago
UpdateCategoryCommand.php
1 year ago
UpdateCategoryCommandHandler.php
6 months ago
DeleteCategoryCommandHandler.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Bookable\Category; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 11 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 12 | use AmeliaBooking\Domain\Entity\Entities; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 15 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 16 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 17 | |
| 18 | /** |
| 19 | * Class DeleteCategoryCommandHandler |
| 20 | * |
| 21 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 22 | */ |
| 23 | class DeleteCategoryCommandHandler extends CommandHandler |
| 24 | { |
| 25 | /** |
| 26 | * @param DeleteCategoryCommand $command |
| 27 | * |
| 28 | * @return CommandResult |
| 29 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 30 | * @throws QueryExecutionException |
| 31 | * @throws NotFoundException |
| 32 | * @throws AccessDeniedException |
| 33 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 34 | */ |
| 35 | public function handle(DeleteCategoryCommand $command) |
| 36 | { |
| 37 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::SERVICES)) { |
| 38 | throw new AccessDeniedException('You are not allowed to delete bookable category.'); |
| 39 | } |
| 40 | |
| 41 | $result = new CommandResult(); |
| 42 | |
| 43 | /** @var CategoryRepository $categoryRepository */ |
| 44 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 45 | |
| 46 | /** @var ServiceRepository $serviceRepository */ |
| 47 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 48 | |
| 49 | /** @var BookableApplicationService $bookableApplicationService */ |
| 50 | $bookableApplicationService = $this->getContainer()->get('application.bookable.service'); |
| 51 | |
| 52 | /** @var Category $category */ |
| 53 | $category = $categoryRepository->getById($command->getArg('id')); |
| 54 | |
| 55 | if (!$category instanceof Category) { |
| 56 | $result->setResult(CommandResult::RESULT_ERROR); |
| 57 | $result->setMessage('Could not delete bookable category.'); |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | /** @var Collection $services */ |
| 63 | $services = $serviceRepository->getByCriteria(['categories' => [$command->getArg('id')]]); |
| 64 | |
| 65 | $category->setServiceList($services); |
| 66 | |
| 67 | $categoryServiceIds = []; |
| 68 | |
| 69 | /** @var Service $service */ |
| 70 | foreach ($services->getItems() as $service) { |
| 71 | $categoryServiceIds[] = $service->getId()->getValue(); |
| 72 | } |
| 73 | |
| 74 | if ($categoryServiceIds) { |
| 75 | $appointmentsCount = $bookableApplicationService->getAppointmentsCountForServices($categoryServiceIds); |
| 76 | |
| 77 | if ($appointmentsCount['futureAppointments'] || $appointmentsCount['packageAppointments']) { |
| 78 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 79 | $result->setMessage('Could not delete category.'); |
| 80 | $result->setData([]); |
| 81 | |
| 82 | return $result; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $categoryRepository->beginTransaction(); |
| 87 | |
| 88 | do_action('amelia_before_category_deleted', $category->toArray()); |
| 89 | |
| 90 | if (!$bookableApplicationService->deleteCategory($category)) { |
| 91 | $categoryRepository->rollback(); |
| 92 | |
| 93 | $result->setResult(CommandResult::RESULT_ERROR); |
| 94 | $result->setMessage('Could not delete category.'); |
| 95 | |
| 96 | return $result; |
| 97 | } |
| 98 | |
| 99 | $categoryRepository->commit(); |
| 100 | |
| 101 | do_action('amelia_after_category_deleted', $category->toArray()); |
| 102 | |
| 103 | |
| 104 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 105 | $result->setMessage('Successfully deleted bookable category.'); |
| 106 | $result->setData([]); |
| 107 | |
| 108 | return $result; |
| 109 | } |
| 110 | } |
| 111 |