<?php
namespace App\V4\EventSubscriber\MicrosoftGraph;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\V4\Event\OutlookSubscriptionConfigurationCreateEvent;
use App\V4\EventSubscriber\AbstractSubscriber;
use App\V4\Manager\MicrosoftGraph\OutlookSubscriptionManager;
use App\V4\Model\OutlookSubscriptionConfiguration\OutlookSubscriptionConfiguration;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class CreateOutlookSubscriptionEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
{
/**
* @var OutlookSubscriptionManager
*/
private $outlookSubscriptionManager;
public function __construct(OutlookSubscriptionManager $outlookSubscriptionManager)
{
$this->outlookSubscriptionManager = $outlookSubscriptionManager;
}
/**
* @param $entity
*
* @return bool
*/
public function supports($entity): bool
{
return $entity instanceof OutlookSubscriptionConfiguration;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
OutlookSubscriptionConfigurationCreateEvent::NAME => 'onOutlookSubConfCreation',
];
}
/**
* @param OutlookSubscriptionConfigurationCreateEvent $event
*
* @throws CacheException
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws UnableToSaveKeyException
*/
public function onOutlookSubConfCreation(OutlookSubscriptionConfigurationCreateEvent $event): void
{
if ($this->supports($event->getOutlookSubscriptionConfiguration())
&& !$this->isIdAlreadyManaged($event->getOutlookSubscriptionConfiguration()->getId(), get_class($this))) {
$this->outlookSubscriptionManager->subscribeAll($event->getOutlookSubscriptionConfiguration());
}
}
}