DeleteBlockTimeCommand.php
3 months ago
DeleteBlockTimeCommandHandler.php
3 months ago
GetBlockTimeCommand.php
3 months ago
GetBlockTimeCommandHandler.php
3 months ago
GetCalendarEventsCommand.php
6 months ago
GetCalendarEventsCommandHandler.php
3 months ago
GetCalendarSlotAvailabilityCommand.php
6 months ago
GetCalendarSlotAvailabilityHandler.php
5 months ago
GetCalendarSlotEntitiesCommand.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
2 months ago
GetCalendarSlotsCommand.php
6 months ago
GetCalendarSlotsCommandHandler.php
3 months ago
ManageCalendarBlockTimeCommand.php
3 months ago
ManageCalendarBlockTimeCommandHandler.php
3 months ago
ManageCalendarBlockTimeCommandHandler.php
104 lines
| 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\Domain\Factory\Schedule\BlockTimeFactory; |
| 17 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Schedule\DayOffRepository; |
| 20 | use AmeliaVendor\Psr\Container\ContainerExceptionInterface; |
| 21 | use Exception; |
| 22 | |
| 23 | class ManageCalendarBlockTimeCommandHandler extends CommandHandler |
| 24 | { |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | public $mandatoryFields = [ |
| 29 | 'name', |
| 30 | 'startDateTime', |
| 31 | 'endDateTime', |
| 32 | ]; |
| 33 | |
| 34 | /** |
| 35 | * @param ManageCalendarBlockTimeCommand $command |
| 36 | * @return CommandResult |
| 37 | * @throws AccessDeniedException |
| 38 | * @throws ContainerExceptionInterface |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws QueryExecutionException |
| 41 | */ |
| 42 | public function handle(ManageCalendarBlockTimeCommand $command): CommandResult |
| 43 | { |
| 44 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES)) { |
| 45 | throw new AccessDeniedException('You are not allowed to delete block time'); |
| 46 | } |
| 47 | |
| 48 | $this->checkMandatoryFields($command); |
| 49 | |
| 50 | $result = new CommandResult(); |
| 51 | $fields = $command->getFields(); |
| 52 | |
| 53 | /** @var DayOffRepository $dayOffRepository */ |
| 54 | $dayOffRepository = $this->container->get('domain.schedule.dayOff.repository'); |
| 55 | |
| 56 | $blockTimeId = !empty($fields['id']) ? $fields['id'] : null; |
| 57 | $isUpdate = $blockTimeId !== null; |
| 58 | $employeeIds = !empty($fields['employeeIds']) ? $fields['employeeIds'] : [null]; |
| 59 | |
| 60 | $dayOffRepository->beginTransaction(); |
| 61 | |
| 62 | try { |
| 63 | foreach ($employeeIds as $employeeId) { |
| 64 | $blockTime = $this->createBlockTime($fields, $blockTimeId, $employeeId); |
| 65 | |
| 66 | if ($isUpdate) { |
| 67 | $dayOffRepository->update($blockTime, $blockTimeId); |
| 68 | } else { |
| 69 | $dayOffRepository->add($blockTime, $employeeId); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $dayOffRepository->commit(); |
| 74 | } catch (Exception $e) { |
| 75 | $dayOffRepository->rollback(); |
| 76 | throw $e; |
| 77 | } |
| 78 | |
| 79 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 80 | $result->setMessage('Successfully blocked.'); |
| 81 | $result->setData([]); |
| 82 | |
| 83 | return $result; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param array $fields |
| 88 | * @param $blockTimeId |
| 89 | * @param $employeeId |
| 90 | * @return BlockTime |
| 91 | * @throws InvalidArgumentException |
| 92 | */ |
| 93 | private function createBlockTime(array $fields, $blockTimeId, $employeeId): BlockTime |
| 94 | { |
| 95 | return BlockTimeFactory::create([ |
| 96 | 'id' => $blockTimeId, |
| 97 | 'name' => $fields['name'], |
| 98 | 'userId' => $employeeId, |
| 99 | 'startDate' => DateTimeService::getCustomDateTimeObjectInUtc($fields['startDateTime'])->format('Y-m-d H:i:s'), |
| 100 | 'endDate' => DateTimeService::getCustomDateTimeObjectInUtc($fields['endDateTime'])->format('Y-m-d H:i:s'), |
| 101 | ]); |
| 102 | } |
| 103 | } |
| 104 |