ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
DeleteAppointmentCommandHandler.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
DeleteAppointmentCommandHandler.php
110 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\CustomField\AbstractCustomFieldApplicationService; |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 12 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 13 | use AmeliaBooking\Domain\Entity\Entities; |
| 14 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 15 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 18 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 19 | use Slim\Exception\ContainerValueNotFoundException; |
| 20 | use Interop\Container\Exception\ContainerException; |
| 21 | |
| 22 | /** |
| 23 | * Class DeleteAppointmentCommandHandler |
| 24 | * |
| 25 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 26 | */ |
| 27 | class DeleteAppointmentCommandHandler extends CommandHandler |
| 28 | { |
| 29 | /** |
| 30 | * @param DeleteAppointmentCommand $command |
| 31 | * |
| 32 | * @return CommandResult |
| 33 | * @throws InvalidArgumentException |
| 34 | * @throws ContainerValueNotFoundException |
| 35 | * @throws AccessDeniedException |
| 36 | * @throws QueryExecutionException |
| 37 | * @throws ContainerException |
| 38 | */ |
| 39 | public function handle(DeleteAppointmentCommand $command) |
| 40 | { |
| 41 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::APPOINTMENTS)) { |
| 42 | throw new AccessDeniedException('You are not allowed to delete appointment'); |
| 43 | } |
| 44 | |
| 45 | $result = new CommandResult(); |
| 46 | |
| 47 | /** @var AppointmentRepository $appointmentRepository */ |
| 48 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 49 | |
| 50 | /** @var AppointmentApplicationService $appointmentApplicationService */ |
| 51 | $appointmentApplicationService = $this->container->get('application.booking.appointment.service'); |
| 52 | |
| 53 | /** @var AbstractCustomFieldApplicationService $customFieldService */ |
| 54 | $customFieldService = $this->container->get('application.customField.service'); |
| 55 | |
| 56 | /** @var Appointment $appointment */ |
| 57 | $appointment = $appointmentRepository->getById($command->getArg('id')); |
| 58 | |
| 59 | $appointmentRepository->beginTransaction(); |
| 60 | |
| 61 | do_action('amelia_before_appointment_deleted', $appointment ? $appointment->toArray() : null); |
| 62 | |
| 63 | if (!$appointmentApplicationService->delete($appointment)) { |
| 64 | $appointmentRepository->rollback(); |
| 65 | |
| 66 | $result->setResult(CommandResult::RESULT_ERROR); |
| 67 | $result->setMessage('Could not delete appointment'); |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | |
| 72 | // Set status to rejected, to send the notification that appointment is rejected |
| 73 | $appointment->setStatus(new BookingStatus(BookingStatus::REJECTED)); |
| 74 | |
| 75 | $bookingsWithChangedStatus = []; |
| 76 | |
| 77 | /** @var CustomerBooking $customerBooking */ |
| 78 | foreach ($appointment->getBookings()->getItems() as $customerBooking) { |
| 79 | $bookingStatus = $customerBooking->getStatus()->getValue(); |
| 80 | |
| 81 | if ($bookingStatus === BookingStatus::PENDING || $bookingStatus === BookingStatus::APPROVED) { |
| 82 | $customerBooking->setChangedStatus(new BooleanValueObject(true)); |
| 83 | $bookingsWithChangedStatus[] = $customerBooking->toArray(); |
| 84 | } |
| 85 | |
| 86 | $customerBooking->setStatus(new BookingStatus(BookingStatus::REJECTED)); |
| 87 | } |
| 88 | |
| 89 | $appointmentApplicationService->manageDeletionParentRecurringAppointment($appointment->getId()->getValue()); |
| 90 | |
| 91 | $appointmentRepository->commit(); |
| 92 | |
| 93 | $customFieldService->deleteUploadedFilesForDeletedBookings( |
| 94 | new Collection(), |
| 95 | $appointment->getBookings() |
| 96 | ); |
| 97 | |
| 98 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 99 | $result->setMessage('Successfully deleted appointment'); |
| 100 | $result->setData([ |
| 101 | Entities::APPOINTMENT => $appointment->toArray(), |
| 102 | 'bookingsWithChangedStatus' => $bookingsWithChangedStatus |
| 103 | ]); |
| 104 | |
| 105 | do_action('amelia_after_appointment_deleted', $appointment->toArray()); |
| 106 | |
| 107 | return $result; |
| 108 | } |
| 109 | } |
| 110 |