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