vendor/sulu/form-bundle/Event/RequestListener.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\FormBundle\Event;
  11. use Sulu\Bundle\FormBundle\Configuration\FormConfigurationFactory;
  12. use Sulu\Bundle\FormBundle\Entity\Dynamic;
  13. use Sulu\Bundle\FormBundle\Form\BuilderInterface;
  14. use Sulu\Bundle\FormBundle\Form\HandlerInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  20. use Symfony\Contracts\Service\ResetInterface;
  21. class RequestListener implements ResetInterface
  22. {
  23. /**
  24. * @var BuilderInterface
  25. */
  26. protected $formBuilder;
  27. /**
  28. * @var HandlerInterface
  29. */
  30. protected $formHandler;
  31. /**
  32. * @var FormConfigurationFactory
  33. */
  34. protected $formConfigurationFactory;
  35. /**
  36. * @var EventDispatcherInterface
  37. */
  38. protected $eventDispatcher;
  39. /**
  40. * Flag set to true when an invalid form is submitted,
  41. * so we can set the http return code to 422.
  42. *
  43. * @var bool
  44. */
  45. protected $invalidSubmittedForm = false;
  46. /**
  47. * RequestListener constructor.
  48. */
  49. public function __construct(
  50. BuilderInterface $formBuilder,
  51. HandlerInterface $formHandler,
  52. FormConfigurationFactory $formConfigurationFactory,
  53. EventDispatcherInterface $eventDispatcher
  54. ) {
  55. $this->formBuilder = $formBuilder;
  56. $this->formHandler = $formHandler;
  57. $this->formConfigurationFactory = $formConfigurationFactory;
  58. $this->eventDispatcher = $eventDispatcher;
  59. }
  60. public function onKernelRequest(RequestEvent $event): void
  61. {
  62. if (\method_exists($event, 'isMainRequest') ? !$event->isMainRequest() : !$event->isMasterRequest()) {
  63. // do nothing if it's not the master request
  64. return;
  65. }
  66. $request = $event->getRequest();
  67. if (!$request->isMethod('post')) {
  68. // do nothing if it's not a post request
  69. return;
  70. }
  71. try {
  72. $form = $this->formBuilder->buildByRequest($request);
  73. if (!$form || !$form->isSubmitted()) {
  74. // do nothing when no form was found or not valid
  75. return;
  76. }
  77. if (!$form->isValid()) {
  78. $this->invalidSubmittedForm = true;
  79. return;
  80. }
  81. } catch (\Exception $e) {
  82. // Catch all exception on build form by request
  83. return;
  84. }
  85. /** @var Dynamic $dynamic */
  86. $dynamic = $form->getData();
  87. $configuration = $this->formConfigurationFactory->buildByDynamic($dynamic);
  88. $dynamic->setLocale($request->getLocale()); // Need to be set to request locale for shadow pages, configuraiton will hold the original locale
  89. if ($this->formHandler->handle($form, $configuration)) {
  90. $serializedObject = $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic);
  91. $dynFormSavedEvent = new DynFormSavedEvent($serializedObject, $dynamic);
  92. $this->eventDispatcher->dispatch($dynFormSavedEvent, DynFormSavedEvent::NAME);
  93. $response = new RedirectResponse('?send=true');
  94. $event->setResponse($response);
  95. }
  96. }
  97. public function onKernelResponse(ResponseEvent $event): void
  98. {
  99. if (\method_exists($event, 'isMainRequest') ? !$event->isMainRequest() : !$event->isMasterRequest()) {
  100. // do nothing if it's not the master request
  101. return;
  102. }
  103. if ($this->invalidSubmittedForm) {
  104. $response = $event->getResponse();
  105. $response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
  106. $event->setResponse($response);
  107. }
  108. }
  109. public function reset(): void
  110. {
  111. $this->invalidSubmittedForm = false;
  112. }
  113. }