PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.3
Booking for Appointments and Events Calendar – Amelia v2.3
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 6 months ago GetCustomerCommand.php 1 year ago GetCustomerCommandHandler.php 6 months ago GetCustomersCommand.php 1 year ago GetCustomersCommandHandler.php 2 months ago UpdateCustomerCommand.php 1 year ago UpdateCustomerCommandHandler.php 3 months ago UpdateCustomerNoteCommand.php 3 months ago UpdateCustomerNoteCommandHandler.php 3 months ago UpdateCustomerStatusCommand.php 1 year ago UpdateCustomerStatusCommandHandler.php 6 months ago
UpdateCustomerCommandHandler.php
240 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\Name;
16 use AmeliaBooking\Domain\ValueObjects\String\Password;
17 use AmeliaBooking\Domain\ValueObjects\String\Phone;
18 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
19 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
20 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
21 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
22 use AmeliaBooking\Infrastructure\Services\Mailchimp\AbstractMailchimpService;
23
24 /**
25 * Class UpdateCustomerCommandHandler
26 *
27 * @package AmeliaBooking\Application\Commands\User\Customer
28 */
29 class UpdateCustomerCommandHandler extends CommandHandler
30 {
31 /**
32 * @param UpdateCustomerCommand $command
33 *
34 * @return CommandResult
35 *
36 * @throws InvalidArgumentException
37 * @throws NotFoundException
38 * @throws QueryExecutionException
39 * @throws AccessDeniedException
40 */
41 public function handle(UpdateCustomerCommand $command)
42 {
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 UserRepository $userRepository */
51 $userRepository = $this->getContainer()->get('domain.users.repository');
52
53 /** @var AbstractMailchimpService $mailchimpService */
54 $mailchimpService = $this->container->get('infrastructure.mailchimp.service');
55
56 $userRepository->beginTransaction();
57
58 $customerData = $command->getFields();
59
60 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
61 if ($command->getToken()) {
62 /** @var AbstractUser $provider */
63 $provider = $command->getCabinetType() === 'provider'
64 ? $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet')
65 : null;
66
67 $oldUser = $provider === null
68 ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet')
69 : $userRepository->getById($customerData['id']);
70
71 if (
72 $provider === null &&
73 ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id')))
74 ) {
75 $userRepository->rollback();
76
77 $result->setResult(CommandResult::RESULT_ERROR);
78 $result->setMessage('Could not retrieve user');
79 $result->setData(
80 [
81 'reauthorize' => true
82 ]
83 );
84
85 return $result;
86 }
87 } else {
88 throw new AccessDeniedException('You are not allowed to perform this action!');
89 }
90 } else {
91 $oldUser = $userRepository->getById($command->getArg('id'));
92 if ($oldUser === null) {
93 $userRepository->rollback();
94
95 $result->setResult(CommandResult::RESULT_ERROR);
96 $result->setMessage('Could not retrieve user');
97 return $result;
98 }
99 }
100
101 if ($command->getField('externalId') === -1) {
102 $command->setField('externalId', null);
103 }
104
105 /** @var SettingsService $settingsService */
106 $settingsService = $this->container->get('domain.settings.service');
107
108 /** @var AbstractUser $currentUser */
109 $currentUser = $this->container->get('logged.in.user');
110
111 if (
112 $command->getField('email') === '' &&
113 !$settingsService->getSetting('roles', 'allowCustomerDeleteProfile') &&
114 (!$currentUser || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER)
115 ) {
116 $result->setResult(CommandResult::RESULT_ERROR);
117 $result->setMessage('Could not update user.');
118
119 return $result;
120 }
121
122 if (!isset($customerData['password'])) {
123 $customerData['translations'] = !empty($customerData['translations']) ? $customerData['translations'] : null;
124
125 $customerData['birthday'] = !empty($customerData['birthday']) ? $customerData['birthday'] : null;
126 }
127
128 $newUserData = array_merge($oldUser->toArray(), $customerData);
129
130 $newUserData = apply_filters('amelia_before_customer_updated_filter', $newUserData);
131
132 /** @var Customer $newUser */
133 $newUser = UserFactory::create($newUserData);
134
135 $oldExternalId = $oldUser->getExternalId() ? $oldUser->getExternalId()->getValue() : null;
136 $newExternalId = $newUser->getExternalId() ? $newUser->getExternalId()->getValue() : null;
137
138 if ($oldExternalId !== $newExternalId && (!$currentUser || $currentUser->getType() !== AbstractUser::USER_ROLE_ADMIN)) {
139 $result->setResult(CommandResult::RESULT_ERROR);
140 $result->setMessage('Could not update user.');
141
142 return $result;
143 }
144
145 // If the phone is not set and the old phone is set, set the phone and country phone iso to null
146 if (empty($customerData['phone']) && $oldUser->getPhone() && $oldUser->getPhone()->getValue()) {
147 $newUser->setPhone(new Phone(null));
148 $newUser->setCountryPhoneIso(new Name(null));
149 }
150
151 if (
152 $userRepository->getByEmail($newUser->getEmail()->getValue()) &&
153 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue()
154 ) {
155 $result->setResult(CommandResult::RESULT_CONFLICT);
156 $result->setMessage('Email already exist.');
157 $result->setData('This email is already in use.');
158
159 return $result;
160 }
161
162 if ($command->getField('password')) {
163 $newPassword = new Password($command->getField('password'));
164
165 $userRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password');
166
167 if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
168 add_filter('amelia_user_profile_updated', '__return_true');
169 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
170 remove_filter('amelia_user_profile_updated', '__return_true');
171 }
172 }
173
174 do_action('amelia_before_customer_updated', $newUser ? $newUser->toArray() : null);
175
176 if (!$userRepository->update($command->getArg('id'), $newUser)) {
177 $userRepository->rollback();
178
179 $result->setResult(CommandResult::RESULT_ERROR);
180 $result->setMessage('Could not update user.');
181
182 return $result;
183 }
184
185 if ($command->getField('externalId') === 0) {
186 /** @var UserApplicationService $userAS */
187 $userAS = $this->getContainer()->get('application.user.service');
188
189 $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser, Entities::CUSTOMER);
190 } elseif ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
191 add_filter('amelia_user_profile_updated', '__return_true');
192 wp_update_user(
193 [
194 'ID' => $newUser->getExternalId()->getValue(),
195 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '',
196 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '',
197 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : ''
198 ]
199 );
200
201 if ($uid = get_current_user_id()) {
202 clean_user_cache($uid);
203 }
204
205 remove_filter('amelia_user_profile_updated', '__return_true');
206 }
207
208 if ($command->getField('email') === '') {
209 /** @var CustomerBookingRepository $bookingRepository */
210 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
211
212 $bookingRepository->updateFieldByColumn('info', null, 'customerId', $oldUser->getId()->getValue());
213 }
214
215 if (
216 $settingsService->isFeatureEnabled('mailchimp') &&
217 $oldUser->getEmail() && $oldUser->getEmail()->getValue() &&
218 $newUser->getEmail() && $newUser->getEmail()->getValue()
219 ) {
220 $mailchimpService->addOrUpdateSubscriber($oldUser->getEmail()->getValue(), $newUser->toArray(), false);
221 }
222
223 $userRepository->commit();
224
225 do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null);
226
227 $result = $userAS->getAuthenticatedUserResponse(
228 $newUser,
229 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
230 true,
231 $oldUser->getLoginType(),
232 'customer'
233 );
234
235 $result->setMessage('Successfully updated user');
236
237 return $result;
238 }
239 }
240