<?php
namespace App\Migration\EventSubscriber;
use App\Migration\Console\MigrationConsoleDisplayInterface;
use App\Migration\Event\MigrationEvents;
use App\Migration\Event\MigrationExceptionEvent;
use App\Migration\Event\MigrationInformationEvent;
use App\Migration\Event\MigrationWarningEvent;
use App\Migration\Helper\MigrationEmailSenderInterface;
use App\Migration\Manager\Level_0\MigrationLogManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\EventDispatcher\Event;
class MigrationEventSubscriber implements EventSubscriberInterface
{
protected KernelInterface $kernel;
protected MigrationConsoleDisplayInterface $migrationConsoleDisplay;
protected MigrationLogManagerInterface $migrationLogManager;
protected MigrationEmailSenderInterface $migrationEmailSender;
public function __construct(KernelInterface $kernel,
MigrationConsoleDisplayInterface $migrationConsoleDisplay,
MigrationLogManagerInterface $migrationLogManager,
MigrationEmailSenderInterface $migrationEmailSender)
{
$this->kernel = $kernel;
$this->migrationConsoleDisplay = $migrationConsoleDisplay;
$this->migrationLogManager = $migrationLogManager;
$this->migrationEmailSender = $migrationEmailSender;
}
public static function getSubscribedEvents(): array
{
$events = [];
foreach (MigrationEvents::EVENTS as $event => $label) {
$events[$event] = [['handle', 256]];
}
return $events;
}
public function handle(Event $event)
{
if ($event instanceof MigrationExceptionEvent) {
$this->handleException($event);
}
else if ($event instanceof MigrationWarningEvent) {
$this->handleWarning($event);
}
else if ($event instanceof MigrationInformationEvent) {
$this->handleInformation($event);
}
}
private function handleException(MigrationExceptionEvent $event)
{
if ($this->kernel->getEnvironment() === "dev" && $event->getException()->isConsoleDisplayed()) {
$this->migrationConsoleDisplay->displayException($event->getException());
}
if ($event->getException()->isDatabaseLogged()) {
$this->migrationLogManager->logException($event->getException());
}
if ($event->getException()->isEmailSend()) {
$this->migrationEmailSender->sendExceptionEmail($event->getException());
}
}
private function handleWarning(MigrationWarningEvent $event)
{
if ($this->kernel->getEnvironment() === "dev" && $event->getWarning()->isConsoleDisplayed()) {
$this->migrationConsoleDisplay->displayWarning($event->getWarning());
}
if ($event->getWarning()->isDatabaseLogged()) {
$this->migrationLogManager->logWarning($event->getWarning());
}
if ($event->getWarning()->isEmailSend()) {
$this->migrationEmailSender->sendWarningEmail($event->getWarning());
}
}
private function handleInformation(MigrationInformationEvent $event)
{
if ($this->kernel->getEnvironment() === "dev" && $event->getInformation()->isConsoleDisplayed()) {
$this->migrationConsoleDisplay->displayInformation($event->getInformation());
}
if ($event->getInformation()->isDatabaseLogged()) {
$this->migrationLogManager->logInformation($event->getInformation());
}
if ($event->getInformation()->isEmailSend()) {
$this->migrationEmailSender->sendInformationEmail($event->getInformation());
}
}
}