These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeBundle\Controller; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Doctrine\ORM\EntityManagerInterface; |
||
7 | use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
||
8 | use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
||
9 | use Kunstmaan\NodeBundle\Entity\NodeVersion; |
||
10 | use Kunstmaan\NodeBundle\Event\Events; |
||
11 | use Kunstmaan\NodeBundle\Event\SlugEvent; |
||
12 | use Kunstmaan\NodeBundle\Event\SlugSecurityEvent; |
||
13 | use Kunstmaan\NodeBundle\Helper\RenderContext; |
||
14 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
||
15 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||
16 | use Symfony\Component\HttpFoundation\Request; |
||
17 | use Symfony\Component\HttpFoundation\Response; |
||
18 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
19 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
||
20 | |||
21 | /** |
||
22 | * This controller is for showing frontend pages based on slugs |
||
23 | */ |
||
24 | class SlugController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * Handle the page requests |
||
28 | * |
||
29 | * @param Request $request The request |
||
30 | * @param string $url The url |
||
31 | * @param bool $preview Show in preview mode |
||
32 | * |
||
33 | * @throws NotFoundHttpException |
||
34 | * @throws AccessDeniedException |
||
35 | * |
||
36 | * @return Response|array |
||
37 | */ |
||
38 | public function slugAction(Request $request, $url = null, $preview = false) |
||
39 | { |
||
40 | /* @var EntityManager $em */ |
||
41 | $em = $this->getDoctrine()->getManager(); |
||
42 | $locale = $request->getLocale(); |
||
43 | |||
44 | /* @var NodeTranslation $nodeTranslation */ |
||
45 | $nodeTranslation = $request->attributes->get('_nodeTranslation'); |
||
46 | |||
47 | // If no node translation -> 404 |
||
48 | if (!$nodeTranslation) { |
||
49 | throw $this->createNotFoundException('No page found for slug ' . $url); |
||
50 | } |
||
51 | |||
52 | $entity = $this->getPageEntity( |
||
53 | $request, |
||
54 | $preview, |
||
55 | $em, |
||
56 | $nodeTranslation |
||
57 | ); |
||
58 | $node = $nodeTranslation->getNode(); |
||
59 | |||
60 | $securityEvent = new SlugSecurityEvent(); |
||
61 | $securityEvent |
||
62 | ->setNode($node) |
||
63 | ->setEntity($entity) |
||
64 | ->setRequest($request) |
||
65 | ->setNodeTranslation($nodeTranslation); |
||
66 | |||
67 | $nodeMenu = $this->container->get('kunstmaan_node.node_menu'); |
||
68 | $nodeMenu->setLocale($locale); |
||
69 | $nodeMenu->setCurrentNode($node); |
||
70 | $nodeMenu->setIncludeOffline($preview); |
||
71 | |||
72 | $eventDispatcher = $this->get('event_dispatcher'); |
||
73 | $eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent); |
||
74 | |||
75 | //render page |
||
76 | $renderContext = new RenderContext( |
||
77 | array( |
||
78 | 'nodetranslation' => $nodeTranslation, |
||
79 | 'slug' => $url, |
||
80 | 'page' => $entity, |
||
81 | 'resource' => $entity, |
||
82 | 'nodemenu' => $nodeMenu, |
||
83 | ) |
||
84 | ); |
||
85 | if (method_exists($entity, 'getDefaultView')) { |
||
86 | $renderContext->setView($entity->getDefaultView()); |
||
87 | } |
||
88 | $preEvent = new SlugEvent(null, $renderContext); |
||
89 | $eventDispatcher->dispatch(Events::PRE_SLUG_ACTION, $preEvent); |
||
90 | $renderContext = $preEvent->getRenderContext(); |
||
91 | |||
92 | $response = $entity->service($this->container, $request, $renderContext); |
||
93 | |||
94 | $postEvent = new SlugEvent($response, $renderContext); |
||
95 | $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent); |
||
96 | |||
97 | $response = $postEvent->getResponse(); |
||
98 | $renderContext = $postEvent->getRenderContext(); |
||
99 | |||
100 | if ($response instanceof Response) { |
||
101 | return $response; |
||
102 | } |
||
103 | |||
104 | $view = $renderContext->getView(); |
||
105 | if (empty($view)) { |
||
106 | throw $this->createNotFoundException(sprintf('Missing view path for page "%s"', \get_class($entity))); |
||
107 | } |
||
108 | |||
109 | $template = new Template(array()); |
||
110 | $template->setTemplate($view); |
||
111 | $template->setOwner([SlugController::class, 'slugAction']); |
||
112 | |||
113 | $request->attributes->set('_template', $template); |
||
114 | |||
115 | return $renderContext->getArrayCopy(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param Request $request |
||
120 | * @param bool $preview |
||
121 | * @param EntityManagerInterface $em |
||
122 | * @param NodeTranslation $nodeTranslation |
||
123 | * |
||
124 | * @return \Kunstmaan\NodeBundle\Entity\HasNodeInterface |
||
125 | */ |
||
126 | private function getPageEntity(Request $request, $preview, EntityManagerInterface $em, NodeTranslation $nodeTranslation) |
||
127 | { |
||
128 | /* @var HasNodeInterface $entity */ |
||
129 | $entity = null; |
||
130 | if ($preview) { |
||
131 | $version = $request->get('version'); |
||
132 | if (!empty($version) && is_numeric($version)) { |
||
133 | $nodeVersion = $em->getRepository(NodeVersion::class)->find($version); |
||
134 | if (!\is_null($nodeVersion)) { |
||
135 | $entity = $nodeVersion->getRef($em); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | if (\is_null($entity)) { |
||
140 | $entity = $nodeTranslation->getPublicNodeVersion()->getRef($em); |
||
0 ignored issues
–
show
|
|||
141 | |||
142 | return $entity; |
||
143 | } |
||
144 | |||
145 | return $entity; |
||
146 | } |
||
147 | } |
||
148 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.