DeleteBlockTimeCommand.php
3 months ago
DeleteBlockTimeCommandHandler.php
1 month ago
GetBlockTimeCommand.php
3 months ago
GetBlockTimeCommandHandler.php
1 month ago
GetCalendarEventsCommand.php
6 months ago
GetCalendarEventsCommandHandler.php
2 weeks ago
GetCalendarSlotAvailabilityCommand.php
6 months ago
GetCalendarSlotAvailabilityHandler.php
4 months ago
GetCalendarSlotEntitiesCommand.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
2 months ago
GetCalendarSlotsCommand.php
6 months ago
GetCalendarSlotsCommandHandler.php
1 month ago
ManageCalendarBlockTimeCommand.php
3 months ago
ManageCalendarBlockTimeCommandHandler.php
1 month ago
GetCalendarSlotAvailabilityHandler.php
117 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\Services\Bookable\BookableApplicationService; |
| 13 | use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService; |
| 14 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 15 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 16 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 17 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 18 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 19 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 20 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 21 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 22 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 23 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 24 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 25 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 26 | use Interop\Container\Exception\ContainerException; |
| 27 | use Psr\Container\ContainerExceptionInterface; |
| 28 | |
| 29 | class GetCalendarSlotAvailabilityHandler extends CommandHandler |
| 30 | { |
| 31 | /** |
| 32 | * @var array |
| 33 | */ |
| 34 | public $mandatoryFields = [ |
| 35 | 'bookingStart', |
| 36 | 'appointmentId' |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * @throws ContainerException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws ContainerExceptionInterface |
| 43 | * @throws QueryExecutionException |
| 44 | */ |
| 45 | public function handle(GetCalendarSlotAvailabilityCommand $command): CommandResult |
| 46 | { |
| 47 | $this->checkMandatoryFields($command); |
| 48 | |
| 49 | $result = new CommandResult(); |
| 50 | |
| 51 | /** @var UserApplicationService $userAS */ |
| 52 | $userAS = $this->container->get('application.user.service'); |
| 53 | /** @var SettingsService $settingsDS */ |
| 54 | $settingsDS = $this->container->get('domain.settings.service'); |
| 55 | /** @var AppointmentRepository $appointmentRepo */ |
| 56 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 57 | /** @var AppointmentApplicationService $appointmentAS */ |
| 58 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 59 | /** @var BookableApplicationService $bookableAS */ |
| 60 | $bookableAS = $this->container->get('application.bookable.service'); |
| 61 | |
| 62 | try { |
| 63 | /** @var AbstractUser $user */ |
| 64 | $user = $command->getUserApplicationService()->authorization( |
| 65 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 66 | $command->getCabinetType() |
| 67 | ); |
| 68 | } catch (AuthorizationException $e) { |
| 69 | $result->setResult(CommandResult::RESULT_ERROR); |
| 70 | $result->setData( |
| 71 | [ |
| 72 | 'reauthorize' => true |
| 73 | ] |
| 74 | ); |
| 75 | |
| 76 | return $result; |
| 77 | } |
| 78 | |
| 79 | /** @var Appointment $appointment */ |
| 80 | $appointment = $appointmentRepo->getById($command->getField('appointmentId')); |
| 81 | |
| 82 | /** @var Service $service */ |
| 83 | $service = $bookableAS->getAppointmentService( |
| 84 | $appointment->getServiceId()->getValue(), |
| 85 | $appointment->getProviderId()->getValue() |
| 86 | ); |
| 87 | |
| 88 | $bookingStart = DateTimeService::getDateTimeObjectInTimeZone( |
| 89 | $command->getField('bookingStart'), |
| 90 | $command->getField('timeZone') ?: DateTimeService::getTimeZone()->getName() |
| 91 | ); |
| 92 | |
| 93 | $bookingEnd = (clone $bookingStart)->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second'); |
| 94 | |
| 95 | $bookingStart->setTimezone(DateTimeService::getTimeZone()); |
| 96 | $bookingEnd->setTimezone(DateTimeService::getTimeZone()); |
| 97 | |
| 98 | $appointment->setBookingStart(new DateTimeValue($bookingStart)); |
| 99 | |
| 100 | $appointment->setBookingEnd(new DateTimeValue($bookingEnd)); |
| 101 | |
| 102 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 103 | $result->setResult(CommandResult::RESULT_ERROR); |
| 104 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 105 | $result->setData( |
| 106 | [ |
| 107 | 'timeSlotUnavailable' => true |
| 108 | ] |
| 109 | ); |
| 110 | |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | return $result; |
| 115 | } |
| 116 | } |
| 117 |