ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Service
/
GetServiceDeleteEffectCommandHandler.php
AddServiceCommand.php
7 years ago
AddServiceCommandHandler.php
2 years ago
DeleteServiceCommand.php
7 years ago
DeleteServiceCommandHandler.php
2 years ago
GetServiceCommand.php
7 years ago
GetServiceCommandHandler.php
2 years ago
GetServiceDeleteEffectCommand.php
7 years ago
GetServiceDeleteEffectCommandHandler.php
2 years ago
GetServicesCommand.php
7 years ago
GetServicesCommandHandler.php
2 years ago
UpdateServiceCommand.php
7 years ago
UpdateServiceCommandHandler.php
2 years ago
UpdateServiceStatusCommand.php
7 years ago
UpdateServiceStatusCommandHandler.php
2 years ago
UpdateServicesPositionsCommand.php
7 years ago
UpdateServicesPositionsCommandHandler.php
2 years ago
GetServiceDeleteEffectCommandHandler.php
73 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\Common\Exceptions\InvalidArgumentException; |
| 8 | use AmeliaBooking\Domain\Entity\Entities; |
| 9 | use AmeliaBooking\Application\Commands\CommandResult; |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 12 | use Interop\Container\Exception\ContainerException; |
| 13 | use Slim\Exception\ContainerValueNotFoundException; |
| 14 | |
| 15 | /** |
| 16 | * Class GetServiceDeleteEffectCommandHandler |
| 17 | * |
| 18 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 19 | */ |
| 20 | class GetServiceDeleteEffectCommandHandler extends CommandHandler |
| 21 | { |
| 22 | /** |
| 23 | * @param GetServiceDeleteEffectCommand $command |
| 24 | * |
| 25 | * @return CommandResult |
| 26 | * @throws ContainerValueNotFoundException |
| 27 | * @throws AccessDeniedException |
| 28 | * @throws QueryExecutionException |
| 29 | * @throws ContainerException |
| 30 | * @throws InvalidArgumentException |
| 31 | */ |
| 32 | public function handle(GetServiceDeleteEffectCommand $command) |
| 33 | { |
| 34 | if (!$command->getPermissionService()->currentUserCanRead(Entities::SERVICES)) { |
| 35 | throw new AccessDeniedException('You are not allowed to read services'); |
| 36 | } |
| 37 | |
| 38 | $result = new CommandResult(); |
| 39 | |
| 40 | /** @var BookableApplicationService $bookableAS */ |
| 41 | $bookableAS = $this->getContainer()->get('application.bookable.service'); |
| 42 | |
| 43 | $appointmentsCount = $bookableAS->getAppointmentsCountForServices([$command->getArg('id')]); |
| 44 | |
| 45 | $message = ''; |
| 46 | |
| 47 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 48 | $appointmentString = $appointmentsCount['futureAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 49 | |
| 50 | $message = "Could not delete service. |
| 51 | This service has {$appointmentsCount['futureAppointments']} {$appointmentString} in the future."; |
| 52 | } elseif ($appointmentsCount['packageAppointments']) { |
| 53 | $message = "This service is available for booking in purchased package. |
| 54 | Are you sure you want to delete this service?"; |
| 55 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 56 | $appointmentString = $appointmentsCount['pastAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 57 | |
| 58 | $message = "This service has {$appointmentsCount['pastAppointments']} {$appointmentString} in the past."; |
| 59 | } |
| 60 | |
| 61 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 62 | $result->setMessage('Successfully retrieved message.'); |
| 63 | $result->setData( |
| 64 | [ |
| 65 | 'valid' => $appointmentsCount['futureAppointments'] ? false : true, |
| 66 | 'message' => $message |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | return $result; |
| 71 | } |
| 72 | } |
| 73 |