src/EventSubscriber/SetEventLocationSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\CalendarEvents;
  5. use Harmonizely\EventTypes\LocationProvider;
  6. use Harmonizely\EventTypes\LocationTypeProvider;
  7. use Harmonizely\Model\EventInterface;
  8. use Harmonizely\Model\EventTypeInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Webmozart\Assert\Assert;
  12. class SetEventLocationSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var LocationProvider
  16.      */
  17.     private LocationProvider $locationProvider;
  18.     /**
  19.      * @var LocationTypeProvider
  20.      */
  21.     private LocationTypeProvider $locationTypeProvider;
  22.     /**
  23.      * @param LocationProvider $locationProvider
  24.      * @param LocationTypeProvider $locationTypeProvider
  25.      */
  26.     public function __construct(LocationProvider $locationProviderLocationTypeProvider $locationTypeProvider)
  27.     {
  28.         $this->locationProvider $locationProvider;
  29.         $this->locationTypeProvider $locationTypeProvider;
  30.     }
  31.     /**
  32.      * @return string[]
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CalendarEvents::EVENT_PRE_CREATE => 'setEventLocation',
  38.             CalendarEvents::EVENT_POST_CANCEL => 'setEventLocation',
  39.             CalendarEvents::EVENT_POST_RESCHEDULE => ['setEventLocation'100],
  40.         ];
  41.     }
  42.     /**
  43.      * @param GenericEvent $genericEvent
  44.      * @return void
  45.      */
  46.     public function setEventLocation(GenericEvent $genericEvent): void
  47.     {
  48.         /** @var EventInterface $event */
  49.         $event $genericEvent->getSubject();
  50.         Assert::isInstanceOf($eventEventInterface::class);
  51.         $eventType $event->getEventType();
  52.         $user $event->getUser();
  53.         $locationType $this->locationTypeProvider->provideFor($eventType$user$event);
  54.         if (EventTypeInterface::LOCATION_TYPE_CUSTOM !== $locationType) {
  55.             return;
  56.         }
  57.         $location $this->locationProvider->provideFor($event->getEventType(), $user);
  58.         $event->setLocationType($locationType);
  59.         $event->setLocation($location);
  60.     }
  61. }