vendor/nellapp/sdk-bundle/src/EventSubscriber/ControllerEntityResolverSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the nellapp-core package.
  4. *
  5. * (c) Benjamin Georgeault
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Nellapp\Bundle\SDKBundle\EventSubscriber;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\Mapping\MappingException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /**
  18. * Class ControllerEntityResolverSubscriber
  19. *
  20. * @author Benjamin Georgeault
  21. */
  22. class ControllerEntityResolverSubscriber implements EventSubscriberInterface
  23. {
  24. const REQUEST_KEY = '_entity_resolver';
  25. public function __construct(
  26. private EntityManagerInterface $em,
  27. ) {}
  28. public static function getSubscribedEvents(): array
  29. {
  30. return [
  31. KernelEvents::CONTROLLER_ARGUMENTS => ['__invoke', 31],
  32. ];
  33. }
  34. public function __invoke(ControllerArgumentsEvent $event): void
  35. {
  36. if (!$event->isMainRequest()) {
  37. return;
  38. }
  39. $request = $event->getRequest();
  40. if (null === $array = $request->attributes->get(self::REQUEST_KEY)) {
  41. return;
  42. }
  43. if (!is_array($array)) {
  44. throw new \LogicException(sprintf(
  45. 'Attribut "_entity_resolver" in route "%s" must be an array.',
  46. $request->attributes->get('_route'),
  47. ));
  48. }
  49. foreach ($array as $varName => $class) {
  50. if (!$request->attributes->has($varName)) {
  51. throw new \LogicException(sprintf(
  52. 'Missing route variable "%s" in route "%s" for attribut "%s".',
  53. $varName,
  54. $request->attributes->get('_route'),
  55. self::REQUEST_KEY,
  56. ));
  57. }
  58. try {
  59. $repo = $this->em->getRepository($class);
  60. } catch (\ReflectionException $e) {
  61. throw new \LogicException(sprintf(
  62. 'Class "%s" not found for route variable "%s" in route "%s" for attribut "%s".',
  63. $class,
  64. $varName,
  65. $request->attributes->get('_route'),
  66. self::REQUEST_KEY,
  67. ), previous: $e);
  68. } catch (MappingException $e) {
  69. throw new \LogicException(sprintf(
  70. 'Class "%s" have no doctrine mapping for route variable "%s" in route "%s" for attribut "%s".',
  71. $class,
  72. $varName,
  73. $request->attributes->get('_route'),
  74. self::REQUEST_KEY,
  75. ), previous: $e);
  76. }
  77. if (null === $entity = $repo->find($request->attributes->get($varName))) {
  78. throw new NotFoundHttpException(sprintf(
  79. 'Cannot found entity "%s" for id "%s".',
  80. $class,
  81. $request->attributes->get($varName),
  82. ));
  83. }
  84. $request->attributes->set($varName, $entity);
  85. }
  86. }
  87. }