<?php
namespace App\V4\EventSubscriber\MicrosoftGraph;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\V4\Event\OutlookSubscriptionConfigurationUserCreateEvent;
use App\V4\EventSubscriber\AbstractSubscriber;
use App\V4\Manager\MicrosoftGraph\OutlookSubscriptionManager;
use App\V4\Model\OutlookSubscriptionConfigurationUser\OutlookSubscriptionConfigurationUser;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class CreateOutlookSubscriptionUserEventSubscriber 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 OutlookSubscriptionConfigurationUser;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
OutlookSubscriptionConfigurationUserCreateEvent::NAME => [
['fetchFolderId', 1000],
['createSubscription', 100],
],
];
}
/**
* @param OutlookSubscriptionConfigurationUserCreateEvent $event
*
* @return void
*
* @throws CacheException
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws RedirectionExceptionInterface
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws UnableToSaveKeyException
* @throws DecodingExceptionInterface
*/
public function fetchFolderId(OutlookSubscriptionConfigurationUserCreateEvent $event): void
{
if (!$this->supports($event->getOutlookSubscriptionConfigurationUser())
|| $this->isIdAlreadyManaged($event->getOutlookSubscriptionConfigurationUser()->getId(), get_class($this))) {
return;
}
if (null !== $event->getOutlookSubscriptionConfigurationUser()->getOutlookFolderId()) {
return;
}
$folderId = $this->outlookSubscriptionManager->fetchFolderId($event->getOutlookSubscriptionConfigurationUser());
$event->getOutlookSubscriptionConfigurationUser()->setOutlookFolderId($folderId);
}
/**
* @param OutlookSubscriptionConfigurationUserCreateEvent $event
*
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws UnableToSaveKeyException
* @throws CacheException
* @throws InvalidArgumentException
*/
public function createSubscription(OutlookSubscriptionConfigurationUserCreateEvent $event): void
{
if (!$this->supports($event->getOutlookSubscriptionConfigurationUser())) {
return;
}
$this->outlookSubscriptionManager->subscribe($event->getOutlookSubscriptionConfigurationUser());
}
}