ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Service
/
GetServiceDeleteEffectCommandHandler.php
AddServiceCommand.php
7 months ago
AddServiceCommandHandler.php
7 months ago
DeleteServiceCommand.php
7 months ago
DeleteServiceCommandHandler.php
4 weeks ago
GetServiceCommand.php
7 months ago
GetServiceCommandHandler.php
7 months ago
GetServiceDeleteEffectCommand.php
1 year ago
GetServiceDeleteEffectCommandHandler.php
4 weeks ago
GetServicesCommand.php
7 months ago
GetServicesCommandHandler.php
1 month ago
UpdateServiceCommand.php
7 months ago
UpdateServiceCommandHandler.php
7 months ago
UpdateServiceStatusCommand.php
7 months ago
UpdateServiceStatusCommandHandler.php
4 weeks ago
UpdateServicesPositionsCommand.php
1 year ago
UpdateServicesPositionsCommandHandler.php
1 year ago
GetServiceDeleteEffectCommandHandler.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 6 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 7 | use AmeliaBooking\Domain\Collection\Collection; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Entities; |
| 10 | use AmeliaBooking\Application\Commands\CommandResult; |
| 11 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 12 | use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository; |
| 15 | use Slim\Exception\ContainerValueNotFoundException; |
| 16 | |
| 17 | /** |
| 18 | * Class GetServiceDeleteEffectCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 21 | */ |
| 22 | class GetServiceDeleteEffectCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @param GetServiceDeleteEffectCommand $command |
| 26 | * |
| 27 | * @return CommandResult |
| 28 | * @throws ContainerValueNotFoundException |
| 29 | * @throws AccessDeniedException |
| 30 | * @throws QueryExecutionException |
| 31 | * @throws InvalidArgumentException |
| 32 | */ |
| 33 | public function handle(GetServiceDeleteEffectCommand $command) |
| 34 | { |
| 35 | if (!$command->getPermissionService()->currentUserCanRead(Entities::SERVICES)) { |
| 36 | throw new AccessDeniedException('You are not allowed to read services'); |
| 37 | } |
| 38 | |
| 39 | $result = new CommandResult(); |
| 40 | |
| 41 | /** @var AbstractPackageApplicationService $packageApplicationService */ |
| 42 | $packageApplicationService = $this->container->get('application.bookable.package'); |
| 43 | |
| 44 | /** @var BookableApplicationService $bookableAS */ |
| 45 | $bookableAS = $this->getContainer()->get('application.bookable.service'); |
| 46 | |
| 47 | $appointmentsCount = $bookableAS->getAppointmentsCountForServices([$command->getArg('id')]); |
| 48 | |
| 49 | $messageKey = ''; |
| 50 | $messageData = null; |
| 51 | |
| 52 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 53 | $messageKey = 'red_delete_service_effect_future'; |
| 54 | $messageData = ['count' => $appointmentsCount['futureAppointments']]; |
| 55 | } elseif ($appointmentsCount['packageAppointments']) { |
| 56 | $messageKey = 'red_delete_service_effect_package'; |
| 57 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 58 | $messageKey = 'red_delete_service_effect_past'; |
| 59 | $messageData = ['count' => $appointmentsCount['pastAppointments']]; |
| 60 | } |
| 61 | |
| 62 | /** @var array $packages */ |
| 63 | $packages = $packageApplicationService->getPackagesArray(['services' => [$command->getArg('id')]]); |
| 64 | |
| 65 | if ($packages && count($packages) > 0) { |
| 66 | $messageKey = 'red_service_failed_to_be_deleted'; |
| 67 | } |
| 68 | |
| 69 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 70 | $result->setMessage('Successfully retrieved message.'); |
| 71 | $result->setData( |
| 72 | [ |
| 73 | 'valid' => !$appointmentsCount['futureAppointments'] && (!$packages || count($packages) === 0), |
| 74 | 'messageKey' => $messageKey, |
| 75 | 'messageData' => $messageData, |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | return $result; |
| 80 | } |
| 81 | } |
| 82 |