ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Category
/
GetCategoryDeleteEffectCommandHandler.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
GetCategoryDeleteEffectCommandHandler.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Bookable\Category; |
| 4 | |
| 5 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 6 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 7 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 8 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 9 | use AmeliaBooking\Domain\Entity\Entities; |
| 10 | use AmeliaBooking\Application\Commands\CommandResult; |
| 11 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 15 | use AmeliaBooking\Domain\Collection\Collection; |
| 16 | use Slim\Exception\ContainerValueNotFoundException; |
| 17 | |
| 18 | /** |
| 19 | * Class GetCategoryDeleteEffectCommandHandler |
| 20 | * |
| 21 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 22 | */ |
| 23 | class GetCategoryDeleteEffectCommandHandler extends CommandHandler |
| 24 | { |
| 25 | /** |
| 26 | * @param GetCategoryDeleteEffectCommand $command |
| 27 | * |
| 28 | * @return CommandResult |
| 29 | * @throws ContainerValueNotFoundException |
| 30 | * @throws AccessDeniedException |
| 31 | * @throws QueryExecutionException |
| 32 | * @throws InvalidArgumentException |
| 33 | */ |
| 34 | public function handle(GetCategoryDeleteEffectCommand $command) |
| 35 | { |
| 36 | if (!$command->getPermissionService()->currentUserCanRead(Entities::SERVICES)) { |
| 37 | throw new AccessDeniedException('You are not allowed to read categories'); |
| 38 | } |
| 39 | |
| 40 | $result = new CommandResult(); |
| 41 | |
| 42 | /** @var BookableApplicationService $bookableAS */ |
| 43 | $bookableAS = $this->getContainer()->get('application.bookable.service'); |
| 44 | |
| 45 | /** @var ServiceRepository $serviceRepository */ |
| 46 | $serviceRepository = $this->getContainer()->get('domain.bookable.service.repository'); |
| 47 | |
| 48 | /** @var PackageRepository $packageRepository */ |
| 49 | $packageRepository = $this->container->get('domain.bookable.package.repository'); |
| 50 | |
| 51 | /** @var Collection $services */ |
| 52 | $services = $serviceRepository->getByCriteria(['categories' => [$command->getArg('id')]]); |
| 53 | |
| 54 | $categoryServiceIds = []; |
| 55 | |
| 56 | /** @var Service $service */ |
| 57 | foreach ($services->getItems() as $service) { |
| 58 | $categoryServiceIds[] = $service->getId()->getValue(); |
| 59 | } |
| 60 | |
| 61 | $messageKey = ''; |
| 62 | $messageData = null; |
| 63 | |
| 64 | if ($categoryServiceIds) { |
| 65 | $appointmentsCount = $bookableAS->getAppointmentsCountForServices($categoryServiceIds); |
| 66 | |
| 67 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 68 | $messageKey = 'red_delete_category_effect_future'; |
| 69 | $messageData = ['count' => $appointmentsCount['futureAppointments']]; |
| 70 | } elseif ($appointmentsCount['packageAppointments']) { |
| 71 | $messageKey = 'red_delete_category_effect_package'; |
| 72 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 73 | $messageKey = 'red_delete_category_effect_past'; |
| 74 | $messageData = ['count' => $appointmentsCount['pastAppointments']]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** @var Collection $packages */ |
| 79 | $packages = $services->length() ? $packageRepository->getByCriteria(['services' => $services->keys()]) : new Collection(); |
| 80 | |
| 81 | if ($packages->length()) { |
| 82 | $messageKey = 'red_category_failed_to_be_deleted'; |
| 83 | } |
| 84 | |
| 85 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 86 | $result->setMessage('Successfully retrieved message.'); |
| 87 | $result->setData( |
| 88 | [ |
| 89 | 'valid' => !($categoryServiceIds && ($appointmentsCount['futureAppointments'] || $appointmentsCount['packageAppointments'])), |
| 90 | 'messageKey' => $messageKey, |
| 91 | 'messageData' => $messageData, |
| 92 | ] |
| 93 | ); |
| 94 | |
| 95 | return $result; |
| 96 | } |
| 97 | } |
| 98 |