src/Service/Panel/Api/CalendarIntegration/EventSubscriber/CalendarAccountConnectSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Panel\Api\CalendarIntegration\EventSubscriber;
  3. use Harmonizely\CalendarEvents;
  4. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  5. use Harmonizely\Model\CalendarAccountInterface;
  6. use Harmonizely\Service\Panel\Api\CalendarIntegration\Contract\ISetDefaultCalendarService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. class CalendarAccountConnectSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var ISecurityHelper
  13.      */
  14.     private ISecurityHelper $securityHelper;
  15.     /**
  16.      * @var ISetDefaultCalendarService
  17.      */
  18.     private ISetDefaultCalendarService $setDefaultCalendarService;
  19.     /**
  20.      * @param ISecurityHelper $securityHelper
  21.      * @param ISetDefaultCalendarService $setDefaultCalendarService
  22.      */
  23.     public function __construct(
  24.         ISecurityHelper $securityHelper,
  25.         ISetDefaultCalendarService $setDefaultCalendarService
  26.     )
  27.     {
  28.         $this->securityHelper $securityHelper;
  29.         $this->setDefaultCalendarService $setDefaultCalendarService;
  30.     }
  31.     /**
  32.      * @return string[]
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CalendarEvents::ACCOUNT_POST_CONNECT => 'accountPostConnect',
  38.         ];
  39.     }
  40.     /**
  41.      * @param GenericEvent $genericEvent
  42.      * @return void
  43.      */
  44.     public function accountPostConnect(GenericEvent $genericEvent): void
  45.     {
  46.         /** @var CalendarAccountInterface $calendarAccount */
  47.         $calendarAccount $genericEvent->getSubject();
  48.         if ($calendarAccount instanceof CalendarAccountInterface) {
  49.             $user $this->securityHelper->getUser();
  50.             if ($user->getCalendarAccounts()->count() === 2) {
  51.                 foreach ($calendarAccount->getCalendars() as $calendar) {
  52.                     if ($calendar->isWritable()) {
  53.                         $this->setDefaultCalendarService->perform($calendar->getId());
  54.                         break;
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }