<?php
namespace Harmonizely\Service\Panel\Api\Zapier\EventSubscriber;
use Harmonizely\Background\Data\ZapierNotificationData;
use Harmonizely\CalendarEvents;
use Harmonizely\Core\BackgroundWork\Contract\IClient;
use Harmonizely\Model\Event;
use Harmonizely\Types\ZapierNotificationType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class ZapierNotificationSubscriber implements EventSubscriberInterface
{
/**
* @var IClient
*/
private IClient $backgroundClient;
/**
* @param IClient $backgroundClient
*/
public function __construct(IClient $backgroundClient)
{
$this->backgroundClient = $backgroundClient;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::EVENT_POST_CREATE => 'create',
CalendarEvents::EVENT_POST_RESCHEDULE => 'reschedule',
CalendarEvents::EVENT_POST_CANCEL => 'cancel',
];
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function create(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
$this->backgroundClient->add(new ZapierNotificationData(ZapierNotificationType::TYPE_EVENT_CREATE, $event->getId()));
$this->backgroundClient->runJobs();
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function reschedule(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
$this->backgroundClient->add(new ZapierNotificationData(ZapierNotificationType::TYPE_EVENT_RESCHEDULE, $event->getId()));
$this->backgroundClient->runJobs();
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function cancel(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
$this->backgroundClient->add(new ZapierNotificationData(ZapierNotificationType::TYPE_EVENT_CANCEL, $event->getId()));
$this->backgroundClient->runJobs();
}
}