PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.6
Booking for Appointments and Events Calendar – Amelia v2.4.6
2.4.7 2.4.6 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 / GetCalendarSlotAvailabilityHandler.php
ameliabooking / src / Application / Commands / Calendar Last commit date
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