ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Event
/
DeleteEventBookingCommandHandler.php
Tag
3 months ago
AddEventCommand.php
1 year ago
AddEventCommandHandler.php
1 week ago
DeleteEventBookingCommand.php
7 years ago
DeleteEventBookingCommandHandler.php
1 week ago
DeleteEventCommand.php
1 year ago
DeleteEventCommandHandler.php
7 months ago
DeleteEventsCommand.php
7 months ago
DeleteEventsCommandHandler.php
4 months ago
GenerateEventWpaCustomPostCommand.php
4 weeks ago
GenerateEventWpaCustomPostCommandHandler.php
4 weeks ago
GetCalendarEventsCommand.php
1 year ago
GetCalendarEventsCommandHandler.php
1 week ago
GetEventBookingCommand.php
7 months ago
GetEventBookingCommandHandler.php
1 week ago
GetEventBookingsCommand.php
1 year ago
GetEventBookingsCommandHandler.php
1 week ago
GetEventCommand.php
7 years ago
GetEventCommandHandler.php
1 week ago
GetEventsCommand.php
1 year ago
GetEventsCommandHandler.php
1 week ago
UpdateEventBookingCommand.php
7 years ago
UpdateEventBookingCommandHandler.php
7 months ago
UpdateEventCommand.php
1 year ago
UpdateEventCommandHandler.php
1 week ago
UpdateEventStatusCommand.php
1 year ago
UpdateEventStatusCommandHandler.php
7 months ago
UpdateEventVisibilityCommand.php
7 months ago
UpdateEventVisibilityCommandHandler.php
7 months ago
DeleteEventBookingCommandHandler.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Booking\Event; |
| 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\Booking\BookingApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Booking\EventApplicationService; |
| 10 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 11 | use AmeliaBooking\Domain\Collection\Collection; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 14 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 15 | use AmeliaBooking\Domain\Entity\Entities; |
| 16 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 17 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 18 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 20 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 21 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 22 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 23 | |
| 24 | /** |
| 25 | * Class DeleteEventBookingCommandHandler |
| 26 | * |
| 27 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 28 | */ |
| 29 | class DeleteEventBookingCommandHandler extends CommandHandler |
| 30 | { |
| 31 | /** |
| 32 | * @param DeleteEventBookingCommand $command |
| 33 | * |
| 34 | * @return CommandResult |
| 35 | * @throws AccessDeniedException |
| 36 | * @throws QueryExecutionException |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public function handle(DeleteEventBookingCommand $command) |
| 40 | { |
| 41 | $user = null; |
| 42 | |
| 43 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::EVENTS)) { |
| 44 | /** @var AbstractUser $user */ |
| 45 | $user = $command->authorize(Entities::PROVIDER); |
| 46 | } |
| 47 | |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | /** @var UserApplicationService $userAS */ |
| 51 | $userAS = $this->container->get('application.user.service'); |
| 52 | |
| 53 | /** @var SettingsService $settingsDS */ |
| 54 | $settingsDS = $this->container->get('domain.settings.service'); |
| 55 | |
| 56 | /** @var CustomerBookingRepository $customerBookingRepository */ |
| 57 | $customerBookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 58 | |
| 59 | /** @var EventRepository $eventRepository */ |
| 60 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 61 | |
| 62 | /** @var EventApplicationService $eventApplicationService */ |
| 63 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 64 | |
| 65 | /** @var BookingApplicationService $bookingAS */ |
| 66 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 67 | |
| 68 | /** @var CustomerBooking $customerBooking */ |
| 69 | $customerBooking = $customerBookingRepository->getById((int)$command->getField('id')); |
| 70 | |
| 71 | /** @var Event $event */ |
| 72 | $event = $eventRepository->getByBookingId( |
| 73 | $customerBooking->getId()->getValue(), |
| 74 | [ |
| 75 | 'fetchEventsTags' => true, |
| 76 | 'fetchEventsProviders' => true, |
| 77 | 'fetchBookingsCoupons' => true, |
| 78 | 'fetchBookingsPayments' => true, |
| 79 | 'fetchBookingsUsers' => true, |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | $event->getBookings()->addItem($customerBooking, $customerBooking->getId()->getValue()); |
| 84 | |
| 85 | if ( |
| 86 | $user && |
| 87 | $userAS->isProvider($user) && |
| 88 | ( |
| 89 | !$settingsDS->getSetting('roles', 'allowWriteEvents') || |
| 90 | (!$event->getProviders()->keyExists($user->getId()->getValue()) && |
| 91 | (!$event->getOrganizerId() || $event->getOrganizerId()->getValue() !== $user->getId()->getValue())) |
| 92 | ) |
| 93 | ) { |
| 94 | throw new AccessDeniedException('You are not allowed to delete this booking'); |
| 95 | } |
| 96 | |
| 97 | $customerBookingRepository->beginTransaction(); |
| 98 | |
| 99 | do_action('amelia_before_event_booking_deleted', $customerBooking->toArray(), $event ? $event->toArray() : null); |
| 100 | |
| 101 | if (!$eventApplicationService->deleteEventBooking($customerBooking)) { |
| 102 | $customerBookingRepository->rollback(); |
| 103 | |
| 104 | $result->setResult(CommandResult::RESULT_ERROR); |
| 105 | $result->setMessage('Could not delete booking'); |
| 106 | |
| 107 | return $result; |
| 108 | } |
| 109 | |
| 110 | $customerBookingRepository->commit(); |
| 111 | |
| 112 | $event->setNotifyParticipants( |
| 113 | $bookingAS->isBookingApprovedOrPending($customerBooking->getStatus()->getValue()) |
| 114 | ); |
| 115 | |
| 116 | $customerBooking->setChangedStatus( |
| 117 | new BooleanValueObject( |
| 118 | $bookingAS->isBookingApprovedOrPending($customerBooking->getStatus()->getValue()) |
| 119 | ) |
| 120 | ); |
| 121 | |
| 122 | $customerBooking->setStatus(new BookingStatus(BookingStatus::REJECTED)); |
| 123 | |
| 124 | /** @var Collection $payments */ |
| 125 | $payments = $event->getBookings()->getItem($event->getBookings()->keys()[0])->getPayments(); |
| 126 | |
| 127 | if ($payments && count($payments->getItems())) { |
| 128 | $customerBooking->setPayments($payments); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | do_action('amelia_after_event_booking_deleted', $customerBooking->toArray(), $event->toArray()); |
| 133 | |
| 134 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 135 | $result->setMessage('Successfully deleted event booking'); |
| 136 | $result->setData( |
| 137 | [ |
| 138 | 'type' => Entities::EVENT, |
| 139 | Entities::EVENT => $event->toArray(), |
| 140 | Entities::BOOKING => $customerBooking->toArray(), |
| 141 | 'appointmentStatusChanged' => false |
| 142 | ] |
| 143 | ); |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | } |
| 148 |