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