src/EventSubscriber/FormBuildGeneratorSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/DemandeIdGeneratorSubscriber.php
  3. namespace App\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Demande;
  6. use App\Entity\Form;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. final class FormBuildGeneratorSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct()
  15.     {
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             KernelEvents::VIEW => ['setVersion'EventPriorities::PRE_WRITE],
  21.         ];
  22.     }
  23.     public function setVersion(ViewEvent $event)
  24.     {
  25.         $form $event->getControllerResult();
  26.         $method $event->getRequest()->getMethod();
  27.         if (!$form instanceof Form || (Request::METHOD_POST !== $method && Request::METHOD_PATCH !== $method && Request::METHOD_PUT !== $method)) {
  28.             return;
  29.         }
  30.         if (!$form->getBuild()) {
  31.             $form->setBuild(0);
  32.         }
  33.         $form->setBuild($form->getBuild() + 1);
  34.     }
  35. }