DeleteBlockTimeCommand.php
4 months ago
DeleteBlockTimeCommandHandler.php
2 months ago
GetBlockTimeCommand.php
4 months ago
GetBlockTimeCommandHandler.php
2 months ago
GetCalendarEventsCommand.php
7 months ago
GetCalendarEventsCommandHandler.php
4 weeks ago
GetCalendarSlotAvailabilityCommand.php
7 months ago
GetCalendarSlotAvailabilityHandler.php
1 week ago
GetCalendarSlotEntitiesCommand.php
7 months ago
GetCalendarSlotEntitiesCommandHandler.php
3 months ago
GetCalendarSlotsCommand.php
7 months ago
GetCalendarSlotsCommandHandler.php
2 months ago
ManageCalendarBlockTimeCommand.php
4 months ago
ManageCalendarBlockTimeCommandHandler.php
2 months ago
GetCalendarSlotAvailabilityHandler.php
98 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\InvalidArgumentException; |
| 16 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 17 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 18 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 19 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 20 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 21 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 22 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 23 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 24 | |
| 25 | class GetCalendarSlotAvailabilityHandler extends CommandHandler |
| 26 | { |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | public $mandatoryFields = [ |
| 31 | 'bookingStart', |
| 32 | 'appointmentId' |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @param GetCalendarSlotAvailabilityCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws QueryExecutionException |
| 41 | */ |
| 42 | public function handle(GetCalendarSlotAvailabilityCommand $command): CommandResult |
| 43 | { |
| 44 | /** @var AbstractUser $user */ |
| 45 | $user = $command->authorize(); |
| 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 AppointmentRepository $appointmentRepo */ |
| 54 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 55 | /** @var AppointmentApplicationService $appointmentAS */ |
| 56 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 57 | /** @var BookableApplicationService $bookableAS */ |
| 58 | $bookableAS = $this->container->get('application.bookable.service'); |
| 59 | |
| 60 | /** @var Appointment $appointment */ |
| 61 | $appointment = $appointmentRepo->getById($command->getField('appointmentId')); |
| 62 | |
| 63 | /** @var Service $service */ |
| 64 | $service = $bookableAS->getAppointmentService( |
| 65 | $appointment->getServiceId()->getValue(), |
| 66 | $appointment->getProviderId()->getValue() |
| 67 | ); |
| 68 | |
| 69 | $bookingStart = DateTimeService::getDateTimeObjectInTimeZone( |
| 70 | $command->getField('bookingStart'), |
| 71 | $command->getField('timeZone') ?: DateTimeService::getTimeZone()->getName() |
| 72 | ); |
| 73 | |
| 74 | $bookingEnd = (clone $bookingStart)->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second'); |
| 75 | |
| 76 | $bookingStart->setTimezone(DateTimeService::getTimeZone()); |
| 77 | $bookingEnd->setTimezone(DateTimeService::getTimeZone()); |
| 78 | |
| 79 | $appointment->setBookingStart(new DateTimeValue($bookingStart)); |
| 80 | |
| 81 | $appointment->setBookingEnd(new DateTimeValue($bookingEnd)); |
| 82 | |
| 83 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 84 | $result->setResult(CommandResult::RESULT_ERROR); |
| 85 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 86 | $result->setData( |
| 87 | [ |
| 88 | 'timeSlotUnavailable' => true |
| 89 | ] |
| 90 | ); |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | return $result; |
| 96 | } |
| 97 | } |
| 98 |