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
UpdateEventCommandHandler.php
261 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\Application\Services\Zoom\AbstractZoomApplicationService; |
| 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\Booking\Event\EventPeriod; |
| 16 | use AmeliaBooking\Domain\Entity\Entities; |
| 17 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 18 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 21 | use Exception; |
| 22 | use Interop\Container\Exception\ContainerException; |
| 23 | use Slim\Exception\ContainerValueNotFoundException; |
| 24 | |
| 25 | /** |
| 26 | * Class UpdateEventCommandHandler |
| 27 | * |
| 28 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 29 | */ |
| 30 | class UpdateEventCommandHandler extends CommandHandler |
| 31 | { |
| 32 | /** |
| 33 | * @var array |
| 34 | */ |
| 35 | public $mandatoryFields = [ |
| 36 | 'id', |
| 37 | 'name', |
| 38 | 'periods', |
| 39 | 'applyGlobally' |
| 40 | ]; |
| 41 | |
| 42 | /** |
| 43 | * @param UpdateEventCommand $command |
| 44 | * |
| 45 | * @return CommandResult |
| 46 | * @throws ContainerValueNotFoundException |
| 47 | * @throws QueryExecutionException |
| 48 | * @throws InvalidArgumentException |
| 49 | * @throws AccessDeniedException |
| 50 | * @throws ContainerException |
| 51 | * @throws Exception |
| 52 | */ |
| 53 | public function handle(UpdateEventCommand $command) |
| 54 | { |
| 55 | $result = new CommandResult(); |
| 56 | |
| 57 | $this->checkMandatoryFields($command); |
| 58 | |
| 59 | $eventData = $command->getFields(); |
| 60 | |
| 61 | /** @var EventRepository $eventRepository */ |
| 62 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 63 | /** @var EventApplicationService $eventApplicationService */ |
| 64 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 65 | /** @var UserApplicationService $userAS */ |
| 66 | $userAS = $this->getContainer()->get('application.user.service'); |
| 67 | /** @var SettingsService $settingsDS */ |
| 68 | $settingsDS = $this->container->get('domain.settings.service'); |
| 69 | /** @var EntityApplicationService $entityService */ |
| 70 | $entityService = $this->container->get('application.entity.service'); |
| 71 | |
| 72 | try { |
| 73 | /** @var AbstractUser $user */ |
| 74 | $user = $command->getUserApplicationService()->authorization( |
| 75 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 76 | $command->getCabinetType() |
| 77 | ); |
| 78 | } catch (AuthorizationException $e) { |
| 79 | $result->setResult(CommandResult::RESULT_ERROR); |
| 80 | $result->setData( |
| 81 | [ |
| 82 | 'reauthorize' => true |
| 83 | ] |
| 84 | ); |
| 85 | |
| 86 | return $result; |
| 87 | } |
| 88 | |
| 89 | if ($userAS->isCustomer($user) || |
| 90 | ( |
| 91 | $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents') |
| 92 | ) |
| 93 | ) { |
| 94 | throw new AccessDeniedException('You are not allowed to update an event'); |
| 95 | } |
| 96 | |
| 97 | $entityService->removeMissingEntitiesForEvent($eventData); |
| 98 | |
| 99 | |
| 100 | /** @var Event $event */ |
| 101 | $oldEvent = $eventApplicationService->getEventById( |
| 102 | $eventData['id'], |
| 103 | [ |
| 104 | 'fetchEventsPeriods' => true, |
| 105 | 'fetchEventsTickets' => true, |
| 106 | 'fetchEventsTags' => true, |
| 107 | 'fetchEventsProviders' => true, |
| 108 | 'fetchEventsImages' => true, |
| 109 | 'fetchBookings' => true, |
| 110 | 'fetchBookingsTickets' => true, |
| 111 | 'fetchBookingsUsers' => true, |
| 112 | 'fetchBookingsPayments' => true, |
| 113 | ] |
| 114 | ); |
| 115 | |
| 116 | $eventData = apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally')); |
| 117 | |
| 118 | do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally')); |
| 119 | |
| 120 | try { |
| 121 | /** @var Event $event */ |
| 122 | $event = $eventApplicationService->build($eventData); |
| 123 | } catch (Exception $e) { |
| 124 | $result->setResult(CommandResult::RESULT_ERROR); |
| 125 | $result->setMessage($e->getMessage()); |
| 126 | $result->setData(['message' => $e->getMessage()]); |
| 127 | return $result; |
| 128 | } |
| 129 | |
| 130 | if ($oldEvent->getRecurring() && |
| 131 | $event->getRecurring() && |
| 132 | ( |
| 133 | $event->getRecurring()->getUntil()->getValue() < $oldEvent->getRecurring()->getUntil()->getValue() || |
| 134 | $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue() |
| 135 | ) |
| 136 | ) { |
| 137 | $result->setResult(CommandResult::RESULT_ERROR); |
| 138 | $result->setMessage('Could not update event'); |
| 139 | |
| 140 | return $result; |
| 141 | } |
| 142 | |
| 143 | $event->setBookings($oldEvent->getBookings()); |
| 144 | |
| 145 | /** @var EventPeriod $oldEventPeriod */ |
| 146 | foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) { |
| 147 | /** @var EventPeriod $eventPeriod */ |
| 148 | foreach ($event->getPeriods()->getItems() as $eventPeriod) { |
| 149 | if ($eventPeriod->getId() && |
| 150 | $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue() |
| 151 | ) { |
| 152 | if ($oldEventPeriod->getZoomMeeting()) { |
| 153 | $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting()); |
| 154 | } |
| 155 | if ($oldEventPeriod->getLessonSpace()) { |
| 156 | $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace()); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $eventRepository->beginTransaction(); |
| 163 | |
| 164 | try { |
| 165 | $parsedEvents = $eventApplicationService->update( |
| 166 | $oldEvent, |
| 167 | $event, |
| 168 | $command->getField('applyGlobally') |
| 169 | ); |
| 170 | } catch (QueryExecutionException $e) { |
| 171 | $eventRepository->rollback(); |
| 172 | throw $e; |
| 173 | } |
| 174 | |
| 175 | $eventRepository->commit(); |
| 176 | |
| 177 | do_action('amelia_after_event_updated', $event ? $event->toArray() : null, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally')); |
| 178 | |
| 179 | $providersRemoved = array_udiff( |
| 180 | $oldEvent->getProviders()->getItems(), |
| 181 | $event->getProviders()->getItems(), |
| 182 | function ($a, $b) { |
| 183 | if ($a->getId()->getValue() == $b->getId()->getValue()) { |
| 184 | return 0; |
| 185 | } else { |
| 186 | return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1); |
| 187 | } |
| 188 | } |
| 189 | ); |
| 190 | |
| 191 | $providersAdded = array_udiff( |
| 192 | $event->getProviders()->getItems(), |
| 193 | $oldEvent->getProviders()->getItems(), |
| 194 | function ($a, $b) { |
| 195 | if ($a->getId()->getValue() == $b->getId()->getValue()) { |
| 196 | return 0; |
| 197 | } else { |
| 198 | return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1); |
| 199 | } |
| 200 | } |
| 201 | ); |
| 202 | |
| 203 | $newInfo = ($event->getDescription() ? $event->getDescription()->getValue() : null) !== |
| 204 | ($oldEvent->getDescription() ? $oldEvent->getDescription()->getValue() : null) || |
| 205 | $event->getName()->getValue() !== $oldEvent->getName()->getValue(); |
| 206 | |
| 207 | $zoomUserChanged = ($event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null) !== |
| 208 | ($oldEvent->getZoomUserId() ? $oldEvent->getZoomUserId()->getValue() : null); |
| 209 | |
| 210 | $zoomUsersLicenced = false; |
| 211 | |
| 212 | if ($oldEvent->getZoomUserId() && $event->getZoomUserId() && $zoomUserChanged) { |
| 213 | /** @var AbstractZoomApplicationService $zoomService */ |
| 214 | $zoomService = $this->container->get('application.zoom.service'); |
| 215 | |
| 216 | $zoomUserType = 0; |
| 217 | $zoomOldUserType = 0; |
| 218 | $zoomResult = $zoomService->getUsers(); |
| 219 | if (!(isset($zoomResult['code']) && $zoomResult['code'] === 124) && |
| 220 | !($zoomResult['users'] === null && isset($zoomResult['message']))) { |
| 221 | $zoomUsers = $zoomResult['users']; |
| 222 | foreach ($zoomUsers as $key => $val) { |
| 223 | if ($val['id'] === $event->getZoomUserId()->getValue()) { |
| 224 | $zoomUserType = $val['type']; |
| 225 | } |
| 226 | if ($val['id'] === $oldEvent->getZoomUserId()->getValue()) { |
| 227 | $zoomOldUserType = $val['type']; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | if ($zoomOldUserType > 1 && $zoomUserType > 1) { |
| 232 | $zoomUsersLicenced = true; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | $organizerChanged = ($event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null) |
| 237 | !== ($oldEvent->getOrganizerId() ? $oldEvent->getOrganizerId()->getValue() : null); |
| 238 | |
| 239 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 240 | $result->setMessage('Successfully updated event.'); |
| 241 | $result->setData( |
| 242 | [ |
| 243 | Entities::EVENTS => $parsedEvents, |
| 244 | 'zoomUserChanged' => $zoomUserChanged && $event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null, |
| 245 | 'zoomUsersLicenced' => $zoomUsersLicenced, |
| 246 | 'newInfo' => $newInfo ? [ |
| 247 | 'name' => $event->getName(), |
| 248 | 'description' => $event->getDescription() |
| 249 | ] : null, |
| 250 | 'newProviders' => $providersAdded, |
| 251 | 'removeProviders' => $providersRemoved, |
| 252 | 'organizerChanged' => $organizerChanged, |
| 253 | 'newOrganizer' => $event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null, |
| 254 | 'notifyParticipants' => $command->getField('notifyParticipants') |
| 255 | ] |
| 256 | ); |
| 257 | |
| 258 | return $result; |
| 259 | } |
| 260 | } |
| 261 |