Completed
Push — master ( 1dbefb...c77374 )
by Jeroen
32s queued 11s
created

SlugController::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 9
cp 0
c 0
b 0
f 0
rs 9.9
cc 2
nc 2
nop 2
crap 6
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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
26
{
27
    /**
28
     * Handle the page requests
29
     *
30
     * @param Request $request The request
31
     * @param string  $url     The url
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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);
0 ignored issues
show
Compatibility introduced by
$em of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

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.

Loading history...
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
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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