ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
CancelBookingCommandHandler.php
AddAppointmentCommand.php
7 years ago
AddAppointmentCommandHandler.php
1 year ago
AddBookingCommand.php
7 years ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
2 years ago
ApproveBookingRemotelyCommandHandler.php
1 year ago
CancelBookingCommand.php
7 years ago
CancelBookingCommandHandler.php
2 years ago
CancelBookingRemotelyCommand.php
7 years ago
CancelBookingRemotelyCommandHandler.php
2 years ago
DeleteAppointmentCommand.php
7 years ago
DeleteAppointmentCommandHandler.php
2 years ago
DeleteBookingCommand.php
4 years ago
DeleteBookingCommandHandler.php
2 years ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
1 year ago
GetAppointmentsCommand.php
7 years ago
GetAppointmentsCommandHandler.php
1 year ago
GetIcsCommand.php
5 years ago
GetIcsCommandHandler.php
4 years ago
GetPackageAppointmentsCommand.php
1 year ago
GetPackageAppointmentsCommandHandler.php
1 year ago
GetTimeSlotsCommand.php
7 years ago
GetTimeSlotsCommandHandler.php
1 year ago
ReassignBookingCommand.php
5 years ago
ReassignBookingCommandHandler.php
1 year ago
RejectBookingRemotelyCommand.php
2 years ago
RejectBookingRemotelyCommandHandler.php
1 year ago
SuccessfulBookingCommand.php
7 years ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
7 years ago
UpdateAppointmentCommandHandler.php
1 year ago
UpdateAppointmentStatusCommand.php
7 years ago
UpdateAppointmentStatusCommandHandler.php
1 year ago
UpdateAppointmentTimeCommand.php
7 years ago
UpdateAppointmentTimeCommandHandler.php
1 year ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
1 year ago
CancelBookingCommandHandler.php
119 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\Application\Services\User\UserApplicationService; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 16 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 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 Interop\Container\Exception\ContainerException; |
| 24 | |
| 25 | /** |
| 26 | * Class CancelBookingCommandHandler |
| 27 | * |
| 28 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 29 | */ |
| 30 | class CancelBookingCommandHandler extends CommandHandler |
| 31 | { |
| 32 | /** |
| 33 | * @param CancelBookingCommand $command |
| 34 | * |
| 35 | * @return CommandResult |
| 36 | * |
| 37 | * @throws AccessDeniedException |
| 38 | * @throws InvalidArgumentException |
| 39 | * @throws NotFoundException |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws ContainerException |
| 42 | */ |
| 43 | public function handle(CancelBookingCommand $command) |
| 44 | { |
| 45 | $result = new CommandResult(); |
| 46 | |
| 47 | $type = $command->getField('type') ?: Entities::APPOINTMENT; |
| 48 | |
| 49 | /** @var ReservationServiceInterface $reservationService */ |
| 50 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 51 | /** @var CustomerBookingRepository $bookingRepository */ |
| 52 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 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 | /** @var CustomerBooking $booking */ |
| 72 | $booking = $bookingRepository->getById((int)$command->getArg('id')); |
| 73 | |
| 74 | $token = $bookingRepository->getToken((int)$command->getArg('id')); |
| 75 | |
| 76 | if (!empty($token['token'])) { |
| 77 | $booking->setToken(new Token($token['token'])); |
| 78 | } |
| 79 | |
| 80 | if (!$command->getUserApplicationService()->isCustomerBooking($booking, $user, null)) { |
| 81 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 82 | } |
| 83 | |
| 84 | do_action('amelia_before_booking_canceled', $booking ? $booking->toArray() : null); |
| 85 | |
| 86 | try { |
| 87 | $bookingData = $reservationService->updateStatus($booking, BookingStatus::CANCELED); |
| 88 | } catch (BookingCancellationException $e) { |
| 89 | $result->setResult(CommandResult::RESULT_ERROR); |
| 90 | $result->setMessage('You are not allowed to update booking status'); |
| 91 | $result->setData( |
| 92 | [ |
| 93 | 'cancelBookingUnavailable' => true |
| 94 | ] |
| 95 | ); |
| 96 | |
| 97 | return $result; |
| 98 | } |
| 99 | |
| 100 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 101 | $result->setMessage('Successfully updated booking status'); |
| 102 | $result->setData( |
| 103 | array_merge( |
| 104 | $bookingData, |
| 105 | [ |
| 106 | 'type' => $type, |
| 107 | 'status' => BookingStatus::CANCELED, |
| 108 | 'message' => |
| 109 | BackendStrings::getAppointmentStrings()['appointment_status_changed'] . strtolower(BackendStrings::getCommonStrings()[BookingStatus::CANCELED]) |
| 110 | ] |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | do_action('amelia_after_booking_canceled', $bookingData); |
| 115 | |
| 116 | return $result; |
| 117 | } |
| 118 | } |
| 119 |