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
DeleteUserCommandHandler.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User; |
| 4 | |
| 5 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 6 | use AmeliaBooking\Application\Services\User\CustomerApplicationService; |
| 7 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 8 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 12 | use AmeliaBooking\Domain\Entity\Entities; |
| 13 | use AmeliaBooking\Application\Commands\CommandResult; |
| 14 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 15 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 16 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 20 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 21 | use Interop\Container\Exception\ContainerException; |
| 22 | use Slim\Exception\ContainerValueNotFoundException; |
| 23 | |
| 24 | /** |
| 25 | * Class DeleteUserCommandHandler |
| 26 | * |
| 27 | * @package AmeliaBooking\Application\Commands\User |
| 28 | */ |
| 29 | class DeleteUserCommandHandler extends CommandHandler |
| 30 | { |
| 31 | /** |
| 32 | * @param DeleteUserCommand $command |
| 33 | * |
| 34 | * @return CommandResult |
| 35 | * @throws ContainerValueNotFoundException |
| 36 | * @throws InvalidArgumentException |
| 37 | * @throws AccessDeniedException |
| 38 | * @throws ContainerException |
| 39 | * @throws QueryExecutionException |
| 40 | * @throws NotFoundException |
| 41 | */ |
| 42 | public function handle(DeleteUserCommand $command) |
| 43 | { |
| 44 | if ( |
| 45 | !$command->getPermissionService()->currentUserCanDelete(Entities::EMPLOYEES) && |
| 46 | !$command->getPermissionService()->currentUserCanDelete(Entities::CUSTOMERS) |
| 47 | ) { |
| 48 | throw new AccessDeniedException('You are not allowed to read user'); |
| 49 | } |
| 50 | |
| 51 | $result = new CommandResult(); |
| 52 | |
| 53 | /** @var UserApplicationService $userAS */ |
| 54 | $userAS = $this->getContainer()->get('application.user.service'); |
| 55 | |
| 56 | /** @var AppointmentRepository $appointmentRepository */ |
| 57 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 58 | |
| 59 | $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id')); |
| 60 | |
| 61 | /** @var UserRepository $userRepository */ |
| 62 | $userRepository = $this->container->get('domain.users.repository'); |
| 63 | |
| 64 | if ($appointmentsCount['futureAppointments']) { |
| 65 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 66 | $result->setMessage('Could not delete user.'); |
| 67 | $result->setData([]); |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | |
| 72 | /** @var AbstractUser $user */ |
| 73 | $user = $userRepository->getById($command->getArg('id')); |
| 74 | |
| 75 | $userRepository->beginTransaction(); |
| 76 | |
| 77 | do_action('amelia_before_user_deleted', $user ? $user->toArray() : null); |
| 78 | |
| 79 | if ($user->getType() === AbstractUser::USER_ROLE_PROVIDER) { |
| 80 | /** @var ProviderApplicationService $providerApplicationService */ |
| 81 | $providerApplicationService = $this->getContainer()->get('application.user.provider.service'); |
| 82 | |
| 83 | /** @var Provider $provider */ |
| 84 | $provider = $providerApplicationService->getProviderWithServicesAndSchedule($user->getId()->getValue()); |
| 85 | |
| 86 | if (!$providerApplicationService->delete($provider)) { |
| 87 | $result->setResult(CommandResult::RESULT_ERROR); |
| 88 | $result->setMessage('Could not delete user.'); |
| 89 | $userRepository->rollback(); |
| 90 | |
| 91 | return $result; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ( |
| 96 | $user->getType() === AbstractUser::USER_ROLE_CUSTOMER || |
| 97 | $user->getType() === AbstractUser::USER_ROLE_ADMIN |
| 98 | ) { |
| 99 | /** @var CustomerApplicationService $customerApplicationService */ |
| 100 | $customerApplicationService = $this->getContainer()->get('application.user.customer.service'); |
| 101 | |
| 102 | if (!$customerApplicationService->delete($user)) { |
| 103 | $result->setResult(CommandResult::RESULT_ERROR); |
| 104 | $result->setMessage('Could not delete user.'); |
| 105 | $userRepository->rollback(); |
| 106 | |
| 107 | return $result; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @var Collection $emptyAppointments */ |
| 112 | $emptyAppointments = $appointmentRepository->getAppointmentsWithoutBookings(); |
| 113 | |
| 114 | /** @var Appointment $appointment */ |
| 115 | foreach ($emptyAppointments->getItems() as $appointment) { |
| 116 | if (!$appointmentRepository->delete($appointment->getId()->getValue())) { |
| 117 | $result->setResult(CommandResult::RESULT_ERROR); |
| 118 | $result->setMessage('Could not delete user.'); |
| 119 | $userRepository->rollback(); |
| 120 | |
| 121 | return $result; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $userRepository->commit(); |
| 126 | |
| 127 | do_action('amelia_after_user_deleted', $user->toArray()); |
| 128 | |
| 129 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 130 | $result->setMessage('Successfully deleted user.'); |
| 131 | $result->setData([]); |
| 132 | |
| 133 | return $result; |
| 134 | } |
| 135 | } |
| 136 |