AddCustomerCommand.php
7 years ago
AddCustomerCommandHandler.php
2 years ago
GetCustomerCommand.php
7 years ago
GetCustomerCommandHandler.php
2 years ago
GetCustomersCommand.php
7 years ago
GetCustomersCommandHandler.php
2 years ago
UpdateCustomerCommand.php
7 years ago
UpdateCustomerCommandHandler.php
1 year ago
AddCustomerCommandHandler.php
70 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\Domain\Entity\User\AbstractUser; |
| 11 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 12 | use AmeliaBooking\Application\Commands\CommandResult; |
| 13 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 14 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 16 | |
| 17 | /** |
| 18 | * Class AddCustomerCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\User\Customer |
| 21 | */ |
| 22 | class AddCustomerCommandHandler extends CommandHandler |
| 23 | { |
| 24 | |
| 25 | public $mandatoryFields = [ |
| 26 | 'type', |
| 27 | 'firstName', |
| 28 | 'lastName', |
| 29 | 'email' |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * @param AddCustomerCommand $command |
| 34 | * |
| 35 | * @return CommandResult |
| 36 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 37 | * @throws AccessDeniedException |
| 38 | * @throws InvalidArgumentException |
| 39 | * @throws \AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException |
| 40 | * @throws \Interop\Container\Exception\ContainerException |
| 41 | */ |
| 42 | public function handle(AddCustomerCommand $command) |
| 43 | { |
| 44 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 45 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 46 | } |
| 47 | |
| 48 | /** @var CustomerApplicationService $customerAS */ |
| 49 | $customerAS = $this->container->get('application.user.customer.service'); |
| 50 | |
| 51 | $this->checkMandatoryFields($command); |
| 52 | |
| 53 | if ($command->getField('externalId') === -1) { |
| 54 | $command->setField('externalId', null); |
| 55 | } |
| 56 | |
| 57 | $userData = $command->getFields(); |
| 58 | |
| 59 | $userData = apply_filters('amelia_before_customer_added_filter', $userData); |
| 60 | |
| 61 | do_action('amelia_before_customer_added', $userData); |
| 62 | |
| 63 | $response = $customerAS->createCustomer($userData); |
| 64 | |
| 65 | do_action('amelia_after_customer_added', $response? $response->getData() : null); |
| 66 | |
| 67 | return $response; |
| 68 | } |
| 69 | } |
| 70 |