GetBlockTimeCommandHandler.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Calendar; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\Schedule\BlockTime; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 18 | use AmeliaBooking\Infrastructure\Repository\Schedule\DayOffRepository; |
| 19 | use AmeliaVendor\Psr\Container\ContainerExceptionInterface; |
| 20 | |
| 21 | /** |
| 22 | * Class GetBlockTimeCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Calendar |
| 25 | */ |
| 26 | class GetBlockTimeCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @param GetBlockTimeCommand $command |
| 30 | * |
| 31 | * @return CommandResult |
| 32 | * @throws AccessDeniedException |
| 33 | * @throws InvalidArgumentException |
| 34 | * @throws QueryExecutionException|ContainerExceptionInterface |
| 35 | */ |
| 36 | public function handle(GetBlockTimeCommand $command): CommandResult |
| 37 | { |
| 38 | $currentUser = $this->container->get('logged.in.user'); |
| 39 | |
| 40 | if ($currentUser === null) { |
| 41 | throw new AccessDeniedException('You are not allowed to read block time'); |
| 42 | } |
| 43 | |
| 44 | if ($currentUser->getType() === Entities::CUSTOMER) { |
| 45 | throw new AccessDeniedException('You are not allowed to read block time'); |
| 46 | } |
| 47 | |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | $this->checkMandatoryFields($command); |
| 51 | |
| 52 | /** @var DayOffRepository $dayOffRepository */ |
| 53 | $dayOffRepository = $this->container->get('domain.schedule.dayOff.repository'); |
| 54 | |
| 55 | /** @var BlockTime $blockTime */ |
| 56 | $blockTime = $dayOffRepository->getBlockTimeById($command->getArg('id')); |
| 57 | |
| 58 | $blockTimeArray = $blockTime->toArray(); |
| 59 | |
| 60 | $canManage = true; |
| 61 | |
| 62 | if ($currentUser->getType() === Entities::PROVIDER) { |
| 63 | $providerId = $currentUser->getId()->getValue(); |
| 64 | $blockTimeUserId = $blockTime->getUserId() ? $blockTime->getUserId()->getValue() : null; |
| 65 | |
| 66 | if ($blockTimeUserId === null || (int)$blockTimeUserId !== (int)$providerId) { |
| 67 | $canManage = false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 72 | $result->setMessage('Successfully retrieved block time.'); |
| 73 | $result->setData([ |
| 74 | 'blockTime' => $blockTimeArray, |
| 75 | 'canManage' => $canManage |
| 76 | ]); |
| 77 | |
| 78 | return $result; |
| 79 | } |
| 80 | } |
| 81 |