ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
CancelBookingCommandHandler.php
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
CancelBookingCommandHandler.php
117 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\Domain\Common\Exceptions\AuthorizationException; |
| 9 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 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\ValueObjects\String\BookingStatus; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 20 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 21 | |
| 22 | /** |
| 23 | * Class CancelBookingCommandHandler |
| 24 | * |
| 25 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 26 | */ |
| 27 | class CancelBookingCommandHandler extends CommandHandler |
| 28 | { |
| 29 | /** |
| 30 | * @param CancelBookingCommand $command |
| 31 | * |
| 32 | * @return CommandResult |
| 33 | * |
| 34 | * @throws AccessDeniedException |
| 35 | * @throws InvalidArgumentException |
| 36 | * @throws NotFoundException |
| 37 | * @throws QueryExecutionException |
| 38 | */ |
| 39 | public function handle(CancelBookingCommand $command) |
| 40 | { |
| 41 | $result = new CommandResult(); |
| 42 | |
| 43 | /** @var CustomerBookingRepository $bookingRepository */ |
| 44 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 45 | |
| 46 | /** @var CustomerBooking $booking */ |
| 47 | $booking = $bookingRepository->getById((int)$command->getArg('id')); |
| 48 | |
| 49 | $type = $booking->getAppointmentId() ? Entities::APPOINTMENT : Entities::EVENT; |
| 50 | |
| 51 | /** @var ReservationServiceInterface $reservationService */ |
| 52 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 53 | |
| 54 | try { |
| 55 | /** @var AbstractUser $user */ |
| 56 | $user = $command->getUserApplicationService()->authorization( |
| 57 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 58 | $command->getCabinetType() |
| 59 | ); |
| 60 | } catch (AuthorizationException $e) { |
| 61 | $result->setResult(CommandResult::RESULT_ERROR); |
| 62 | $result->setData( |
| 63 | [ |
| 64 | 'reauthorize' => true, |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | return $result; |
| 69 | } |
| 70 | |
| 71 | $token = $bookingRepository->getToken((int)$command->getArg('id')); |
| 72 | |
| 73 | if (!empty($token['token'])) { |
| 74 | $booking->setToken(new Token($token['token'])); |
| 75 | } |
| 76 | |
| 77 | if (!$command->getUserApplicationService()->isCustomerBooking($booking, $user, null)) { |
| 78 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 79 | } |
| 80 | |
| 81 | do_action('amelia_before_booking_canceled', $booking->toArray()); |
| 82 | |
| 83 | try { |
| 84 | $bookingData = $reservationService->updateStatus($booking, BookingStatus::CANCELED); |
| 85 | } catch (BookingCancellationException $e) { |
| 86 | $result->setResult(CommandResult::RESULT_ERROR); |
| 87 | $result->setMessage('You are not allowed to update booking status'); |
| 88 | $result->setData( |
| 89 | [ |
| 90 | 'cancelBookingUnavailable' => true, |
| 91 | ] |
| 92 | ); |
| 93 | |
| 94 | return $result; |
| 95 | } |
| 96 | |
| 97 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 98 | $result->setMessage('Successfully updated booking status'); |
| 99 | $result->setData( |
| 100 | array_merge( |
| 101 | $bookingData, |
| 102 | [ |
| 103 | 'type' => $type, |
| 104 | 'status' => BookingStatus::CANCELED, |
| 105 | 'message' => |
| 106 | BackendStrings::get('booking_status_changed') . |
| 107 | strtolower(BackendStrings::get(BookingStatus::CANCELED)) |
| 108 | ], |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | do_action('amelia_after_booking_canceled', $bookingData); |
| 113 | |
| 114 | return $result; |
| 115 | } |
| 116 | } |
| 117 |