ameliabooking
/
src
/
Application
/
Commands
/
User
/
Customer
/
UpdateCustomerNoteCommandHandler.php
AddCustomerCommand.php
1 year ago
AddCustomerCommandHandler.php
6 months ago
GetCustomerCommand.php
1 year ago
GetCustomerCommandHandler.php
6 months ago
GetCustomersCommand.php
1 year ago
GetCustomersCommandHandler.php
2 months ago
UpdateCustomerCommand.php
1 year ago
UpdateCustomerCommandHandler.php
1 week ago
UpdateCustomerNoteCommand.php
3 months ago
UpdateCustomerNoteCommandHandler.php
3 months ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
6 months ago
UpdateCustomerNoteCommandHandler.php
102 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\Infrastructure\Common\Exceptions\NotFoundException; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 14 | |
| 15 | /** |
| 16 | * Class UpdateCustomerNoteCommandHandler |
| 17 | * |
| 18 | * @package AmeliaBooking\Application\Commands\User\Customer |
| 19 | */ |
| 20 | class UpdateCustomerNoteCommandHandler extends CommandHandler |
| 21 | { |
| 22 | /** |
| 23 | * @param UpdateCustomerNoteCommand $command |
| 24 | * |
| 25 | * @return CommandResult |
| 26 | * |
| 27 | * @throws InvalidArgumentException |
| 28 | * @throws NotFoundException |
| 29 | * @throws QueryExecutionException |
| 30 | * @throws AccessDeniedException |
| 31 | */ |
| 32 | public function handle(UpdateCustomerNoteCommand $command) |
| 33 | { |
| 34 | $result = new CommandResult(); |
| 35 | |
| 36 | /** @var UserApplicationService $userAS */ |
| 37 | $userAS = $this->getContainer()->get('application.user.service'); |
| 38 | |
| 39 | /** @var UserRepository $userRepository */ |
| 40 | $userRepository = $this->getContainer()->get('domain.users.repository'); |
| 41 | |
| 42 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 43 | if ($command->getToken()) { |
| 44 | $provider = $command->getCabinetType() === 'provider' |
| 45 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet') |
| 46 | : null; |
| 47 | |
| 48 | $oldUser = $provider === null |
| 49 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet') |
| 50 | : $userRepository->getById($command->getArg('id')); |
| 51 | |
| 52 | if ( |
| 53 | $provider === null && |
| 54 | ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id'))) |
| 55 | ) { |
| 56 | $result->setResult(CommandResult::RESULT_ERROR); |
| 57 | $result->setMessage('Could not retrieve user'); |
| 58 | $result->setData(['reauthorize' => true]); |
| 59 | |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | if ($provider !== null && $oldUser === null) { |
| 64 | $result->setResult(CommandResult::RESULT_ERROR); |
| 65 | $result->setMessage('Could not retrieve user'); |
| 66 | |
| 67 | return $result; |
| 68 | } |
| 69 | } else { |
| 70 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 71 | } |
| 72 | } else { |
| 73 | $oldUser = $userRepository->getById($command->getArg('id')); |
| 74 | if ($oldUser === null) { |
| 75 | $result->setResult(CommandResult::RESULT_ERROR); |
| 76 | $result->setMessage('Could not retrieve user'); |
| 77 | return $result; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $customerId = (int)$command->getArg('id'); |
| 82 | $note = $command->getField('note'); |
| 83 | |
| 84 | $userRepository->beginTransaction(); |
| 85 | |
| 86 | try { |
| 87 | $userRepository->updateFieldById($customerId, $note, 'note'); |
| 88 | } catch (QueryExecutionException $e) { |
| 89 | $userRepository->rollback(); |
| 90 | throw $e; |
| 91 | } |
| 92 | |
| 93 | $userRepository->commit(); |
| 94 | |
| 95 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 96 | $result->setMessage('Successfully updated customer note'); |
| 97 | $result->setData([]); |
| 98 | |
| 99 | return $result; |
| 100 | } |
| 101 | } |
| 102 |