Customer
2 days ago
Provider
2 days ago
DeleteUserCommand.php
1 year ago
DeleteUserCommandHandler.php
1 year ago
GetCurrentUserCommand.php
1 year ago
GetCurrentUserCommandHandler.php
1 year ago
GetUserDeleteEffectCommand.php
1 year ago
GetUserDeleteEffectCommandHandler.php
6 months ago
GetWPUsersCommand.php
1 year ago
GetWPUsersCommandHandler.php
6 months ago
GetUserDeleteEffectCommandHandler.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User; |
| 4 | |
| 5 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 6 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 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\Domain\Services\DateTime\DateTimeService; |
| 12 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 15 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 16 | use Interop\Container\Exception\ContainerException; |
| 17 | use Slim\Exception\ContainerValueNotFoundException; |
| 18 | |
| 19 | /** |
| 20 | * Class GetUserDeleteEffectCommandHandler |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Commands\User |
| 23 | */ |
| 24 | class GetUserDeleteEffectCommandHandler extends CommandHandler |
| 25 | { |
| 26 | /** |
| 27 | * @param GetUserDeleteEffectCommand $command |
| 28 | * |
| 29 | * @return CommandResult |
| 30 | * @throws ContainerValueNotFoundException |
| 31 | * @throws AccessDeniedException |
| 32 | * @throws InvalidArgumentException |
| 33 | * @throws NotFoundException |
| 34 | * @throws QueryExecutionException |
| 35 | * @throws ContainerException |
| 36 | */ |
| 37 | public function handle(GetUserDeleteEffectCommand $command) |
| 38 | { |
| 39 | if ( |
| 40 | !$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) && |
| 41 | !$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) |
| 42 | ) { |
| 43 | throw new AccessDeniedException('You are not allowed to read user'); |
| 44 | } |
| 45 | |
| 46 | $result = new CommandResult(); |
| 47 | |
| 48 | /** @var UserApplicationService $userAS */ |
| 49 | $userAS = $this->getContainer()->get('application.user.service'); |
| 50 | |
| 51 | /** @var EventRepository $eventRepository */ |
| 52 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 53 | |
| 54 | $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id')); |
| 55 | |
| 56 | $eventsIds = $eventRepository->getFilteredIds( |
| 57 | [ |
| 58 | 'customerId' => $command->getArg('id'), |
| 59 | 'customerBookingStatus' => BookingStatus::APPROVED, |
| 60 | 'dates' => [DateTimeService::getNowDateTime()], |
| 61 | ], |
| 62 | 0 |
| 63 | ); |
| 64 | |
| 65 | $messageKey = ''; |
| 66 | $messageData = null; |
| 67 | |
| 68 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 69 | $messageKey = 'red_delete_user_effect_future'; |
| 70 | $messageData = ['count' => $appointmentsCount['futureAppointments']]; |
| 71 | } elseif ($appointmentsCount['packageAppointments']) { |
| 72 | $messageKey = 'red_delete_user_effect_package'; |
| 73 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 74 | $messageKey = 'red_delete_user_effect_past'; |
| 75 | $messageData = ['count' => $appointmentsCount['pastAppointments']]; |
| 76 | } elseif ($eventsIds) { |
| 77 | $messageKey = 'red_delete_user_event_effect_future'; |
| 78 | $messageData = ['count' => count($eventsIds)]; |
| 79 | } |
| 80 | |
| 81 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 82 | $result->setMessage('Successfully retrieved message.'); |
| 83 | $result->setData( |
| 84 | [ |
| 85 | 'valid' => ! $appointmentsCount['futureAppointments'], |
| 86 | 'messageKey' => $messageKey, |
| 87 | 'messageData' => $messageData, |
| 88 | 'futureAppointmentsCount' => $appointmentsCount['futureAppointments'], |
| 89 | 'packageAppointmentsCount' => $appointmentsCount['packageAppointments'], |
| 90 | 'pastAppointmentsCount' => $appointmentsCount['pastAppointments'], |
| 91 | 'eventsCount' => count($eventsIds) |
| 92 | ] |
| 93 | ); |
| 94 | |
| 95 | return $result; |
| 96 | } |
| 97 | } |
| 98 |