Completed
Push — master ( a7fff6...5ea170 )
by Jeroen
18:57 queued 10s
created

Kunstmaan/NodeBundle/Controller/SlugController.php (1 issue)

Upgrade to new PHP Analysis Engine

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\Event\Events;
10
use Kunstmaan\NodeBundle\Event\SlugEvent;
11
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
12
use Kunstmaan\NodeBundle\Helper\RenderContext;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
20
/**
21
 * This controller is for showing frontend pages based on slugs
22
 */
23
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 {@see 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...
24
{
25
    /**
26
     * Handle the page requests
27
     *
28
     * @param Request $request The request
29
     * @param string  $url     The url
30
     * @param bool    $preview Show in preview mode
31
     *
32
     * @throws NotFoundHttpException
33
     * @throws AccessDeniedException
34
     *
35
     * @return Response|array
36
     */
37
    public function slugAction(Request $request, $url = null, $preview = false)
38
    {
39
        /* @var EntityManager $em */
40
        $em = $this->getDoctrine()->getManager();
41
        $locale = $request->getLocale();
42
43
        /* @var NodeTranslation $nodeTranslation */
44
        $nodeTranslation = $request->attributes->get('_nodeTranslation');
45
46
        // If no node translation -> 404
47
        if (!$nodeTranslation) {
48
            throw $this->createNotFoundException('No page found for slug ' . $url);
49
        }
50
51
        $entity = $this->getPageEntity(
52
            $request,
53
            $preview,
54
            $em,
55
            $nodeTranslation
56
        );
57
        $node = $nodeTranslation->getNode();
58
59
        $securityEvent = new SlugSecurityEvent();
60
        $securityEvent
61
            ->setNode($node)
62
            ->setEntity($entity)
63
            ->setRequest($request)
64
            ->setNodeTranslation($nodeTranslation);
65
66
        $nodeMenu = $this->container->get('kunstmaan_node.node_menu');
67
        $nodeMenu->setLocale($locale);
68
        $nodeMenu->setCurrentNode($node);
69
        $nodeMenu->setIncludeOffline($preview);
70
71
        $eventDispatcher = $this->get('event_dispatcher');
72
        $eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
73
74
        //render page
75
        $renderContext = new RenderContext(
76
            array(
77
                'nodetranslation' => $nodeTranslation,
78
                'slug' => $url,
79
                'page' => $entity,
80
                'resource' => $entity,
81
                'nodemenu' => $nodeMenu,
82
            )
83
        );
84
        if (method_exists($entity, 'getDefaultView')) {
85
            /* @noinspection PhpUndefinedMethodInspection */
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
        /** @noinspection PhpUndefinedMethodInspection */
93
        $response = $entity->service($this->container, $request, $renderContext);
94
95
        $postEvent = new SlugEvent($response, $renderContext);
96
        $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent);
97
98
        $response = $postEvent->getResponse();
99
        $renderContext = $postEvent->getRenderContext();
100
101
        if ($response instanceof Response) {
102
            return $response;
103
        }
104
105
        $view = $renderContext->getView();
106
        if (empty($view)) {
107
            throw $this->createNotFoundException(sprintf('Missing view path for page "%s"', get_class($entity)));
108
        }
109
110
        $template = new Template(array());
111
        $template->setTemplate($view);
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('KunstmaanNodeBundle:NodeVersion')->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