src/V4/EventSubscriber/MicrosoftGraph/CreateOutlookSubscriptionUserEventSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\MicrosoftGraph;
  3. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  4. use App\Service\Cache\Exception\UnableToSaveKeyException;
  5. use App\V4\Event\OutlookSubscriptionConfigurationUserCreateEvent;
  6. use App\V4\EventSubscriber\AbstractSubscriber;
  7. use App\V4\Manager\MicrosoftGraph\OutlookSubscriptionManager;
  8. use App\V4\Model\OutlookSubscriptionConfigurationUser\OutlookSubscriptionConfigurationUser;
  9. use Psr\Cache\CacheException;
  10. use Psr\Cache\InvalidArgumentException;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  17. class CreateOutlookSubscriptionUserEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var OutlookSubscriptionManager
  21.      */
  22.     private $outlookSubscriptionManager;
  23.     public function __construct(OutlookSubscriptionManager $outlookSubscriptionManager)
  24.     {
  25.         $this->outlookSubscriptionManager $outlookSubscriptionManager;
  26.     }
  27.     /**
  28.      * @param $entity
  29.      *
  30.      * @return bool
  31.      */
  32.     public function supports($entity): bool
  33.     {
  34.         return $entity instanceof OutlookSubscriptionConfigurationUser;
  35.     }
  36.     /**
  37.      * @return string[]
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             OutlookSubscriptionConfigurationUserCreateEvent::NAME => [
  43.                 ['fetchFolderId'1000],
  44.                 ['createSubscription'100],
  45.             ],
  46.         ];
  47.     }
  48.     /**
  49.      * @param OutlookSubscriptionConfigurationUserCreateEvent $event
  50.      *
  51.      * @return void
  52.      *
  53.      * @throws CacheException
  54.      * @throws ClientExceptionInterface
  55.      * @throws InvalidArgumentException
  56.      * @throws RedirectionExceptionInterface
  57.      * @throws ResourceClassNotSupportedException
  58.      * @throws ServerExceptionInterface
  59.      * @throws TransportExceptionInterface
  60.      * @throws UnableToSaveKeyException
  61.      * @throws DecodingExceptionInterface
  62.      */
  63.     public function fetchFolderId(OutlookSubscriptionConfigurationUserCreateEvent $event): void
  64.     {
  65.         if (!$this->supports($event->getOutlookSubscriptionConfigurationUser())
  66.             || $this->isIdAlreadyManaged($event->getOutlookSubscriptionConfigurationUser()->getId(), get_class($this))) {
  67.             return;
  68.         }
  69.         if (null !== $event->getOutlookSubscriptionConfigurationUser()->getOutlookFolderId()) {
  70.             return;
  71.         }
  72.         $folderId $this->outlookSubscriptionManager->fetchFolderId($event->getOutlookSubscriptionConfigurationUser());
  73.         $event->getOutlookSubscriptionConfigurationUser()->setOutlookFolderId($folderId);
  74.     }
  75.     /**
  76.      * @param OutlookSubscriptionConfigurationUserCreateEvent $event
  77.      *
  78.      * @throws ClientExceptionInterface
  79.      * @throws RedirectionExceptionInterface
  80.      * @throws ResourceClassNotSupportedException
  81.      * @throws ServerExceptionInterface
  82.      * @throws TransportExceptionInterface
  83.      * @throws UnableToSaveKeyException
  84.      * @throws CacheException
  85.      * @throws InvalidArgumentException
  86.      */
  87.     public function createSubscription(OutlookSubscriptionConfigurationUserCreateEvent $event): void
  88.     {
  89.         if (!$this->supports($event->getOutlookSubscriptionConfigurationUser())) {
  90.             return;
  91.         }
  92.         $this->outlookSubscriptionManager->subscribe($event->getOutlookSubscriptionConfigurationUser());
  93.     }
  94. }