Customer
1 year ago
Provider
1 year 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
1 year ago
GetWPUsersCommand.php
1 year ago
GetWPUsersCommandHandler.php
1 year ago
GetUserDeleteEffectCommandHandler.php
99 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\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\Domain\Services\DateTime\DateTimeService; |
| 13 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 17 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 18 | use Interop\Container\Exception\ContainerException; |
| 19 | use Slim\Exception\ContainerValueNotFoundException; |
| 20 | |
| 21 | /** |
| 22 | * Class GetUserDeleteEffectCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\User |
| 25 | */ |
| 26 | class GetUserDeleteEffectCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @param GetUserDeleteEffectCommand $command |
| 30 | * |
| 31 | * @return CommandResult |
| 32 | * @throws ContainerValueNotFoundException |
| 33 | * @throws AccessDeniedException |
| 34 | * @throws InvalidArgumentException |
| 35 | * @throws NotFoundException |
| 36 | * @throws QueryExecutionException |
| 37 | * @throws ContainerException |
| 38 | */ |
| 39 | public function handle(GetUserDeleteEffectCommand $command) |
| 40 | { |
| 41 | if (!$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) && |
| 42 | !$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) |
| 43 | ) { |
| 44 | throw new AccessDeniedException('You are not allowed to read user'); |
| 45 | } |
| 46 | |
| 47 | $result = new CommandResult(); |
| 48 | |
| 49 | /** @var UserApplicationService $userAS */ |
| 50 | $userAS = $this->getContainer()->get('application.user.service'); |
| 51 | |
| 52 | /** @var EventRepository $eventRepository */ |
| 53 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 54 | |
| 55 | $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id')); |
| 56 | |
| 57 | /** @var Collection $events */ |
| 58 | $events = $eventRepository->getFiltered( |
| 59 | [ |
| 60 | 'customerId' => $command->getArg('id'), |
| 61 | 'bookingStatus' => BookingStatus::APPROVED, |
| 62 | 'dates' => [DateTimeService::getNowDateTime()], |
| 63 | ] |
| 64 | ); |
| 65 | |
| 66 | $message = ''; |
| 67 | |
| 68 | if ($appointmentsCount['futureAppointments'] > 0) { |
| 69 | $appointmentString = $appointmentsCount['futureAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 70 | |
| 71 | $message = "Could not delete user. |
| 72 | This user has {$appointmentsCount['futureAppointments']} {$appointmentString} in the future."; |
| 73 | } elseif ($appointmentsCount['packageAppointments']) { |
| 74 | $message = "This service is available for booking in purchased package. |
| 75 | Are you sure you want to delete this user?"; |
| 76 | } elseif ($appointmentsCount['pastAppointments'] > 0) { |
| 77 | $appointmentString = $appointmentsCount['pastAppointments'] === 1 ? 'appointment' : 'appointments'; |
| 78 | |
| 79 | $message = "This user has {$appointmentsCount['pastAppointments']} {$appointmentString} in the past."; |
| 80 | } elseif ($events->length()) { |
| 81 | $eventString = $events->length() > 1 ? 'events' : 'event'; |
| 82 | |
| 83 | $message = "This user is an attendee in future {$eventString}. |
| 84 | Are you sure you want to delete this user?"; |
| 85 | } |
| 86 | |
| 87 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 88 | $result->setMessage('Successfully retrieved message.'); |
| 89 | $result->setData( |
| 90 | [ |
| 91 | 'valid' => $appointmentsCount['futureAppointments'] ? false : true, |
| 92 | 'message' => $message |
| 93 | ] |
| 94 | ); |
| 95 | |
| 96 | return $result; |
| 97 | } |
| 98 | } |
| 99 |