Customer
1 week ago
Provider
1 week ago
DeleteUserCommand.php
1 year ago
DeleteUserCommandHandler.php
1 year ago
GetCurrentUserCommand.php
1 year ago
GetCurrentUserCommandHandler.php
1 week ago
GetUserDeleteEffectCommand.php
1 year ago
GetUserDeleteEffectCommandHandler.php
7 months ago
GetWPUsersCommand.php
1 year ago
GetWPUsersCommandHandler.php
7 months ago
GetCurrentUserCommandHandler.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Entities; |
| 8 | use AmeliaBooking\Application\Commands\CommandResult; |
| 9 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 10 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 11 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 12 | use AmeliaBooking\Domain\ValueObjects\String\Email; |
| 13 | use Exception; |
| 14 | |
| 15 | /** |
| 16 | * Class GetCurrentUserCommandHandler |
| 17 | * |
| 18 | * @package AmeliaBooking\Application\Commands\User |
| 19 | */ |
| 20 | class GetCurrentUserCommandHandler extends CommandHandler |
| 21 | { |
| 22 | /** |
| 23 | * @param GetCurrentUserCommand $command |
| 24 | * |
| 25 | * @return CommandResult |
| 26 | * @throws InvalidArgumentException |
| 27 | * @throws AccessDeniedException |
| 28 | * @throws Exception |
| 29 | */ |
| 30 | public function handle(GetCurrentUserCommand $command) |
| 31 | { |
| 32 | $result = new CommandResult(); |
| 33 | |
| 34 | $this->checkMandatoryFields($command); |
| 35 | |
| 36 | if ($command->getToken()) { |
| 37 | try { |
| 38 | /** @var AbstractUser $user */ |
| 39 | $user = $command->authorize(AbstractUser::USER_ROLE_CUSTOMER); |
| 40 | } catch (AuthorizationException $e) { |
| 41 | $user = null; |
| 42 | } catch (AccessDeniedException $e) { |
| 43 | $user = null; |
| 44 | } |
| 45 | } else { |
| 46 | /** @var AbstractUser $user */ |
| 47 | $user = $this->getContainer()->get('logged.in.user'); |
| 48 | } |
| 49 | |
| 50 | if ($user && $user->getType() === 'customer' && !!$user->getExternalId() && !$user->getEmail()->getValue()) { |
| 51 | $wpUser = wp_get_current_user(); |
| 52 | $user->setEmail(new Email($wpUser->user_email)); |
| 53 | } |
| 54 | |
| 55 | $userArray = $user ? $user->toArray() : null; |
| 56 | |
| 57 | $userArray = apply_filters('amelia_get_current_user_filter', $userArray); |
| 58 | |
| 59 | do_action('amelia_get_current_user', $userArray); |
| 60 | |
| 61 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 62 | $result->setMessage('Successfully retrieved current user'); |
| 63 | $result->setData( |
| 64 | [ |
| 65 | Entities::USER => $userArray, |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | } |
| 72 |