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