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