PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.11
Booking for Appointments and Events Calendar – Amelia v1.2.11
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 7 years ago AddCustomerCommandHandler.php 2 years ago GetCustomerCommand.php 7 years ago GetCustomerCommandHandler.php 2 years ago GetCustomersCommand.php 7 years ago GetCustomersCommandHandler.php 2 years ago UpdateCustomerCommand.php 7 years ago UpdateCustomerCommandHandler.php 1 year ago
UpdateCustomerCommandHandler.php
199 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 add_filter('amelia_user_profile_updated', '__return_true');
136 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
137 remove_filter('amelia_user_profile_updated', '__return_true');
138 }
139 }
140
141 do_action('amelia_before_customer_updated', $newUser? $newUser->toArray() : null);
142
143 if (!$userRepository->update($command->getArg('id'), $newUser)) {
144 $userRepository->rollback();
145
146 $result->setResult(CommandResult::RESULT_ERROR);
147 $result->setMessage('Could not update user.');
148
149 return $result;
150 }
151
152 if ($command->getField('externalId') === 0) {
153 /** @var UserApplicationService $userAS */
154 $userAS = $this->getContainer()->get('application.user.service');
155
156 $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser);
157 } else if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
158 add_filter('amelia_user_profile_updated', '__return_true');
159 wp_update_user(
160 [
161 'ID' => $newUser->getExternalId()->getValue(),
162 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '',
163 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '',
164 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : ''
165 ]
166 );
167
168 if ($uid = get_current_user_id()) {
169 clean_user_cache($uid);
170 }
171
172 remove_filter('amelia_user_profile_updated', '__return_true');
173 }
174
175 if ($command->getField('email') === '') {
176 /** @var CustomerBookingRepository $bookingRepository */
177 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
178
179 $bookingRepository->updateInfoByCustomerId($oldUser->getId()->getValue(), null);
180 }
181
182 $userRepository->commit();
183
184 do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null);
185
186 $result = $userAS->getAuthenticatedUserResponse(
187 $newUser,
188 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
189 true,
190 $oldUser->getLoginType(),
191 'customer'
192 );
193
194 $result->setMessage('Successfully updated user');
195
196 return $result;
197 }
198 }
199