PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / User / DeleteUserCommandHandler.php
ameliabooking / src / Application / Commands / User Last commit date
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
DeleteUserCommandHandler.php
134 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 (!$command->getPermissionService()->currentUserCanDelete(Entities::EMPLOYEES) &&
45 !$command->getPermissionService()->currentUserCanDelete(Entities::CUSTOMERS)
46 ) {
47 throw new AccessDeniedException('You are not allowed to read user');
48 }
49
50 $result = new CommandResult();
51
52 /** @var UserApplicationService $userAS */
53 $userAS = $this->getContainer()->get('application.user.service');
54
55 /** @var AppointmentRepository $appointmentRepository */
56 $appointmentRepository = $this->container->get('domain.booking.appointment.repository');
57
58 $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id'));
59
60 /** @var UserRepository $userRepository */
61 $userRepository = $this->container->get('domain.users.repository');
62
63 if ($appointmentsCount['futureAppointments']) {
64 $result->setResult(CommandResult::RESULT_CONFLICT);
65 $result->setMessage('Could not delete user.');
66 $result->setData([]);
67
68 return $result;
69 }
70
71 /** @var AbstractUser $user */
72 $user = $userRepository->getById($command->getArg('id'));
73
74 $userRepository->beginTransaction();
75
76 do_action('amelia_before_user_deleted', $user ? $user->toArray() : null);
77
78 if ($user->getType() === AbstractUser::USER_ROLE_PROVIDER) {
79 /** @var ProviderApplicationService $providerApplicationService */
80 $providerApplicationService = $this->getContainer()->get('application.user.provider.service');
81
82 /** @var Provider $provider */
83 $provider = $providerApplicationService->getProviderWithServicesAndSchedule($user->getId()->getValue());
84
85 if (!$providerApplicationService->delete($provider)) {
86 $result->setResult(CommandResult::RESULT_ERROR);
87 $result->setMessage('Could not delete user.');
88 $userRepository->rollback();
89
90 return $result;
91 }
92 }
93
94 if ($user->getType() === AbstractUser::USER_ROLE_CUSTOMER ||
95 $user->getType() === AbstractUser::USER_ROLE_ADMIN
96 ) {
97 /** @var CustomerApplicationService $customerApplicationService */
98 $customerApplicationService = $this->getContainer()->get('application.user.customer.service');
99
100 if (!$customerApplicationService->delete($user)) {
101 $result->setResult(CommandResult::RESULT_ERROR);
102 $result->setMessage('Could not delete user.');
103 $userRepository->rollback();
104
105 return $result;
106 }
107 }
108
109 /** @var Collection $emptyAppointments */
110 $emptyAppointments = $appointmentRepository->getAppointmentsWithoutBookings();
111
112 /** @var Appointment $appointment */
113 foreach ($emptyAppointments->getItems() as $appointment) {
114 if (!$appointmentRepository->delete($appointment->getId()->getValue())) {
115 $result->setResult(CommandResult::RESULT_ERROR);
116 $result->setMessage('Could not delete user.');
117 $userRepository->rollback();
118
119 return $result;
120 }
121 }
122
123 $userRepository->commit();
124
125 do_action('amelia_after_user_deleted', $user->toArray());
126
127 $result->setResult(CommandResult::RESULT_SUCCESS);
128 $result->setMessage('Successfully deleted user.');
129 $result->setData([]);
130
131 return $result;
132 }
133 }
134