<?php
declare(strict_types=1);
namespace Harmonizely\EventListener;
use Harmonizely\Background\Data\EventMailManagerData;
use Harmonizely\Core\BackgroundWork\Contract\IClient;
use Harmonizely\Mailer\AllowedSendingEmailNotificationToInviteePolicy;
use Harmonizely\Model\EventInterface;
use Harmonizely\Service\Core\Contract\ICompanySelector;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class NewEventScheduledListener
{
private AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy;
private IClient $backgroundClient;
private ICompanySelector $companySelector;
public function __construct(
AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy,
IClient $backgroundClient,
ICompanySelector $companySelector
) {
$this->allowedSendingEmailNotificationToInviteePolicy = $allowedSendingEmailNotificationToInviteePolicy;
$this->backgroundClient = $backgroundClient;
$this->companySelector = $companySelector;
}
public function sendEmail(GenericEvent $event): void
{
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
if (false === ($this->allowedSendingEmailNotificationToInviteePolicy)($event)) {
return;
}
if ($event->isRecurring()) {
return;
}
$this->backgroundClient->add(new EventMailManagerData(
$event->getId(),
EventMailManagerData::NEW_EVENT_INVITEE_CONFIRMATION_ACTION,
$this->companySelector->getCompany()
));
$this->backgroundClient->runJobs();
}
public function sendNewEventEmailConfirmation(GenericEvent $event): void
{
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
if ($event->isRecurring()) {
return;
}
$this->backgroundClient->add(new EventMailManagerData(
$event->getId(),
EventMailManagerData::NEW_EVENT_EMAIL_CONFIRMATION,
$this->companySelector->getCompany()
));
$this->backgroundClient->runJobs();
}
}