PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.3
Booking for Appointments and Events Calendar – Amelia v2.3
2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Calendar / ManageCalendarBlockTimeCommandHandler.php
ameliabooking / src / Application / Commands / Calendar Last commit date
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