ogcore/src/EventSubscriber/CommandTaskScheduleSubscrib...

60 lines
1.8 KiB
PHP

<?php
namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Dto\Output\CommandTaskScheduleOutput;
use App\Entity\CommandTaskSchedule;
use App\Entity\CommandTaskScript;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final readonly class CommandTaskScheduleSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
)
{
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['updateCommandTaskNextExecution', EventPriorities::POST_WRITE],
];
}
/**
* @throws \Exception
*/
public function updateCommandTaskNextExecution(ViewEvent $event): void
{
$commandTaskScheduleOutput = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$commandTaskScheduleOutput instanceof CommandTaskScheduleOutput ||
!in_array($method, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE])) {
return;
}
/** @var CommandTaskSchedule $commandTaskSchedule */
$commandTaskSchedule = $commandTaskScheduleOutput->getEntity();
$commandTask = $commandTaskSchedule->getCommandTask();
if ($commandTask === null) {
return;
}
$commandTask->setNextExecution($commandTask->calculateNextExecutionDate());
$this->entityManager->persist($commandTask);
$this->entityManager->flush();
}
}