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
6 months ago
UpdateCustomerCommand.php
1 year ago
UpdateCustomerCommandHandler.php
5 months ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
6 months ago
AddCustomerCommandHandler.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User\Customer; |
| 4 | |
| 5 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 6 | use AmeliaBooking\Application\Services\User\CustomerApplicationService; |
| 7 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Entities; |
| 10 | use AmeliaBooking\Application\Commands\CommandResult; |
| 11 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 12 | |
| 13 | /** |
| 14 | * Class AddCustomerCommandHandler |
| 15 | * |
| 16 | * @package AmeliaBooking\Application\Commands\User\Customer |
| 17 | */ |
| 18 | class AddCustomerCommandHandler extends CommandHandler |
| 19 | { |
| 20 | public $mandatoryFields = [ |
| 21 | 'firstName', |
| 22 | 'lastName', |
| 23 | 'email' |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * @param AddCustomerCommand $command |
| 28 | * |
| 29 | * @return CommandResult |
| 30 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 31 | * @throws AccessDeniedException |
| 32 | * @throws InvalidArgumentException |
| 33 | * @throws \AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException |
| 34 | */ |
| 35 | public function handle(AddCustomerCommand $command) |
| 36 | { |
| 37 | /** @var CommandResult $result */ |
| 38 | $result = new CommandResult(); |
| 39 | |
| 40 | /** @var UserApplicationService $userAS */ |
| 41 | $userAS = $this->getContainer()->get('application.user.service'); |
| 42 | |
| 43 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 44 | if ($command->getToken()) { |
| 45 | if ($userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet') === null) { |
| 46 | $result->setResult(CommandResult::RESULT_ERROR); |
| 47 | $result->setMessage('Could not retrieve user'); |
| 48 | $result->setData( |
| 49 | [ |
| 50 | 'reauthorize' => true |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | return $result; |
| 55 | } |
| 56 | } else { |
| 57 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** @var CustomerApplicationService $customerAS */ |
| 62 | $customerAS = $this->container->get('application.user.customer.service'); |
| 63 | |
| 64 | $this->checkMandatoryFields($command); |
| 65 | |
| 66 | if ($command->getField('externalId') === -1) { |
| 67 | $command->setField('externalId', null); |
| 68 | } |
| 69 | |
| 70 | $userData = $command->getFields(); |
| 71 | |
| 72 | $userData['type'] = 'customer'; |
| 73 | |
| 74 | $userData = apply_filters('amelia_before_customer_added_filter', $userData); |
| 75 | |
| 76 | do_action('amelia_before_customer_added', $userData); |
| 77 | |
| 78 | $response = $customerAS->createCustomer($userData); |
| 79 | |
| 80 | do_action('amelia_after_customer_added', $response ? $response->getData() : null); |
| 81 | |
| 82 | return $response; |
| 83 | } |
| 84 | } |
| 85 |