PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 / Booking / Appointment / CancelBookingRemotelyCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 2 months ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 2 months ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 6 months ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 1 month ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 10 months ago DeleteBookingRemotelyCommand.php 10 months ago DeleteBookingRemotelyCommandHandler.php 6 months ago GetAppointmentBookingsCommand.php 6 months ago GetAppointmentBookingsCommandHandler.php 2 months ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 2 weeks ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 2 weeks ago GetIcsCommand.php 6 months ago GetIcsCommandHandler.php 4 years ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 month ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 3 months ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 4 months ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 3 months ago UpdateAppointmentNoteCommand.php 3 months ago UpdateAppointmentNoteCommandHandler.php 3 months ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 2 months ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 1 month ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 5 months ago
CancelBookingRemotelyCommandHandler.php
166 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Appointment;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\Booking\BookingFallbackService;
9 use AmeliaBooking\Application\Services\User\CustomerApplicationService;
10 use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException;
11 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
12 use AmeliaBooking\Domain\Entity\Entities;
13 use AmeliaBooking\Domain\Entity\User\AbstractUser;
14 use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface;
15 use AmeliaBooking\Domain\Services\Settings\SettingsService;
16 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
17 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
18 use AmeliaBooking\Domain\ValueObjects\String\Token;
19 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
20 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
21 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
22 use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings;
23 use Slim\Exception\ContainerException;
24 use Slim\Exception\ContainerValueNotFoundException;
25 use UnexpectedValueException;
26
27 /**
28 * Class CancelBookingRemotelyCommandHandler
29 *
30 * @package AmeliaBooking\Application\Commands\Booking\Appointment
31 */
32 class CancelBookingRemotelyCommandHandler extends CommandHandler
33 {
34 /**
35 * @var array
36 */
37 public $mandatoryFields = [
38 'token',
39 ];
40
41 /**
42 * @param CancelBookingRemotelyCommand $command
43 *
44 * @return CommandResult
45 * @throws UnexpectedValueException
46 * @throws ContainerException
47 * @throws \InvalidArgumentException
48 * @throws ContainerValueNotFoundException
49 * @throws QueryExecutionException
50 * @throws InvalidArgumentException
51 * @throws AccessDeniedException
52 * @throws NotFoundException
53 */
54 public function handle(CancelBookingRemotelyCommand $command)
55 {
56 $this->checkMandatoryFields($command);
57
58 $result = new CommandResult();
59
60 $type = $command->getField('type') ?: Entities::APPOINTMENT;
61
62 /** @var CustomerApplicationService $customerAS */
63 $customerAS = $this->container->get('application.user.customer.service');
64 /** @var CustomerBookingRepository $bookingRepository */
65 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
66
67 /** @var AbstractUser $user */
68 $user = $this->container->get('logged.in.user');
69
70 /** @var CustomerBooking $booking */
71 $booking = $bookingRepository->getById((int)$command->getArg('id'));
72
73 /** @var SettingsService $settingsService */
74 $settingsService = $this->container->get('domain.settings.service');
75
76 $notificationSettings = $settingsService->getCategorySettings('notifications');
77
78 if (!$booking) {
79 if (!empty($notificationSettings['cancelErrorUrl'])) {
80 $result->setUrl($notificationSettings['cancelErrorUrl']);
81 return $result;
82 }
83
84 return $result->setHtml(BookingFallbackService::getFallbackHtml('failed'));
85 }
86
87 $token = $bookingRepository->getToken((int)$command->getArg('id'));
88
89 if (empty($token['token'])) {
90 throw new AccessDeniedException('You are not allowed to update booking status');
91 }
92
93 $booking->setToken(new Token($token['token']));
94
95 if (!$customerAS->isCustomerBooking($booking, $user, $command->getField('token'))) {
96 throw new AccessDeniedException('You are not allowed to update booking status');
97 }
98
99 if ($booking->getStatus()->getValue() === BookingStatus::CANCELED) {
100 if (!empty($command->getField('fromForm'))) {
101 $result->setData(['fromForm' => true]);
102 return $result;
103 }
104
105 if (!empty($notificationSettings['cancelSuccessUrl'])) {
106 $result->setUrl($notificationSettings['cancelSuccessUrl']);
107 return $result;
108 }
109
110 return $result->setHtml(BookingFallbackService::getFallbackHtml('canceled'));
111 }
112
113 /** @var ReservationServiceInterface $reservationService */
114 $reservationService = $this->container->get('application.reservation.service')->get($type);
115
116 $status = BookingStatus::CANCELED;
117
118 do_action('amelia_before_booking_canceled', $booking ? $booking->toArray() : null);
119
120 try {
121 $bookingData = $reservationService->updateStatus($booking, $status);
122
123 $result->setResult(CommandResult::RESULT_SUCCESS);
124 $result->setMessage('Successfully updated booking status');
125 $result->setData(
126 array_merge(
127 $bookingData,
128 [
129 'type' => $type,
130 'status' => $status,
131 'message' => BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($status))
132 ]
133 )
134 );
135 } catch (BookingCancellationException $e) {
136 $result->setResult(CommandResult::RESULT_ERROR);
137 }
138
139 if (!empty($command->getField('fromForm'))) {
140 $result->setData(['fromForm' => true]);
141 return $result;
142 }
143
144 if ($notificationSettings['cancelSuccessUrl'] && $result->getResult() === CommandResult::RESULT_SUCCESS) {
145 $result->setUrl($notificationSettings['cancelSuccessUrl']);
146
147 do_action('amelia_after_booking_canceled', $booking ? $booking->toArray() : null);
148
149 return $result;
150 }
151
152 if ($notificationSettings['cancelErrorUrl'] && $result->getResult() === CommandResult::RESULT_ERROR) {
153 $result->setUrl($notificationSettings['cancelErrorUrl']);
154
155 return $result;
156 }
157
158 // No redirect URL defined - show fallback page
159 if ($result->getResult() === CommandResult::RESULT_SUCCESS) {
160 return $result->setHtml(BookingFallbackService::getFallbackHtml('canceled'));
161 }
162
163 return $result->setHtml(BookingFallbackService::getFallbackHtml('cancel_error'));
164 }
165 }
166