PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.4
Booking for Appointments and Events Calendar – Amelia v1.2.4
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 1 year ago AddProviderCommandHandler.php 1 year ago GetProviderCommand.php 1 year 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 1 year ago
UpdateProviderCommandHandler.php
208 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 Interop\Container\Exception\ContainerException;
21 use Slim\Exception\ContainerValueNotFoundException;
22
23 /**
24 * Class UpdateProviderCommandHandler
25 *
26 * @package AmeliaBooking\Application\Commands\User\Provider
27 */
28 class UpdateProviderCommandHandler extends CommandHandler
29 {
30 /**
31 * @param UpdateProviderCommand $command
32 *
33 * @return CommandResult
34 * @throws ContainerValueNotFoundException
35 * @throws AccessDeniedException
36 * @throws InvalidArgumentException
37 * @throws QueryExecutionException
38 * @throws ContainerException
39 */
40 public function handle(UpdateProviderCommand $command)
41 {
42 $result = new CommandResult();
43
44 $this->checkMandatoryFields($command);
45
46 /** @var ProviderRepository $providerRepository */
47 $providerRepository = $this->container->get('domain.users.providers.repository');
48
49 /** @var ProviderApplicationService $providerAS */
50 $providerAS = $this->container->get('application.user.provider.service');
51
52 $userId = (int)$command->getArg('id');
53
54 /** @var AbstractUser $currentUser */
55 $currentUser = $this->container->get('logged.in.user');
56
57 /** @var UserApplicationService $userAS */
58 $userAS = $this->getContainer()->get('application.user.service');
59
60 if (!$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES) ||
61 (
62 !$command->getPermissionService()->currentUserCanWriteOthers(Entities::EMPLOYEES) &&
63 (
64 !$currentUser->getId() ||
65 $currentUser->getId()->getValue() !== $userId
66 )
67 )
68
69 ) {
70 $oldUser = $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet');
71
72 if ($oldUser === null) {
73 $result->setResult(CommandResult::RESULT_ERROR);
74 $result->setMessage('Could not retrieve user');
75 $result->setData(
76 [
77 'reauthorize' => true
78 ]
79 );
80
81 return $result;
82 }
83
84 $oldUser = $providerAS->getProviderWithServicesAndSchedule($oldUser->getId()->getValue());
85 } else {
86 $oldUser = $providerAS->getProviderWithServicesAndSchedule($userId);
87 }
88
89 $command->setField('id', $userId);
90
91 $providerData = $command->getFields();
92
93 if (!isset($providerData['stripeConnect'])) {
94 $providerData['stripeConnect'] = null;
95 }
96
97 /** @var EntityApplicationService $entityService */
98 $entityService = $this->container->get('application.entity.service');
99
100 $entityService->removeMissingEntitiesForProvider($providerData);
101
102 if (!!$oldUser->getBadgeId() && !isset($providerData['badgeId'])) {
103 $providerData['badgeId'] = null;
104 }
105
106 $newUserData = array_merge($oldUser->toArray(), $providerData);
107
108 $newUserData = apply_filters('amelia_before_provider_updated_filter', $newUserData, $oldUser->toArray());
109
110 /** @var Provider $newUser */
111 $newUser = UserFactory::create($newUserData);
112
113 if (!($newUser instanceof AbstractUser)) {
114 $result->setResult(CommandResult::RESULT_ERROR);
115 $result->setMessage('Could not update user.');
116
117 return $result;
118 }
119
120 if ($command->getUserApplicationService()->checkProviderPermissions($currentUser, $command->getToken())) {
121 /** @var SettingsService $settingsDS */
122 $settingsDS = $this->container->get('domain.settings.service');
123
124 $rolesSettings = $settingsDS->getCategorySettings('roles');
125
126 if (!$rolesSettings['allowConfigureServices']) {
127 $newUser->setServiceList($oldUser->getServiceList());
128 }
129
130 if (!$rolesSettings['allowConfigureSchedule']) {
131 $newUser->setWeekDayList($oldUser->getWeekDayList());
132 }
133
134 if (!$rolesSettings['allowConfigureDaysOff']) {
135 $newUser->setDayOffList($oldUser->getDayOffList());
136 }
137
138 if (!$rolesSettings['allowConfigureSpecialDays']) {
139 $newUser->setSpecialDayList($oldUser->getSpecialDayList());
140 }
141 }
142
143 $providerRepository->beginTransaction();
144
145 if ($providerRepository->getByEmail($newUser->getEmail()->getValue()) &&
146 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue()) {
147 $result->setResult(CommandResult::RESULT_CONFLICT);
148 $result->setMessage('Email already exist.');
149 $result->setData('This email is already in use.');
150
151 return $result;
152 }
153
154 if ($command->getField('password')) {
155 $newPassword = new Password($command->getField('password'));
156
157 $providerRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password');
158
159 if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) {
160 wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue());
161 }
162 }
163
164 do_action('amelia_before_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
165
166 try {
167 if (!$providerAS->update($oldUser, $newUser)) {
168 $providerRepository->rollback();
169 return $result;
170 }
171
172 if ($command->getField('externalId') === 0) {
173 /** @var UserApplicationService $userAS */
174 $userAS = $this->getContainer()->get('application.user.service');
175
176 $userAS->setWpUserIdForNewUser($userId, $newUser);
177 }
178 } catch (QueryExecutionException $e) {
179 $providerRepository->rollback();
180 throw $e;
181 }
182
183 $result = $userAS->getAuthenticatedUserResponse(
184 $newUser,
185 $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(),
186 true,
187 $oldUser->getLoginType(),
188 'provider'
189 );
190
191 $result->setData(
192 array_merge(
193 $result->getData(),
194 ['sendEmployeePanelAccessEmail' =>
195 $command->getField('password') && $command->getField('sendEmployeePanelAccessEmail'),
196 'password' => $command->getField('password')
197 ]
198 )
199 );
200
201 $providerRepository->commit();
202
203 do_action('amelia_after_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null);
204
205 return $result;
206 }
207 }
208