<?php
namespace App\V4\EventSubscriber\MicrosoftGraph;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\V4\Event\OutlookSubscriptionConfigurationUserRemoveEvent;
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\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class RemoveOutlookSubscriptionUserEventSubscriber 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 [
OutlookSubscriptionConfigurationUserRemoveEvent::NAME => 'onOutlookSubConfUserRemove',
];
}
/**
* @param OutlookSubscriptionConfigurationUserRemoveEvent $event
*
* @throws ResourceClassNotSupportedException
* @throws UnableToSaveKeyException
* @throws CacheException
* @throws InvalidArgumentException
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function onOutlookSubConfUserRemove(OutlookSubscriptionConfigurationUserRemoveEvent $event): void
{
if ($this->supports($event->getOutlookSubscriptionConfigurationUser())
&& !$this->isIdAlreadyManaged($event->getOutlookSubscriptionConfigurationUser()->getId(), get_class($this))) {
$this->outlookSubscriptionManager->unsubscribe($event->getOutlookSubscriptionConfigurationUser());
}
}
}