<?php
namespace Crea\EventBundle\EventSubscriber;
use Crea\EventBundle\Console\CreaEventConsoleDisplayInterface;
use Crea\EventBundle\Helper\EventEmailSenderHelper;
use Crea\EventBundle\Manager\Level_1\EventLogManager;
use Crea\EventBundle\Model\CreaEvent;
use Crea\EventBundle\Entity\CreaEventCodes;
use Crea\EventBundle\Repository\EventModelRepository;
use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
class CreaEventSubscriber implements EventSubscriberInterface
{
protected EventModelRepository $eventModelRepository;
protected KernelInterface $kernel;
protected CreaEventConsoleDisplayInterface $creaEventConsoleDisplay;
protected EventLogManager $eventLogManager;
protected EventEmailSenderHelper $eventEmailSenderHelper;
public function __construct(EventModelRepository $eventModelRepository,
KernelInterface $kernel,
CreaEventConsoleDisplayInterface $creaEventConsoleDisplay,
EventLogManager $eventLogManager,
EventEmailSenderHelper $eventEmailSenderHelper)
{
$this->eventModelRepository = $eventModelRepository;
$this->kernel = $kernel;
$this->creaEventConsoleDisplay = $creaEventConsoleDisplay;
$this->eventLogManager = $eventLogManager;
$this->eventEmailSenderHelper = $eventEmailSenderHelper;
}
public static function getSubscribedEvents(): array
{
$events = [];
foreach (CreaEventCodes::EVENT_MODEL_CODES as $eventCode) {
$events[$eventCode] = [['handle', 256]];
}
return $events;
}
/**
* @throws Exception
* @throws TransportExceptionInterface
*/
public function handle(CreaEvent $creaEvent)
{
if ($this->kernel->getEnvironment() === "dev" && $creaEvent->isConsoleDisplayed()) {
if ($creaEvent->getEventType()->getCode() === CreaEventCodes::EVENT_TYPE_ERROR_CODE) {
$this->creaEventConsoleDisplay->displayException($creaEvent);
}
elseif ($creaEvent->getEventType()->getCode() === CreaEventCodes::EVENT_TYPE_WARNING_CODE) {
$this->creaEventConsoleDisplay->displayWarning($creaEvent);
}
else {
$this->creaEventConsoleDisplay->displayInformation($creaEvent);
}
}
if ($creaEvent->isDatabaseLogged()) {
$this->eventLogManager->logEvent($creaEvent);
}
if ($creaEvent->isEmailSend()) {
$this->eventEmailSenderHelper->sendEmail($creaEvent);
}
}
}