PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.25
Booking for Appointments and Events Calendar – Amelia v1.2.25
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 / Provider / UpdateProviderCommandHandler.php
ameliabooking / src / Application / Commands / User / Provider Last commit date
AddProviderCommand.php 7 years ago AddProviderCommandHandler.php 2 years ago GetProviderCommand.php 7 years ago GetProviderCommandHandler.php 1 year ago GetProvidersCommand.php 7 years ago GetProvidersCommandHandler.php 1 year ago UpdateProviderCommand.php 7 years ago UpdateProviderCommandHandler.php 1 year ago UpdateProviderStatusCommand.php 7 years ago UpdateProviderStatusCommandHandler.php 2 years ago
UpdateProviderCommandHandler.php
243 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\User\Provider;
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\Entity\EntityApplicationService;
9 use AmeliaBooking\Application\Services\User\ProviderApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
12 use AmeliaBooking\Domain\Entity\Entities;
13 use AmeliaBooking\Domain\Entity\User\AbstractUser;
14 use AmeliaBooking\Domain\Entity\User\Provider;
15 use AmeliaBooking\Domain\Factory\User\UserFactory;
16 use AmeliaBooking\Domain\Services\Settings\SettingsService;
17 use AmeliaBooking\Domain\ValueObjects\String\Password;
18 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
19 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
20 use AmeliaBooking\Infrastructure\Services\Apple\AbstractAppleCalendarService;
21 use Interop\Container\Exception\ContainerException;
22 use Slim\Exception\ContainerValueNotFoundException;
23
24 /**
25 * Class UpdateProviderCommandHandler
26 *
27 * @package AmeliaBooking\Application\Commands\User\Provider
28 */
29 class UpdateProviderCommandHandler extends CommandHandler
30 {
31 /**
32 * @param UpdateProviderCommand $command
33 *
34 * @return CommandResult
35 * @throws ContainerValueNotFoundException
36 * @throws AccessDeniedException
37 * @throws InvalidArgumentException
38 * @throws QueryExecutionException
39 * @throws ContainerException
40 */
41 public function handle(UpdateProviderCommand $command)
42 {
43 $result = new CommandResult();
44
45 $this->checkMandatoryFields($command);
46
47 /** @var ProviderRepository $providerRepository */
48 $providerRepository = $this->container->get('domain.users.providers.repository');
49
50 /** @var ProviderApplicationService $providerAS */
51 $providerAS = $this->container->get('application.user.provider.service');
52
53 $userId = (int)$command->getArg('id');
54
55 /** @var AbstractUser $currentUser */
56 $currentUser = $this->container->get('logged.in.user');
57
58 /** @var UserApplicationService $userAS */
59 $userAS = $this->getContainer()->get('application.user.service');
60
61 if (!$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES) ||
62 (
63 !$command->getPermissionService()->currentUserCanWriteOthers(Entities::EMPLOYEES) &&
64 (
65 !$currentUser->getId() ||
66 $currentUser->getId()->getValue() !== $userId
67 )
68 )
69
70 ) {
71 $oldUser = $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet');
72
73 if ($oldUser === null) {
74 $result->setResult(CommandResult::RESULT_ERROR);
75 $result->setMessage('Could not retrieve user');
76 $result->setData(
77 [
78 'reauthorize' => true
79 ]
80 );
81
82 return $result;
83 }
84
85 $oldUser = $providerAS->getProviderWithServicesAndSchedule($oldUser->getId()->getValue());
86 } else {
87 $oldUser = $providerAS->getProviderWithServicesAndSchedule($userId);
88 }
89
90 $command->setField('id', $userId);
91
92 $providerData = $command->getFields();
93
94 if (!isset($providerData['stripeConnect'])) {
95 $providerData['stripeConnect'] = null;
96 }
97
98 if (!isset($providerData['employeeAppleCalendar'])) {
99 $providerData['employeeAppleCalendar'] = null;
100 } else {
101 /** @var AbstractAppleCalendarService $appleCalendarService */
102 $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service');
103
104 $appleId = $providerData['employeeAppleCalendar'] ['iCloudId'];
105 $applePassword = $providerData['employeeAppleCalendar'] ['appSpecificPassword'];
106
107 $credentials = $appleCalendarService->handleAppleCredentials($appleId, $applePassword);
108
109 if (!$credentials) {
110 $providerData['employeeAppleCalendar'] = null;
111 }
112 }
113
114 /** @var EntityApplicationService $entityService */
115 $entityService = $this->container->get('application.entity.service');
116
117 $entityService->removeMissingEntitiesForProvider($providerData);
118
119 if (!!$oldUser->getBadgeId() && !isset($providerData['badgeId'])) {
120 $providerData['badgeId'] = null;
121 }
122
123 $newUserData = array_merge($oldUser->toArray(), $providerData);
124
125 $newUserData = apply_filters('amelia_before_provider_updated_filter', $newUserData, $oldUser->toArray());
126
127 /** @var Provider $newUser */
128 $newUser = UserFactory::create($newUserData);
129
130 if (!($newUser instanceof AbstractUser)) {
131 $result->setResult(CommandResult::RESULT_ERROR);
132 $result->setMessage('Could not update user.');
133
134 return $result;
135 }
136
137 if ($command->getUserApplicationService()->checkProviderPermissions($currentUser, $command->getToken())) {
138 /** @var SettingsService $settingsDS */
139 $settingsDS = $this->container->get('domain.settings.service');
140
141 $rolesSettings = $settingsDS->getCategorySettings('roles');
142
143 if (!$rolesSettings['allowConfigureServices']) {
144 $newUser->setServiceList($oldUser->getServiceList());
145 }
146
147 if (!$rolesSettings['allowConfigureSchedule']) {
148 $newUser->setWeekDayList($oldUser->getWeekDayList());
149 }
150
151 if (!$rolesSettings['allowConfigureDaysOff']) {
152 $newUser->setDayOffList($oldUser->getDayOffList());
153 }
154
155 if (!$rolesSettings['allowConfigureSpecialDays']) {
156 $newUser->setSpecialDayList($oldUser->getSpecialDayList());
157 }
158 }
159
160 $providerRepository->beginTransaction();
161
162 if ($providerRepository->getByEmail($newUser->getEmail()->getValue()) &&
163 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue()) {
164 $result->setResult(CommandResult::RESULT_CONFLICT);
165 $result->setMessage('Email already exist.');
166 $result->setData('This email is already in use.');
167
168 return $result;
169 }
170
171 if ($command->getField('password')) {
172 $newPassword = new Password($command->getField('password'));
173
174 $providerRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password');
175
176 if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
177 add_filter('amelia_user_profile_updated', '__return_true');
178 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
179 remove_filter('amelia_user_profile_updated', '__return_true');
180 }
181 }
182
183 do_action('amelia_before_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
184
185 try {
186 if (!$providerAS->update($oldUser, $newUser)) {
187 $providerRepository->rollback();
188 return $result;
189 }
190
191 if ($command->getField('externalId') === 0) {
192 /** @var UserApplicationService $userAS */
193 $userAS = $this->getContainer()->get('application.user.service');
194
195 $userAS->setWpUserIdForNewUser($userId, $newUser, $command->getField('password'));
196 } else if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
197 add_filter('amelia_user_profile_updated', '__return_true');
198 wp_update_user(
199 [
200 'ID' => $newUser->getExternalId()->getValue(),
201 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '',
202 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '',
203 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : ''
204 ]
205 );
206
207 if ($uid = get_current_user_id()) {
208 clean_user_cache($uid);
209 }
210
211 remove_filter('amelia_user_profile_updated', '__return_true');
212 }
213 } catch (QueryExecutionException $e) {
214 $providerRepository->rollback();
215 throw $e;
216 }
217
218 $result = $userAS->getAuthenticatedUserResponse(
219 $newUser,
220 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
221 true,
222 $oldUser->getLoginType(),
223 'provider'
224 );
225
226 $result->setData(
227 array_merge(
228 $result->getData(),
229 ['sendEmployeePanelAccessEmail' =>
230 $command->getField('password') && $command->getField('sendEmployeePanelAccessEmail'),
231 'password' => $command->getField('password')
232 ]
233 )
234 );
235
236 $providerRepository->commit();
237
238 do_action('amelia_after_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
239
240 return $result;
241 }
242 }
243