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