ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Category
/
DeleteCategoryCommandHandler.php
AddCategoryCommand.php
7 years ago
AddCategoryCommandHandler.php
2 years ago
DeleteCategoryCommand.php
7 years ago
DeleteCategoryCommandHandler.php
2 years ago
GetCategoriesCommand.php
7 years ago
GetCategoriesCommandHandler.php
2 years ago
GetCategoryCommand.php
7 years ago
GetCategoryCommandHandler.php
2 years ago
UpdateCategoriesPositionsCommand.php
7 years ago
UpdateCategoriesPositionsCommandHandler.php
2 years ago
UpdateCategoryCommand.php
7 years ago
UpdateCategoryCommandHandler.php
2 years ago
DeleteCategoryCommandHandler.php
112 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 \Interop\Container\Exception\ContainerException |
| 34 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 35 | */ |
| 36 | public function handle(DeleteCategoryCommand $command) |
| 37 | { |
| 38 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::SERVICES)) { |
| 39 | throw new AccessDeniedException('You are not allowed to delete bookable category.'); |
| 40 | } |
| 41 | |
| 42 | $result = new CommandResult(); |
| 43 | |
| 44 | /** @var CategoryRepository $categoryRepository */ |
| 45 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 46 | |
| 47 | /** @var ServiceRepository $serviceRepository */ |
| 48 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 49 | |
| 50 | /** @var BookableApplicationService $bookableApplicationService */ |
| 51 | $bookableApplicationService = $this->getContainer()->get('application.bookable.service'); |
| 52 | |
| 53 | /** @var Category $category */ |
| 54 | $category = $categoryRepository->getById($command->getArg('id')); |
| 55 | |
| 56 | if (!$category instanceof Category) { |
| 57 | $result->setResult(CommandResult::RESULT_ERROR); |
| 58 | $result->setMessage('Could not delete bookable category.'); |
| 59 | |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | /** @var Collection $services */ |
| 64 | $services = $serviceRepository->getByCriteria(['categories' => [$command->getArg('id')]]); |
| 65 | |
| 66 | $category->setServiceList($services); |
| 67 | |
| 68 | $categoryServiceIds = []; |
| 69 | |
| 70 | /** @var Service $service */ |
| 71 | foreach ($services->getItems() as $service) { |
| 72 | $categoryServiceIds[] = $service->getId()->getValue(); |
| 73 | } |
| 74 | |
| 75 | if ($categoryServiceIds) { |
| 76 | $appointmentsCount = $bookableApplicationService->getAppointmentsCountForServices($categoryServiceIds); |
| 77 | |
| 78 | if ($appointmentsCount['futureAppointments'] || $appointmentsCount['packageAppointments']) { |
| 79 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 80 | $result->setMessage('Could not delete category.'); |
| 81 | $result->setData([]); |
| 82 | |
| 83 | return $result; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $categoryRepository->beginTransaction(); |
| 88 | |
| 89 | do_action('amelia_before_category_deleted', $category->toArray()); |
| 90 | |
| 91 | if (!$bookableApplicationService->deleteCategory($category)) { |
| 92 | $categoryRepository->rollback(); |
| 93 | |
| 94 | $result->setResult(CommandResult::RESULT_ERROR); |
| 95 | $result->setMessage('Could not delete category.'); |
| 96 | |
| 97 | return $result; |
| 98 | } |
| 99 | |
| 100 | $categoryRepository->commit(); |
| 101 | |
| 102 | do_action('amelia_after_category_deleted', $category->toArray()); |
| 103 | |
| 104 | |
| 105 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 106 | $result->setMessage('Successfully deleted bookable category.'); |
| 107 | $result->setData([]); |
| 108 | |
| 109 | return $result; |
| 110 | } |
| 111 | } |
| 112 |