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
226 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 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 161 | wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue()); |
| 162 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | do_action('amelia_before_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null); |
| 167 | |
| 168 | try { |
| 169 | if (!$providerAS->update($oldUser, $newUser)) { |
| 170 | $providerRepository->rollback(); |
| 171 | return $result; |
| 172 | } |
| 173 | |
| 174 | if ($command->getField('externalId') === 0) { |
| 175 | /** @var UserApplicationService $userAS */ |
| 176 | $userAS = $this->getContainer()->get('application.user.service'); |
| 177 | |
| 178 | $userAS->setWpUserIdForNewUser($userId, $newUser, $command->getField('password')); |
| 179 | } else if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 180 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 181 | wp_update_user( |
| 182 | [ |
| 183 | 'ID' => $newUser->getExternalId()->getValue(), |
| 184 | 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '', |
| 185 | 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '', |
| 186 | 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : '' |
| 187 | ] |
| 188 | ); |
| 189 | |
| 190 | if ($uid = get_current_user_id()) { |
| 191 | clean_user_cache($uid); |
| 192 | } |
| 193 | |
| 194 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 195 | } |
| 196 | } catch (QueryExecutionException $e) { |
| 197 | $providerRepository->rollback(); |
| 198 | throw $e; |
| 199 | } |
| 200 | |
| 201 | $result = $userAS->getAuthenticatedUserResponse( |
| 202 | $newUser, |
| 203 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(), |
| 204 | true, |
| 205 | $oldUser->getLoginType(), |
| 206 | 'provider' |
| 207 | ); |
| 208 | |
| 209 | $result->setData( |
| 210 | array_merge( |
| 211 | $result->getData(), |
| 212 | ['sendEmployeePanelAccessEmail' => |
| 213 | $command->getField('password') && $command->getField('sendEmployeePanelAccessEmail'), |
| 214 | 'password' => $command->getField('password') |
| 215 | ] |
| 216 | ) |
| 217 | ); |
| 218 | |
| 219 | $providerRepository->commit(); |
| 220 | |
| 221 | do_action('amelia_after_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null); |
| 222 | |
| 223 | return $result; |
| 224 | } |
| 225 | } |
| 226 |