ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
CancelBookingRemotelyCommandHandler.php
AddAppointmentCommand.php
1 year ago
AddAppointmentCommandHandler.php
6 months ago
AddBookingCommand.php
1 year ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
1 year ago
ApproveBookingRemotelyCommandHandler.php
6 months ago
CancelBookingCommand.php
1 year ago
CancelBookingCommandHandler.php
6 months ago
CancelBookingRemotelyCommand.php
1 year ago
CancelBookingRemotelyCommandHandler.php
6 months 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
6 months ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
5 months ago
GetAppointmentsCommand.php
1 year ago
GetAppointmentsCommandHandler.php
6 months ago
GetIcsCommand.php
6 months ago
GetIcsCommandHandler.php
4 years ago
GetPackageAppointmentsCommand.php
1 year ago
GetPackageAppointmentsCommandHandler.php
6 months ago
GetTimeSlotsCommand.php
1 year ago
GetTimeSlotsCommandHandler.php
10 months ago
ReassignBookingCommand.php
1 year ago
ReassignBookingCommandHandler.php
10 months ago
RejectBookingRemotelyCommand.php
1 year ago
RejectBookingRemotelyCommandHandler.php
6 months ago
SuccessfulBookingCommand.php
1 year ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
1 year ago
UpdateAppointmentCommandHandler.php
6 months ago
UpdateAppointmentStatusCommand.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
5 months ago
UpdateAppointmentTimeCommand.php
1 year ago
UpdateAppointmentTimeCommandHandler.php
10 months ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
5 months ago
CancelBookingRemotelyCommandHandler.php
138 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\User\CustomerApplicationService; |
| 9 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 10 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 11 | use AmeliaBooking\Domain\Entity\Entities; |
| 12 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 13 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 14 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 15 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 17 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 21 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 22 | use Slim\Exception\ContainerException; |
| 23 | use Slim\Exception\ContainerValueNotFoundException; |
| 24 | use UnexpectedValueException; |
| 25 | |
| 26 | /** |
| 27 | * Class CancelBookingRemotelyCommandHandler |
| 28 | * |
| 29 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 30 | */ |
| 31 | class CancelBookingRemotelyCommandHandler extends CommandHandler |
| 32 | { |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | public $mandatoryFields = [ |
| 37 | 'token', |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * @param CancelBookingRemotelyCommand $command |
| 42 | * |
| 43 | * @return CommandResult |
| 44 | * @throws UnexpectedValueException |
| 45 | * @throws ContainerException |
| 46 | * @throws \InvalidArgumentException |
| 47 | * @throws ContainerValueNotFoundException |
| 48 | * @throws QueryExecutionException |
| 49 | * @throws InvalidArgumentException |
| 50 | * @throws AccessDeniedException |
| 51 | * @throws NotFoundException |
| 52 | */ |
| 53 | public function handle(CancelBookingRemotelyCommand $command) |
| 54 | { |
| 55 | $this->checkMandatoryFields($command); |
| 56 | |
| 57 | $result = new CommandResult(); |
| 58 | |
| 59 | $type = $command->getField('type') ?: Entities::APPOINTMENT; |
| 60 | |
| 61 | /** @var CustomerApplicationService $customerAS */ |
| 62 | $customerAS = $this->container->get('application.user.customer.service'); |
| 63 | /** @var CustomerBookingRepository $bookingRepository */ |
| 64 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 65 | |
| 66 | /** @var AbstractUser $user */ |
| 67 | $user = $this->container->get('logged.in.user'); |
| 68 | |
| 69 | /** @var CustomerBooking $booking */ |
| 70 | $booking = $bookingRepository->getById((int)$command->getArg('id')); |
| 71 | |
| 72 | if (!$booking) { |
| 73 | $result->setData(['message' => "Booking doesn't exists"]); |
| 74 | return $result; |
| 75 | } |
| 76 | |
| 77 | $token = $bookingRepository->getToken((int)$command->getArg('id')); |
| 78 | |
| 79 | if (empty($token['token'])) { |
| 80 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 81 | } |
| 82 | |
| 83 | $booking->setToken(new Token($token['token'])); |
| 84 | |
| 85 | if (!$customerAS->isCustomerBooking($booking, $user, $command->getField('token'))) { |
| 86 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 87 | } |
| 88 | |
| 89 | /** @var ReservationServiceInterface $reservationService */ |
| 90 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 91 | |
| 92 | /** @var SettingsService $settingsService */ |
| 93 | $settingsService = $this->container->get('domain.settings.service'); |
| 94 | |
| 95 | $status = BookingStatus::CANCELED; |
| 96 | |
| 97 | do_action('amelia_before_booking_canceled', $booking ? $booking->toArray() : null); |
| 98 | |
| 99 | try { |
| 100 | $bookingData = $reservationService->updateStatus($booking, $status); |
| 101 | |
| 102 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 103 | $result->setMessage('Successfully updated booking status'); |
| 104 | $result->setData( |
| 105 | array_merge( |
| 106 | $bookingData, |
| 107 | [ |
| 108 | 'type' => $type, |
| 109 | 'status' => $status, |
| 110 | 'message' => BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($status)) |
| 111 | ] |
| 112 | ) |
| 113 | ); |
| 114 | } catch (BookingCancellationException $e) { |
| 115 | $result->setResult(CommandResult::RESULT_ERROR); |
| 116 | } |
| 117 | |
| 118 | $notificationSettings = $settingsService->getCategorySettings('notifications'); |
| 119 | |
| 120 | if (!empty($command->getField('fromForm'))) { |
| 121 | $result->setData(['fromForm' => true]); |
| 122 | return $result; |
| 123 | } |
| 124 | |
| 125 | if ($notificationSettings['cancelSuccessUrl'] && $result->getResult() === CommandResult::RESULT_SUCCESS) { |
| 126 | $result->setUrl($notificationSettings['cancelSuccessUrl']); |
| 127 | |
| 128 | do_action('amelia_after_booking_canceled', $booking ? $booking->toArray() : null); |
| 129 | } elseif ($notificationSettings['cancelErrorUrl'] && $result->getResult() === CommandResult::RESULT_ERROR) { |
| 130 | $result->setUrl($notificationSettings['cancelErrorUrl']); |
| 131 | } else { |
| 132 | $result->setUrl('/'); |
| 133 | } |
| 134 | |
| 135 | return $result; |
| 136 | } |
| 137 | } |
| 138 |