Customer
1 year ago
Provider
1 year ago
DeleteUserCommand.php
7 years ago
DeleteUserCommandHandler.php
2 years ago
GetCurrentUserCommand.php
7 years ago
GetCurrentUserCommandHandler.php
2 years ago
GetUserDeleteEffectCommand.php
7 years ago
GetUserDeleteEffectCommandHandler.php
1 year ago
GetWPUsersCommand.php
7 years ago
GetWPUsersCommandHandler.php
2 years ago
GetUserDeleteEffectCommandHandler.php
97 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 (!$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) && |
| 40 | !$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) |
| 41 | ) { |
| 42 | throw new AccessDeniedException('You are not allowed to read user'); |
| 43 | } |
| 44 | |
| 45 | $result = new CommandResult(); |
| 46 | |
| 47 | /** @var UserApplicationService $userAS */ |
| 48 | $userAS = $this->getContainer()->get('application.user.service'); |
| 49 | |
| 50 | /** @var EventRepository $eventRepository */ |
| 51 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 52 | |
| 53 | $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id')); |
| 54 | |
| 55 | $eventsIds = $eventRepository->getFilteredIds( |
| 56 | [ |
| 57 | 'customerId' => $command->getArg('id'), |
| 58 | 'customerBookingStatus' => BookingStatus::APPROVED, |
| 59 | 'dates' => [DateTimeService::getNowDateTime()], |
| 60 | ], |
| 61 | 0 |
| 62 | ); |
| 63 | |
| 64 | $message = ''; |
| 65 | |
| 66 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 67 | $appointmentString = $appointmentsCount['futureAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 68 | |
| 69 | $message = "Could not delete user. |
| 70 | This user has {$appointmentsCount['futureAppointments']} {$appointmentString} in the future."; |
| 71 | } elseif ($appointmentsCount['packageAppointments']) { |
| 72 | $message = "This user has bookings in purchased package. |
| 73 | Are you sure you want to delete this user?"; |
| 74 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 75 | $appointmentString = $appointmentsCount['pastAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 76 | |
| 77 | $message = "This user has {$appointmentsCount['pastAppointments']} {$appointmentString} in the past."; |
| 78 | } elseif ($eventsIds) { |
| 79 | $eventString = sizeof($eventsIds) > 1 ? 'events' : 'event'; |
| 80 | |
| 81 | $message = "This user is an attendee in future {$eventString}. |
| 82 | Are you sure you want to delete this user?"; |
| 83 | } |
| 84 | |
| 85 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 86 | $result->setMessage('Successfully retrieved message.'); |
| 87 | $result->setData( |
| 88 | [ |
| 89 | 'valid' => $appointmentsCount['futureAppointments'] ? false : true, |
| 90 | 'message' => $message |
| 91 | ] |
| 92 | ); |
| 93 | |
| 94 | return $result; |
| 95 | } |
| 96 | } |
| 97 |