<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Entity\Content\Task;
use App\Service\TaxonomyDurationManager;
use Doctrine\ORM\Events as DoctrineEvents;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class TaskSubscriber implements EventSubscriberInterface
{
private $needRefresh = false;
public function __construct(
private readonly TaxonomyDurationManager $durationManager
) {
}
public function postRemove(Task $task): void
{
if ($task->isDuration()) {
$this->needRefresh = true;
}
}
public static function getSubscribedEvents(): array
{
return [
DoctrineEvents::postRemove => 'postRemove',
KernelEvents::TERMINATE => 'terminate',
ConsoleEvents::TERMINATE => 'terminate',
];
}
public function terminate(): void
{
if ($this->needRefresh) {
$this->durationManager->refreshTaxonomiesWithDuration();
}
}
}