ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Event
/
UpdateEventStatusCommandHandler.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
UpdateEventStatusCommandHandler.php
96 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\Translations\BackendStrings; |
| 16 | |
| 17 | /** |
| 18 | * Class UpdateEventStatusCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 21 | */ |
| 22 | class UpdateEventStatusCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @var array |
| 26 | */ |
| 27 | public $mandatoryFields = [ |
| 28 | 'status', |
| 29 | 'applyGlobally' |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * @param UpdateEventStatusCommand $command |
| 34 | * |
| 35 | * @return CommandResult |
| 36 | * @throws \Slim\Exception\ContainerException |
| 37 | * @throws \InvalidArgumentException |
| 38 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 39 | * @throws QueryExecutionException |
| 40 | * @throws InvalidArgumentException |
| 41 | * @throws AccessDeniedException |
| 42 | * @throws \Interop\Container\Exception\ContainerException |
| 43 | */ |
| 44 | public function handle(UpdateEventStatusCommand $command) |
| 45 | { |
| 46 | if (!$command->getPermissionService()->currentUserCanWriteStatus(Entities::EVENTS)) { |
| 47 | throw new AccessDeniedException('You are not allowed to update event status'); |
| 48 | } |
| 49 | |
| 50 | $result = new CommandResult(); |
| 51 | |
| 52 | $this->checkMandatoryFields($command); |
| 53 | |
| 54 | /** @var EventApplicationService $eventApplicationService */ |
| 55 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 56 | |
| 57 | /** @var EventRepository $eventRepository */ |
| 58 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 59 | |
| 60 | $requestedStatus = $command->getField('status'); |
| 61 | |
| 62 | /** @var Event $event */ |
| 63 | $event = $eventRepository->getById((int)$command->getArg('id')); |
| 64 | |
| 65 | $eventRepository->beginTransaction(); |
| 66 | |
| 67 | do_action('amelia_before_event_status_updated', $event ? $event->toArray() : null, $requestedStatus, $command->getField('applyGlobally')); |
| 68 | |
| 69 | try { |
| 70 | /** @var Collection $updatedEvents */ |
| 71 | $updatedEvents = $eventApplicationService->updateStatus( |
| 72 | $event, |
| 73 | $requestedStatus, |
| 74 | $command->getField('applyGlobally') |
| 75 | ); |
| 76 | } catch (QueryExecutionException $e) { |
| 77 | $eventRepository->rollback(); |
| 78 | throw $e; |
| 79 | } |
| 80 | |
| 81 | $eventRepository->commit(); |
| 82 | |
| 83 | do_action('amelia_after_event_status_updated', $event ? $event->toArray() : null, $requestedStatus, $command->getField('applyGlobally')); |
| 84 | |
| 85 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 86 | $result->setMessage('Successfully updated event status'); |
| 87 | $result->setData([ |
| 88 | 'status' => $requestedStatus, |
| 89 | 'message' => BackendStrings::getEventStrings()['event_status_changed'] . $requestedStatus, |
| 90 | Entities::EVENTS => $updatedEvents->toArray(), |
| 91 | ]); |
| 92 | |
| 93 | return $result; |
| 94 | } |
| 95 | } |
| 96 |