<?php
declare(strict_types=1);
namespace Harmonizely\EventSubscriber;
use Harmonizely\CalendarEvents;
use Harmonizely\EventTypes\LocationProvider;
use Harmonizely\EventTypes\LocationTypeProvider;
use Harmonizely\Model\EventInterface;
use Harmonizely\Model\EventTypeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
class SetEventLocationSubscriber implements EventSubscriberInterface
{
/**
* @var LocationProvider
*/
private LocationProvider $locationProvider;
/**
* @var LocationTypeProvider
*/
private LocationTypeProvider $locationTypeProvider;
/**
* @param LocationProvider $locationProvider
* @param LocationTypeProvider $locationTypeProvider
*/
public function __construct(LocationProvider $locationProvider, LocationTypeProvider $locationTypeProvider)
{
$this->locationProvider = $locationProvider;
$this->locationTypeProvider = $locationTypeProvider;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::EVENT_PRE_CREATE => 'setEventLocation',
CalendarEvents::EVENT_POST_CANCEL => 'setEventLocation',
CalendarEvents::EVENT_POST_RESCHEDULE => ['setEventLocation', 100],
];
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function setEventLocation(GenericEvent $genericEvent): void
{
/** @var EventInterface $event */
$event = $genericEvent->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
$eventType = $event->getEventType();
$user = $event->getUser();
$locationType = $this->locationTypeProvider->provideFor($eventType, $user, $event);
if (EventTypeInterface::LOCATION_TYPE_CUSTOM !== $locationType) {
return;
}
$location = $this->locationProvider->provideFor($event->getEventType(), $user);
$event->setLocationType($locationType);
$event->setLocation($location);
}
}