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 / Provider / UpdateProviderCommandHandler.php
ameliabooking / src / Application / Commands / User / Provider Last commit date
AddProviderCommand.php 1 year ago AddProviderCommandHandler.php 1 year ago GetProviderCommand.php 7 years ago GetProviderCommandHandler.php 1 year ago GetProvidersCommand.php 1 year ago GetProvidersCommandHandler.php 1 year ago UpdateProviderCommand.php 1 year ago UpdateProviderCommandHandler.php 1 year ago UpdateProviderStatusCommand.php 1 year ago UpdateProviderStatusCommandHandler.php 2 years ago
UpdateProviderCommandHandler.php
245 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 (
62 !$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES) ||
63 (
64 !$command->getPermissionService()->currentUserCanWriteOthers(Entities::EMPLOYEES) &&
65 (
66 !$currentUser->getId() ||
67 $currentUser->getId()->getValue() !== $userId
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 (
163 $providerRepository->getByEmail($newUser->getEmail()->getValue()) &&
164 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue()
165 ) {
166 $result->setResult(CommandResult::RESULT_CONFLICT);
167 $result->setMessage('Email already exist.');
168 $result->setData('This email is already in use.');
169
170 return $result;
171 }
172
173 if ($command->getField('password')) {
174 $newPassword = new Password($command->getField('password'));
175
176 $providerRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password');
177
178 if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
179 add_filter('amelia_user_profile_updated', '__return_true');
180 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
181 remove_filter('amelia_user_profile_updated', '__return_true');
182 }
183 }
184
185 do_action('amelia_before_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
186
187 try {
188 if (!$providerAS->update($oldUser, $newUser)) {
189 $providerRepository->rollback();
190 return $result;
191 }
192
193 if ($command->getField('externalId') === 0) {
194 /** @var UserApplicationService $userAS */
195 $userAS = $this->getContainer()->get('application.user.service');
196
197 $userAS->setWpUserIdForNewUser($userId, $newUser, $command->getField('password'));
198 } elseif ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
199 add_filter('amelia_user_profile_updated', '__return_true');
200 wp_update_user(
201 [
202 'ID' => $newUser->getExternalId()->getValue(),
203 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '',
204 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '',
205 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : ''
206 ]
207 );
208
209 if ($uid = get_current_user_id()) {
210 clean_user_cache($uid);
211 }
212
213 remove_filter('amelia_user_profile_updated', '__return_true');
214 }
215 } catch (QueryExecutionException $e) {
216 $providerRepository->rollback();
217 throw $e;
218 }
219
220 $result = $userAS->getAuthenticatedUserResponse(
221 $newUser,
222 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
223 true,
224 $oldUser->getLoginType(),
225 'provider'
226 );
227
228 $result->setData(
229 array_merge(
230 $result->getData(),
231 ['sendEmployeePanelAccessEmail' =>
232 $command->getField('password') && $command->getField('sendEmployeePanelAccessEmail'),
233 'password' => $command->getField('password')
234 ]
235 )
236 );
237
238 $providerRepository->commit();
239
240 do_action('amelia_after_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
241
242 return $result;
243 }
244 }
245