<?php
declare(strict_types=1);
namespace Harmonizely\EventListener;
use Exception;
use Harmonizely\Calendar\ExternalCalendarManager;
use Harmonizely\EmailManager\EventEmailManagerInterface;
use Harmonizely\Mailer\AllowedSendingEmailNotificationToInviteePolicy;
use Harmonizely\Model\EventInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class EventCanceledListener
{
private $eventEmailManager;
private $externalCalendarManager;
private $logger;
private AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy;
public function __construct(
EventEmailManagerInterface $eventEmailManager,
ExternalCalendarManager $externalCalendarManager,
LoggerInterface $logger,
AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy
) {
$this->eventEmailManager = $eventEmailManager;
$this->externalCalendarManager = $externalCalendarManager;
$this->logger = $logger;
$this->allowedSendingEmailNotificationToInviteePolicy = $allowedSendingEmailNotificationToInviteePolicy;
}
public function sendEmail(GenericEvent $event): void
{
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
$this->eventEmailManager->sendCancellationNotify($event);
}
public function sendCancellationNotifyToInvitee(GenericEvent $event): void
{
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
if (false === ($this->allowedSendingEmailNotificationToInviteePolicy)($event)) {
return;
}
$this->eventEmailManager->sendCancellationNotifyToInvitee($event);
}
public function calDavCancel(GenericEvent $event): void
{
//TODO - не вызывать при отмене через гугл или аутблук
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
try {
$this->externalCalendarManager->remove($event);
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
}
}