PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 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