AddProviderCommand.php
1 year ago
AddProviderCommandHandler.php
3 days ago
GetProviderCommand.php
7 years ago
GetProviderCommandHandler.php
2 months ago
GetProvidersCommand.php
1 year ago
GetProvidersCommandHandler.php
4 months ago
UpdateProviderCommand.php
1 year ago
UpdateProviderCommandHandler.php
3 days ago
UpdateProviderStatusCommand.php
1 year ago
UpdateProviderStatusCommandHandler.php
6 months ago
AddProviderCommandHandler.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User\Provider; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 9 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Entities; |
| 12 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 13 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 14 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\Password; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 18 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 19 | use Interop\Container\Exception\ContainerException; |
| 20 | use Slim\Exception\ContainerValueNotFoundException; |
| 21 | |
| 22 | /** |
| 23 | * Class AddProviderCommandHandler |
| 24 | * |
| 25 | * @package AmeliaBooking\Application\Commands\User\Provider |
| 26 | */ |
| 27 | class AddProviderCommandHandler extends CommandHandler |
| 28 | { |
| 29 | public $mandatoryFields = [ |
| 30 | 'type', |
| 31 | 'firstName', |
| 32 | 'lastName', |
| 33 | 'email' |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * @param AddProviderCommand $command |
| 38 | * |
| 39 | * @return CommandResult |
| 40 | * @throws ContainerValueNotFoundException |
| 41 | * @throws AccessDeniedException |
| 42 | * @throws InvalidArgumentException |
| 43 | * @throws QueryExecutionException |
| 44 | * @throws ContainerException |
| 45 | */ |
| 46 | public function handle(AddProviderCommand $command) |
| 47 | { |
| 48 | if ( |
| 49 | !$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES) || |
| 50 | !$command->getPermissionService()->currentUserCanWriteOthers(Entities::EMPLOYEES) |
| 51 | ) { |
| 52 | throw new AccessDeniedException('You are not allowed to add employee.'); |
| 53 | } |
| 54 | |
| 55 | /** @var AbstractUser $currentUser */ |
| 56 | $currentUser = $this->container->get('logged.in.user'); |
| 57 | |
| 58 | // Only admins may link a new provider to an existing WordPress user account. |
| 59 | $requestedExternalId = $command->getField('externalId'); |
| 60 | if ( |
| 61 | !empty($requestedExternalId) && |
| 62 | (int)$requestedExternalId !== 0 && |
| 63 | (!$currentUser || $currentUser->getType() !== AbstractUser::USER_ROLE_ADMIN) |
| 64 | ) { |
| 65 | throw new AccessDeniedException('You are not allowed to link to an existing WordPress user.'); |
| 66 | } |
| 67 | |
| 68 | /** @var ProviderApplicationService $providerAS */ |
| 69 | $providerAS = $this->container->get('application.user.provider.service'); |
| 70 | |
| 71 | $this->checkMandatoryFields($command); |
| 72 | |
| 73 | $providerData = $command->getFields(); |
| 74 | |
| 75 | $providerData = apply_filters('amelia_before_provider_added_filter', $providerData); |
| 76 | |
| 77 | do_action('amelia_before_provider_added', $providerData); |
| 78 | |
| 79 | $result = $providerAS->createProvider($providerData); |
| 80 | |
| 81 | do_action('amelia_after_provider_added', $result ? $result->getData() : null); |
| 82 | |
| 83 | return $result; |
| 84 | } |
| 85 | } |
| 86 |