From 77175c8da19482192c35bd886efa62fa71458f81 Mon Sep 17 00:00:00 2001 From: Vincent GRAILLOT Date: Mon, 4 Oct 2021 22:06:50 +0200 Subject: [PATCH] Add NotFoundHttpException in EntityParamConverter In ParamConverter, if an object is not found, a 404 Response is generated. --- .../ParamConverter/EntityParamConverter.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sources/lib/Request/ParamConverter/EntityParamConverter.php b/sources/lib/Request/ParamConverter/EntityParamConverter.php index 78efc30..76063a7 100644 --- a/sources/lib/Request/ParamConverter/EntityParamConverter.php +++ b/sources/lib/Request/ParamConverter/EntityParamConverter.php @@ -7,6 +7,7 @@ use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class EntityParamConverter implements ParamConverterInterface { @@ -47,6 +48,16 @@ public function apply(Request $request, ParamConverter $configuration) } } + if (null === $entity && false === $options["optional"]) { + throw new NotFoundHttpException( + sprintf( + '%s object not found by the %s.', + $options['model'], + $this->getClassName($this) + ) + ); + } + $request->attributes->set($name, $entity); return true; @@ -83,4 +94,11 @@ private function getPk(Model $model, Request $request) } return $values; } + + private function getClassName(ParamConverterInterface $configuration) + { + $r = new \ReflectionClass($configuration); + + return $r->getName(); + } }