AddEventCommand.php
1 year ago
AddEventCommandHandler.php
1 year ago
DeleteEventBookingCommand.php
7 years ago
DeleteEventBookingCommandHandler.php
1 year ago
DeleteEventCommand.php
1 year ago
DeleteEventCommandHandler.php
6 months ago
DeleteEventsCommand.php
6 months ago
DeleteEventsCommandHandler.php
6 months ago
GetCalendarEventsCommand.php
1 year ago
GetCalendarEventsCommandHandler.php
1 year ago
GetEventBookingCommand.php
6 months ago
GetEventBookingCommandHandler.php
5 months ago
GetEventBookingsCommand.php
1 year ago
GetEventBookingsCommandHandler.php
6 months ago
GetEventCommand.php
7 years ago
GetEventCommandHandler.php
6 months ago
GetEventDeleteEffectCommand.php
1 year ago
GetEventDeleteEffectCommandHandler.php
6 months ago
GetEventsCommand.php
1 year ago
GetEventsCommandHandler.php
6 months ago
UpdateEventBookingCommand.php
7 years ago
UpdateEventBookingCommandHandler.php
6 months ago
UpdateEventCommand.php
1 year ago
UpdateEventCommandHandler.php
6 months ago
UpdateEventStatusCommand.php
1 year ago
UpdateEventStatusCommandHandler.php
6 months ago
UpdateEventVisibilityCommand.php
6 months ago
UpdateEventVisibilityCommandHandler.php
6 months ago
DeleteEventCommandHandler.php
109 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\EventApplicationService; |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 12 | use AmeliaBooking\Domain\Entity\Entities; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 15 | use AmeliaBooking\Infrastructure\WP\EventListeners\Booking\Event\EventStatusUpdatedEventHandler; |
| 16 | |
| 17 | /** |
| 18 | * Class DeleteEventCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 21 | */ |
| 22 | class DeleteEventCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @param DeleteEventCommand $command |
| 26 | * |
| 27 | * @return CommandResult |
| 28 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 29 | * @throws AccessDeniedException |
| 30 | * @throws InvalidArgumentException |
| 31 | * @throws QueryExecutionException |
| 32 | */ |
| 33 | public function handle(DeleteEventCommand $command) |
| 34 | { |
| 35 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::EVENTS)) { |
| 36 | throw new AccessDeniedException('You are not allowed to delete event'); |
| 37 | } |
| 38 | |
| 39 | $result = new CommandResult(); |
| 40 | |
| 41 | $this->checkMandatoryFields($command); |
| 42 | |
| 43 | /** @var EventApplicationService $eventApplicationService */ |
| 44 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 45 | |
| 46 | /** @var EventRepository $eventRepository */ |
| 47 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 48 | |
| 49 | $event = $eventRepository->getById($command->getArg('id')); |
| 50 | |
| 51 | do_action('amelia_before_event_deleted', $event ? $event->toArray() : null); |
| 52 | |
| 53 | $eventRepository->beginTransaction(); |
| 54 | |
| 55 | try { |
| 56 | if ($event->getStatus()->getValue() === 'approved') { |
| 57 | $events = new Collection(); |
| 58 | $events->addItem($event, $event->getId()->getValue()); |
| 59 | /** @var Collection $updatedEvents */ |
| 60 | $updatedEvents = $eventApplicationService->updateStatus( |
| 61 | $events, |
| 62 | 'rejected', |
| 63 | $command->getField('applyGlobally') |
| 64 | ); |
| 65 | |
| 66 | $statusResult = new CommandResult(); |
| 67 | $statusResult->setResult(CommandResult::RESULT_SUCCESS); |
| 68 | $statusResult->setData( |
| 69 | [ |
| 70 | 'status' => 'rejected', |
| 71 | Entities::EVENTS => $updatedEvents->toArray(), |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | EventStatusUpdatedEventHandler::handle($statusResult, $this->container); |
| 76 | } |
| 77 | } catch (QueryExecutionException $e) { |
| 78 | $eventRepository->rollback(); |
| 79 | throw $e; |
| 80 | } |
| 81 | |
| 82 | $eventRepository->commit(); |
| 83 | |
| 84 | $eventRepository->beginTransaction(); |
| 85 | |
| 86 | try { |
| 87 | $deletedEvents = $eventApplicationService->delete($event, $command->getField('applyGlobally')); |
| 88 | } catch (QueryExecutionException $e) { |
| 89 | $eventRepository->rollback(); |
| 90 | throw $e; |
| 91 | } |
| 92 | |
| 93 | $eventRepository->commit(); |
| 94 | |
| 95 | do_action('amelia_after_event_deleted', $event ? $event->toArray() : null); |
| 96 | |
| 97 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 98 | $result->setMessage('Successfully deleted event'); |
| 99 | $result->setData( |
| 100 | [ |
| 101 | Entities::EVENT => $event->toArray(), |
| 102 | 'deletedEvents' => $deletedEvents |
| 103 | ] |
| 104 | ); |
| 105 | |
| 106 | return $result; |
| 107 | } |
| 108 | } |
| 109 |