AddCustomerCommand.php
7 years ago
AddCustomerCommandHandler.php
1 year ago
GetCustomerCommand.php
7 years ago
GetCustomerCommandHandler.php
1 year ago
GetCustomersCommand.php
7 years ago
GetCustomersCommandHandler.php
1 year ago
UpdateCustomerCommand.php
7 years ago
UpdateCustomerCommandHandler.php
1 year ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
1 year ago
UpdateCustomerCommandHandler.php
211 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 | $customerData = $command->getFields(); |
| 59 | |
| 60 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 61 | if ($command->getToken()) { |
| 62 | /** @var AbstractUser $provider */ |
| 63 | $provider = $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet'); |
| 64 | |
| 65 | $oldUser = $provider === null |
| 66 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet') |
| 67 | : $userRepository->getById($customerData['id']); |
| 68 | |
| 69 | if ($provider === null && |
| 70 | ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id'))) |
| 71 | ) { |
| 72 | $result->setResult(CommandResult::RESULT_ERROR); |
| 73 | $result->setMessage('Could not retrieve user'); |
| 74 | $result->setData( |
| 75 | [ |
| 76 | 'reauthorize' => true |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | return $result; |
| 81 | } |
| 82 | } else { |
| 83 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 84 | } |
| 85 | } else { |
| 86 | $oldUser = $userRepository->getById($command->getArg('id')); |
| 87 | } |
| 88 | |
| 89 | if ($command->getField('externalId') === -1) { |
| 90 | $command->setField('externalId', null); |
| 91 | } |
| 92 | |
| 93 | /** @var SettingsService $settingsService */ |
| 94 | $settingsService = $this->container->get('domain.settings.service'); |
| 95 | |
| 96 | /** @var AbstractUser $currentUser */ |
| 97 | $currentUser = $this->container->get('logged.in.user'); |
| 98 | |
| 99 | if ($command->getField('email') === '' && |
| 100 | !$settingsService->getSetting('roles', 'allowCustomerDeleteProfile') && |
| 101 | (!$currentUser || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER) |
| 102 | ) { |
| 103 | $result->setResult(CommandResult::RESULT_ERROR); |
| 104 | $result->setMessage('Could not update user.'); |
| 105 | |
| 106 | return $result; |
| 107 | } |
| 108 | |
| 109 | if (!isset($customerData['password'])) { |
| 110 | $customerData['translations'] = !empty($customerData['translations']) ? $customerData['translations'] : null; |
| 111 | |
| 112 | $customerData['birthday'] = !empty($customerData['birthday']) ? $customerData['birthday'] : null; |
| 113 | } |
| 114 | |
| 115 | $newUserData = array_merge($oldUser->toArray(), $customerData); |
| 116 | |
| 117 | $newUserData = apply_filters('amelia_before_customer_updated_filter', $newUserData); |
| 118 | |
| 119 | /** @var Customer $newUser */ |
| 120 | $newUser = UserFactory::create($newUserData); |
| 121 | |
| 122 | if (!($newUser instanceof AbstractUser)) { |
| 123 | $result->setResult(CommandResult::RESULT_ERROR); |
| 124 | $result->setMessage('Could not update user.'); |
| 125 | |
| 126 | return $result; |
| 127 | } |
| 128 | |
| 129 | if ($oldUser && |
| 130 | $userRepository->getByEmail($newUser->getEmail()->getValue()) && |
| 131 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue() |
| 132 | ) { |
| 133 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 134 | $result->setMessage('Email already exist.'); |
| 135 | $result->setData('This email is already in use.'); |
| 136 | |
| 137 | return $result; |
| 138 | } |
| 139 | |
| 140 | if ($command->getField('password')) { |
| 141 | /** @var Password $newPassword */ |
| 142 | $newPassword = new Password($command->getField('password')); |
| 143 | |
| 144 | $userRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password'); |
| 145 | |
| 146 | if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 147 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 148 | wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue()); |
| 149 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | do_action('amelia_before_customer_updated', $newUser? $newUser->toArray() : null); |
| 154 | |
| 155 | if (!$userRepository->update($command->getArg('id'), $newUser)) { |
| 156 | $userRepository->rollback(); |
| 157 | |
| 158 | $result->setResult(CommandResult::RESULT_ERROR); |
| 159 | $result->setMessage('Could not update user.'); |
| 160 | |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | if ($command->getField('externalId') === 0) { |
| 165 | /** @var UserApplicationService $userAS */ |
| 166 | $userAS = $this->getContainer()->get('application.user.service'); |
| 167 | |
| 168 | $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser); |
| 169 | } else if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 170 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 171 | wp_update_user( |
| 172 | [ |
| 173 | 'ID' => $newUser->getExternalId()->getValue(), |
| 174 | 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '', |
| 175 | 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '', |
| 176 | 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : '' |
| 177 | ] |
| 178 | ); |
| 179 | |
| 180 | if ($uid = get_current_user_id()) { |
| 181 | clean_user_cache($uid); |
| 182 | } |
| 183 | |
| 184 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 185 | } |
| 186 | |
| 187 | if ($command->getField('email') === '') { |
| 188 | /** @var CustomerBookingRepository $bookingRepository */ |
| 189 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 190 | |
| 191 | $bookingRepository->updateInfoByCustomerId($oldUser->getId()->getValue(), null); |
| 192 | } |
| 193 | |
| 194 | $userRepository->commit(); |
| 195 | |
| 196 | do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null); |
| 197 | |
| 198 | $result = $userAS->getAuthenticatedUserResponse( |
| 199 | $newUser, |
| 200 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(), |
| 201 | true, |
| 202 | $oldUser->getLoginType(), |
| 203 | 'customer' |
| 204 | ); |
| 205 | |
| 206 | $result->setMessage('Successfully updated user'); |
| 207 | |
| 208 | return $result; |
| 209 | } |
| 210 | } |
| 211 |