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
AddEventCommandHandler.php
121 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\Application\Services\Entity\EntityApplicationService; |
| 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\Event\Event; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 16 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 18 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 19 | use Exception; |
| 20 | |
| 21 | /** |
| 22 | * Class AddEventCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 25 | */ |
| 26 | class AddEventCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @var array |
| 30 | */ |
| 31 | public $mandatoryFields = [ |
| 32 | 'name', |
| 33 | 'periods' |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * @param AddEventCommand $command |
| 38 | * |
| 39 | * @return CommandResult |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws Exception |
| 43 | */ |
| 44 | public function handle(AddEventCommand $command) |
| 45 | { |
| 46 | /** @var AbstractUser $user */ |
| 47 | $user = $command->authorize(); |
| 48 | |
| 49 | $result = new CommandResult(); |
| 50 | |
| 51 | $this->checkMandatoryFields($command); |
| 52 | |
| 53 | $eventData = $command->getFields(); |
| 54 | |
| 55 | /** @var EventRepository $eventRepository */ |
| 56 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 57 | /** @var EventApplicationService $eventApplicationService */ |
| 58 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 59 | /** @var UserApplicationService $userAS */ |
| 60 | $userAS = $this->getContainer()->get('application.user.service'); |
| 61 | /** @var SettingsService $settingsDS */ |
| 62 | $settingsDS = $this->container->get('domain.settings.service'); |
| 63 | /** @var EntityApplicationService $entityService */ |
| 64 | $entityService = $this->container->get('application.entity.service'); |
| 65 | |
| 66 | if ( |
| 67 | $userAS->isCustomer($user) || |
| 68 | ($userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')) |
| 69 | ) { |
| 70 | throw new AccessDeniedException('You are not allowed to add an event'); |
| 71 | } |
| 72 | |
| 73 | if ($userAS->isProvider($user) && $userAS->isAmeliaUser($user) && empty($eventData['providers'])) { |
| 74 | $eventData['providers'] = [ |
| 75 | ['id' => $user->getId()->getValue()], |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | $entityService->removeMissingEntitiesForEvent($eventData); |
| 80 | |
| 81 | $eventRepository->beginTransaction(); |
| 82 | |
| 83 | $eventData = apply_filters('amelia_before_event_added_filter', $eventData); |
| 84 | |
| 85 | do_action('amelia_before_event_added', $eventData); |
| 86 | |
| 87 | try { |
| 88 | /** @var Event $event */ |
| 89 | $event = $eventApplicationService->build($eventData); |
| 90 | } catch (Exception $e) { |
| 91 | $result->setResult(CommandResult::RESULT_ERROR); |
| 92 | $result->setMessage($e->getMessage()); |
| 93 | $result->setData(['message' => $e->getMessage()]); |
| 94 | return $result; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | try { |
| 99 | /** @var Collection $events */ |
| 100 | $events = $eventApplicationService->add($event); |
| 101 | } catch (QueryExecutionException $e) { |
| 102 | $eventRepository->rollback(); |
| 103 | throw $e; |
| 104 | } |
| 105 | |
| 106 | $eventRepository->commit(); |
| 107 | |
| 108 | do_action('amelia_after_event_added', $event ? $event->toArray() : null); |
| 109 | |
| 110 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 111 | $result->setMessage('Successfully added new event.'); |
| 112 | $result->setData( |
| 113 | [ |
| 114 | Entities::EVENTS => $events->toArray(), |
| 115 | ] |
| 116 | ); |
| 117 | |
| 118 | return $result; |
| 119 | } |
| 120 | } |
| 121 |