<?php
namespace Harmonizely\Service\Panel\Api\PushNotification\EventSubscriber;
use Harmonizely\Background\Data\PushNotificationData;
use Harmonizely\CalendarEvents;
use Harmonizely\Core\BackgroundWork\Contract\IClient;
use Harmonizely\Model\Event;
use Harmonizely\Model\Member;
use Harmonizely\Organization\OrganizationEvents;
use Harmonizely\Types\UserDeviceNotificationType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class PushNotificationSubscriber 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',
OrganizationEvents::MEMBER_POST_JOIN => 'memberPostJoin',
];
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function create(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
$this->backgroundClient->add(new PushNotificationData(UserDeviceNotificationType::TYPE_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 PushNotificationData(UserDeviceNotificationType::TYPE_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 PushNotificationData(UserDeviceNotificationType::TYPE_CANCEL, $event->getId()));
$this->backgroundClient->runJobs();
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function memberPostJoin(GenericEvent $genericEvent)
{
/** @var Member $member */
$member = $genericEvent->getSubject();
$user = $member->getUser()->getId();
$this->backgroundClient->add(new PushNotificationData(UserDeviceNotificationType::TYPE_ORGANIZATION, null, $user));
$this->backgroundClient->runJobs();
}
}