ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
RejectBookingRemotelyCommandHandler.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
RejectBookingRemotelyCommandHandler.php
137 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 RejectBookingRemotelyCommandHandler |
| 28 | * |
| 29 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 30 | */ |
| 31 | class RejectBookingRemotelyCommandHandler extends CommandHandler |
| 32 | { |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | public $mandatoryFields = [ |
| 37 | 'token', |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * @param RejectBookingRemotelyCommand $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 \Interop\Container\Exception\ContainerException |
| 52 | * @throws NotFoundException |
| 53 | */ |
| 54 | public function handle(RejectBookingRemotelyCommand $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 === null) { |
| 79 | $result->setUrl($notificationSettings['rejectErrorUrl']); |
| 80 | $result->setMessage('This booking does not exist!'); |
| 81 | return $result; |
| 82 | } |
| 83 | |
| 84 | $token = $bookingRepository->getToken((int)$command->getArg('id')); |
| 85 | |
| 86 | if (empty($token['token'])) { |
| 87 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 88 | } |
| 89 | |
| 90 | $booking->setToken(new Token($token['token'])); |
| 91 | |
| 92 | if (!$customerAS->isCustomerBooking($booking, $user, $command->getField('token'))) { |
| 93 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 94 | } |
| 95 | |
| 96 | /** @var ReservationServiceInterface $reservationService */ |
| 97 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 98 | |
| 99 | $status = BookingStatus::REJECTED; |
| 100 | |
| 101 | do_action('amelia_before_booking_rejected_link', $booking ? $booking->toArray() : null); |
| 102 | |
| 103 | try { |
| 104 | $bookingData = $reservationService->updateStatus($booking, $status); |
| 105 | |
| 106 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 107 | $result->setMessage('Successfully updated booking status'); |
| 108 | $result->setData( |
| 109 | array_merge( |
| 110 | $bookingData, |
| 111 | [ |
| 112 | 'type' => $type, |
| 113 | 'status' => $status, |
| 114 | 'message' => BackendStrings::getAppointmentStrings()['appointment_status_changed'] . strtolower(BackendStrings::getCommonStrings()[$status]) |
| 115 | ] |
| 116 | ) |
| 117 | ); |
| 118 | } catch (BookingCancellationException $e) { |
| 119 | $result->setResult(CommandResult::RESULT_ERROR); |
| 120 | } |
| 121 | |
| 122 | $notificationSettings = $settingsService->getCategorySettings('notifications'); |
| 123 | |
| 124 | if ($notificationSettings['rejectSuccessUrl'] && $result->getResult() === CommandResult::RESULT_SUCCESS) { |
| 125 | $result->setUrl($notificationSettings['rejectSuccessUrl']); |
| 126 | |
| 127 | do_action('amelia_after_booking_rejected_link', $booking ? $booking->toArray() : null); |
| 128 | } elseif ($notificationSettings['rejectErrorUrl'] && $result->getResult() === CommandResult::RESULT_ERROR) { |
| 129 | $result->setUrl($notificationSettings['rejectErrorUrl']); |
| 130 | } else { |
| 131 | $result->setUrl('/'); |
| 132 | } |
| 133 | |
| 134 | return $result; |
| 135 | } |
| 136 | } |
| 137 |