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