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
AddCustomerCommandHandler.php
87 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 | |
| 21 | public $mandatoryFields = [ |
| 22 | 'firstName', |
| 23 | 'lastName', |
| 24 | 'email' |
| 25 | ]; |
| 26 | |
| 27 | /** |
| 28 | * @param AddCustomerCommand $command |
| 29 | * |
| 30 | * @return CommandResult |
| 31 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 32 | * @throws AccessDeniedException |
| 33 | * @throws InvalidArgumentException |
| 34 | * @throws \AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException |
| 35 | * @throws \Interop\Container\Exception\ContainerException |
| 36 | */ |
| 37 | public function handle(AddCustomerCommand $command) |
| 38 | { |
| 39 | /** @var CommandResult $result */ |
| 40 | $result = new CommandResult(); |
| 41 | |
| 42 | /** @var UserApplicationService $userAS */ |
| 43 | $userAS = $this->getContainer()->get('application.user.service'); |
| 44 | |
| 45 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 46 | if ($command->getToken()) { |
| 47 | if ($userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet') === null) { |
| 48 | $result->setResult(CommandResult::RESULT_ERROR); |
| 49 | $result->setMessage('Could not retrieve user'); |
| 50 | $result->setData( |
| 51 | [ |
| 52 | 'reauthorize' => true |
| 53 | ] |
| 54 | ); |
| 55 | |
| 56 | return $result; |
| 57 | } |
| 58 | } else { |
| 59 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** @var CustomerApplicationService $customerAS */ |
| 64 | $customerAS = $this->container->get('application.user.customer.service'); |
| 65 | |
| 66 | $this->checkMandatoryFields($command); |
| 67 | |
| 68 | if ($command->getField('externalId') === -1) { |
| 69 | $command->setField('externalId', null); |
| 70 | } |
| 71 | |
| 72 | $userData = $command->getFields(); |
| 73 | |
| 74 | $userData['type'] = 'customer'; |
| 75 | |
| 76 | $userData = apply_filters('amelia_before_customer_added_filter', $userData); |
| 77 | |
| 78 | do_action('amelia_before_customer_added', $userData); |
| 79 | |
| 80 | $response = $customerAS->createCustomer($userData); |
| 81 | |
| 82 | do_action('amelia_after_customer_added', $response? $response->getData() : null); |
| 83 | |
| 84 | return $response; |
| 85 | } |
| 86 | } |
| 87 |