PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.4
Booking for Appointments and Events Calendar – Amelia v1.2.4
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 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 / Customer / UpdateCustomerCommandHandler.php
ameliabooking / src / Application / Commands / User / Customer Last commit date
AddCustomerCommand.php 1 year ago AddCustomerCommandHandler.php 1 year ago GetCustomerCommand.php 1 year ago GetCustomerCommandHandler.php 1 year ago GetCustomersCommand.php 1 year ago GetCustomersCommandHandler.php 1 year ago UpdateCustomerCommand.php 1 year ago UpdateCustomerCommandHandler.php 1 year ago
UpdateCustomerCommandHandler.php
181 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\User\Customer;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\User\UserApplicationService;
9 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
10 use AmeliaBooking\Domain\Entity\Entities;
11 use AmeliaBooking\Domain\Entity\User\AbstractUser;
12 use AmeliaBooking\Domain\Entity\User\Customer;
13 use AmeliaBooking\Domain\Factory\User\UserFactory;
14 use AmeliaBooking\Domain\Services\Settings\SettingsService;
15 use AmeliaBooking\Domain\ValueObjects\String\Password;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
17 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
18 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
19 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
20 use Interop\Container\Exception\ContainerException;
21
22 /**
23 * Class UpdateCustomerCommandHandler
24 *
25 * @package AmeliaBooking\Application\Commands\User\Customer
26 */
27 class UpdateCustomerCommandHandler extends CommandHandler
28 {
29 /**
30 * @param UpdateCustomerCommand $command
31 *
32 * @return CommandResult
33 *
34 * @throws ContainerException
35 * @throws InvalidArgumentException
36 * @throws NotFoundException
37 * @throws QueryExecutionException
38 * @throws AccessDeniedException
39 */
40 public function handle(UpdateCustomerCommand $command)
41 {
42 /** @var CommandResult $result */
43 $result = new CommandResult();
44
45 $this->checkMandatoryFields($command);
46
47 /** @var UserApplicationService $userAS */
48 $userAS = $this->getContainer()->get('application.user.service');
49
50 /** @var Customer $oldUser */
51 $oldUser = null;
52
53 /** @var UserRepository $userRepository */
54 $userRepository = $this->getContainer()->get('domain.users.repository');
55
56 $userRepository->beginTransaction();
57
58 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
59 $oldUser = $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet');
60
61 if ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id'))) {
62 $result->setResult(CommandResult::RESULT_ERROR);
63 $result->setMessage('Could not retrieve user');
64 $result->setData(
65 [
66 'reauthorize' => true
67 ]
68 );
69
70 return $result;
71 }
72 } else {
73 $oldUser = $userRepository->getById($command->getArg('id'));
74 }
75
76 if ($command->getField('externalId') === -1) {
77 $command->setField('externalId', null);
78 }
79
80 /** @var SettingsService $settingsService */
81 $settingsService = $this->container->get('domain.settings.service');
82
83 /** @var AbstractUser $currentUser */
84 $currentUser = $this->container->get('logged.in.user');
85
86 if ($command->getField('email') === '' &&
87 !$settingsService->getSetting('roles', 'allowCustomerDeleteProfile') &&
88 (!$currentUser || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER)
89 ) {
90 $result->setResult(CommandResult::RESULT_ERROR);
91 $result->setMessage('Could not update user.');
92
93 return $result;
94 }
95
96 $customerData = $command->getFields();
97
98 if (!isset($customerData['password'])) {
99 $customerData['translations'] = !empty($customerData['translations']) ? $customerData['translations'] : null;
100 $customerData['birthday'] = !empty($customerData['birthday']) ? $customerData['birthday'] : null;
101 }
102
103 $newUserData = array_merge($oldUser->toArray(), $customerData);
104
105 $newUserData = apply_filters('amelia_before_customer_updated_filter', $newUserData);
106
107 /** @var Customer $newUser */
108 $newUser = UserFactory::create($newUserData);
109
110 if (!($newUser instanceof AbstractUser)) {
111 $result->setResult(CommandResult::RESULT_ERROR);
112 $result->setMessage('Could not update user.');
113
114 return $result;
115 }
116
117 if ($oldUser &&
118 $userRepository->getByEmail($newUser->getEmail()->getValue()) &&
119 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue()
120 ) {
121 $result->setResult(CommandResult::RESULT_CONFLICT);
122 $result->setMessage('Email already exist.');
123 $result->setData('This email is already in use.');
124
125 return $result;
126 }
127
128 if ($command->getField('password')) {
129 /** @var Password $newPassword */
130 $newPassword = new Password($command->getField('password'));
131
132 $userRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password');
133
134 if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
135 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
136 }
137 }
138
139 do_action('amelia_before_customer_updated', $newUser? $newUser->toArray() : null);
140
141 if (!$userRepository->update($command->getArg('id'), $newUser)) {
142 $userRepository->rollback();
143
144 $result->setResult(CommandResult::RESULT_ERROR);
145 $result->setMessage('Could not update user.');
146
147 return $result;
148 }
149
150 if ($command->getField('externalId') === 0) {
151 /** @var UserApplicationService $userAS */
152 $userAS = $this->getContainer()->get('application.user.service');
153
154 $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser);
155 }
156
157 if ($command->getField('email') === '') {
158 /** @var CustomerBookingRepository $bookingRepository */
159 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
160
161 $bookingRepository->updateInfoByCustomerId($oldUser->getId()->getValue(), null);
162 }
163
164 $userRepository->commit();
165
166 do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null);
167
168 $result = $userAS->getAuthenticatedUserResponse(
169 $newUser,
170 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
171 true,
172 $oldUser->getLoginType(),
173 'customer'
174 );
175
176 $result->setMessage('Successfully updated user');
177
178 return $result;
179 }
180 }
181