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