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\EventDispatcher\LegacyEventDispatcherProxy; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | use Symfony\Component\HttpFoundation\Response; |
||
19 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
20 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
||
21 | |||
22 | /** |
||
23 | * This controller is for showing frontend pages based on slugs |
||
24 | */ |
||
25 | class SlugController extends Controller |
||
26 | { |
||
27 | /** |
||
28 | * Handle the page requests |
||
29 | * |
||
30 | * @param Request $request The request |
||
31 | * @param string $url The url |
||
32 | * @param bool $preview Show in preview mode |
||
33 | * |
||
34 | * @throws NotFoundHttpException |
||
35 | * @throws AccessDeniedException |
||
36 | * |
||
37 | * @return Response|array |
||
38 | */ |
||
39 | public function slugAction(Request $request, $url = null, $preview = false) |
||
40 | { |
||
41 | /* @var EntityManager $em */ |
||
42 | $em = $this->getDoctrine()->getManager(); |
||
43 | $locale = $request->getLocale(); |
||
44 | |||
45 | /* @var NodeTranslation $nodeTranslation */ |
||
46 | $nodeTranslation = $request->attributes->get('_nodeTranslation'); |
||
47 | |||
48 | // If no node translation -> 404 |
||
49 | if (!$nodeTranslation) { |
||
50 | throw $this->createNotFoundException('No page found for slug ' . $url); |
||
51 | } |
||
52 | |||
53 | $entity = $this->getPageEntity( |
||
54 | $request, |
||
55 | $preview, |
||
56 | $em, |
||
57 | $nodeTranslation |
||
58 | ); |
||
59 | $node = $nodeTranslation->getNode(); |
||
60 | |||
61 | $securityEvent = new SlugSecurityEvent(); |
||
62 | $securityEvent |
||
63 | ->setNode($node) |
||
64 | ->setEntity($entity) |
||
65 | ->setRequest($request) |
||
66 | ->setNodeTranslation($nodeTranslation); |
||
67 | |||
68 | $nodeMenu = $this->container->get('kunstmaan_node.node_menu'); |
||
69 | $nodeMenu->setLocale($locale); |
||
70 | $nodeMenu->setCurrentNode($node); |
||
71 | $nodeMenu->setIncludeOffline($preview); |
||
72 | |||
73 | $this->dispatch($securityEvent, Events::SLUG_SECURITY); |
||
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 | $this->dispatch($preEvent, Events::PRE_SLUG_ACTION); |
||
90 | $renderContext = $preEvent->getRenderContext(); |
||
91 | |||
92 | $response = $entity->service($this->container, $request, $renderContext); |
||
93 | |||
94 | $postEvent = new SlugEvent($response, $renderContext); |
||
95 | $this->dispatch($postEvent, Events::POST_SLUG_ACTION); |
||
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); |
||
141 | |||
142 | return $entity; |
||
143 | } |
||
144 | |||
145 | return $entity; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param object $event |
||
150 | * @param string $eventName |
||
151 | * |
||
152 | * @return object |
||
153 | */ |
||
154 | View Code Duplication | private function dispatch($event, string $eventName) |
|
0 ignored issues
–
show
|
|||
155 | { |
||
156 | $eventDispatcher = $this->container->get('event_dispatcher'); |
||
157 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
||
158 | $eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher); |
||
159 | |||
160 | return $eventDispatcher->dispatch($event, $eventName); |
||
161 | } |
||
162 | |||
163 | return $eventDispatcher->dispatch($eventName, $event); |
||
164 | } |
||
165 | } |
||
166 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.