<?php
namespace Harmonizely\Service\Panel\Api\CalendarIntegration\EventSubscriber;
use Harmonizely\CalendarEvents;
use Harmonizely\Core\Security\Contract\ISecurityHelper;
use Harmonizely\Model\CalendarAccountInterface;
use Harmonizely\Service\Panel\Api\CalendarIntegration\Contract\ISetDefaultCalendarService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class CalendarAccountConnectSubscriber implements EventSubscriberInterface
{
/**
* @var ISecurityHelper
*/
private ISecurityHelper $securityHelper;
/**
* @var ISetDefaultCalendarService
*/
private ISetDefaultCalendarService $setDefaultCalendarService;
/**
* @param ISecurityHelper $securityHelper
* @param ISetDefaultCalendarService $setDefaultCalendarService
*/
public function __construct(
ISecurityHelper $securityHelper,
ISetDefaultCalendarService $setDefaultCalendarService
)
{
$this->securityHelper = $securityHelper;
$this->setDefaultCalendarService = $setDefaultCalendarService;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::ACCOUNT_POST_CONNECT => 'accountPostConnect',
];
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function accountPostConnect(GenericEvent $genericEvent): void
{
/** @var CalendarAccountInterface $calendarAccount */
$calendarAccount = $genericEvent->getSubject();
if ($calendarAccount instanceof CalendarAccountInterface) {
$user = $this->securityHelper->getUser();
if ($user->getCalendarAccounts()->count() === 2) {
foreach ($calendarAccount->getCalendars() as $calendar) {
if ($calendar->isWritable()) {
$this->setDefaultCalendarService->perform($calendar->getId());
break;
}
}
}
}
}
}