<?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\Service\Core\Contract\ICompanySelector;
use Harmonizely\Types\Company\UserDeviceNotificationType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class PushNotificationSubscriber implements EventSubscriberInterface
{
/**
* @var IClient
*/
private IClient $backgroundClient;
/**
* @var ICompanySelector
*/
private ICompanySelector $companySelector;
/**
* @param IClient $backgroundClient
* @param ICompanySelector $companySelector
*/
public function __construct(
IClient $backgroundClient,
ICompanySelector $companySelector
)
{
$this->backgroundClient = $backgroundClient;
$this->companySelector = $companySelector;
}
/**
* @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();
if ($event->isRecurring()) {
return;
}
$this->backgroundClient->add(new PushNotificationData(
UserDeviceNotificationType::TYPE_CREATE,
$event->getId(),
null,
$this->companySelector->getCompany()
));
$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(),
null,
$this->companySelector->getCompany()
));
$this->backgroundClient->runJobs();
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function cancel(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
if ($event->isRecurring()) {
if ($event->getCancellation() && !$event->getCancellation()->isSendNotification()) {
return;
}
}
$this->backgroundClient->add(new PushNotificationData(
UserDeviceNotificationType::TYPE_CANCEL,
$event->getId(),
null,
$this->companySelector->getCompany()
));
$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->companySelector->getCompany()
));
$this->backgroundClient->runJobs();
}
}