ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
DeleteBookingRemotelyCommandHandler.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
DeleteBookingRemotelyCommandHandler.php
85 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\Reservation\AbstractReservationService; |
| 9 | use AmeliaBooking\Domain\Entity\Entities; |
| 10 | use AmeliaBooking\Domain\Entity\Payment\Payment; |
| 11 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 15 | use Slim\Exception\ContainerException; |
| 16 | use Slim\Exception\ContainerValueNotFoundException; |
| 17 | |
| 18 | /** |
| 19 | * Class DeleteBookingRemotelyCommandHandler |
| 20 | * |
| 21 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 22 | */ |
| 23 | class DeleteBookingRemotelyCommandHandler extends CommandHandler |
| 24 | { |
| 25 | /** |
| 26 | * @param DeleteBookingRemotelyCommand $command |
| 27 | * |
| 28 | * @return CommandResult |
| 29 | * @throws InvalidArgumentException |
| 30 | * @throws ContainerValueNotFoundException |
| 31 | * @throws AccessDeniedException |
| 32 | * @throws QueryExecutionException |
| 33 | * @throws ContainerException |
| 34 | * @throws NotFoundException |
| 35 | */ |
| 36 | public function handle(DeleteBookingRemotelyCommand $command) |
| 37 | { |
| 38 | $result = new CommandResult(); |
| 39 | |
| 40 | $type = $command->getField('type') ?: Entities::APPOINTMENT; |
| 41 | |
| 42 | $bookingId = $command->getArg('id'); |
| 43 | |
| 44 | $token = $command->getField('token'); |
| 45 | |
| 46 | if (!$token) { |
| 47 | throw new AccessDeniedException('No token sent.'); |
| 48 | } |
| 49 | |
| 50 | /** @var AbstractReservationService $reservationService */ |
| 51 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 52 | |
| 53 | $booking = $reservationService->getBooking($bookingId); |
| 54 | |
| 55 | if (!$booking->getToken() || $booking->getToken()->getValue() !== $token) { |
| 56 | throw new AccessDeniedException('Invalid token sent.'); |
| 57 | } |
| 58 | |
| 59 | if ($booking->getPayments()->length() > 0 && $booking->getPayments()->getItem($booking->getPayments()->keys()[0])) { |
| 60 | /** @var Payment $payment */ |
| 61 | $payment = $booking->getPayments()->getItem($booking->getPayments()->keys()[0]); |
| 62 | $now = new \DateTime(); |
| 63 | $diffInSeconds = $now->getTimestamp() - |
| 64 | DateTimeService::getCustomDateTimeObjectInUtc($payment->getCreated()->getValue()->format('Y-m-d H:i:s'))->getTimestamp(); |
| 65 | if ($diffInSeconds > 1800) { |
| 66 | throw new AccessDeniedException('Token expired.'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | $reservationService->deleteBooking($bookingId); |
| 72 | } catch (\Exception $e) { |
| 73 | $result->setResult(CommandResult::RESULT_ERROR); |
| 74 | $result->setMessage($e->getMessage()); |
| 75 | |
| 76 | return $result; |
| 77 | } |
| 78 | |
| 79 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 80 | $result->setMessage('Successfully deleted booking'); |
| 81 | |
| 82 | return $result; |
| 83 | } |
| 84 | } |
| 85 |