<?php
// api/src/EventSubscriber/DemandeIdGeneratorSubscriber.php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Demande;
use App\Entity\Form;
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 class FormBuildGeneratorSubscriber implements EventSubscriberInterface
{
public function __construct()
{
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['setVersion', EventPriorities::PRE_WRITE],
];
}
public function setVersion(ViewEvent $event)
{
$form = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$form instanceof Form || (Request::METHOD_POST !== $method && Request::METHOD_PATCH !== $method && Request::METHOD_PUT !== $method)) {
return;
}
if (!$form->getBuild()) {
$form->setBuild(0);
}
$form->setBuild($form->getBuild() + 1);
}
}