1 | <?php |
||
2 | |||
3 | /** |
||
4 | * (c) FSi sp. z o.o. <[email protected]> |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace FSi\Bundle\AdminBundle\Request\ParamConverter; |
||
13 | |||
14 | use FSi\Bundle\AdminBundle\Admin\ManagerInterface; |
||
15 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
||
16 | use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
19 | use FSi\Bundle\AdminBundle\Admin\Element; |
||
20 | |||
21 | class AdminElementParamConverter implements ParamConverterInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var ManagerInterface |
||
25 | */ |
||
26 | private $manager; |
||
27 | |||
28 | public function __construct(ManagerInterface $manager) |
||
29 | { |
||
30 | $this->manager = $manager; |
||
31 | } |
||
32 | |||
33 | public function apply(Request $request, ParamConverter $configuration): bool |
||
34 | { |
||
35 | $param = $configuration->getName(); |
||
36 | $id = $request->attributes->get($param, ''); |
||
37 | |||
38 | if (!$this->manager->hasElement($id)) { |
||
39 | throw new NotFoundHttpException(sprintf('Admin element with id %s does not exist.', $id)); |
||
40 | } |
||
41 | |||
42 | $request->attributes->set($param, $this->manager->getElement($id)); |
||
43 | |||
44 | return true; |
||
45 | } |
||
46 | |||
47 | public function supports(ParamConverter $configuration): bool |
||
48 | { |
||
49 | if (!$configuration instanceof ParamConverter) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
50 | return false; |
||
51 | } |
||
52 | |||
53 | if (!$configuration->getClass()) { |
||
54 | return false; |
||
55 | } |
||
56 | |||
57 | if (!class_exists($configuration->getClass()) && !interface_exists($configuration->getClass())) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | $implements = class_implements($configuration->getClass()); |
||
62 | |||
63 | if (in_array(Element::class, $implements, true)) { |
||
64 | return true; |
||
65 | } |
||
66 | |||
67 | return false; |
||
68 | } |
||
69 | } |
||
70 |