Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
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 "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...
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
            $renderContext->setView($entity->getDefaultView());
86
        }
87
        $preEvent = new SlugEvent(null, $renderContext);
88
        $eventDispatcher->dispatch(Events::PRE_SLUG_ACTION, $preEvent);
89
        $renderContext = $preEvent->getRenderContext();
90
91
        $response = $entity->service($this->container, $request, $renderContext);
92
93
        $postEvent = new SlugEvent($response, $renderContext);
94
        $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent);
95
96
        $response = $postEvent->getResponse();
97
        $renderContext = $postEvent->getRenderContext();
98
99
        if ($response instanceof Response) {
100
            return $response;
101
        }
102
103
        $view = $renderContext->getView();
104
        if (empty($view)) {
105
            throw $this->createNotFoundException(sprintf('Missing view path for page "%s"', \get_class($entity)));
106
        }
107
108
        $template = new Template(array());
109
        $template->setTemplate($view);
110
        $template->setOwner([SlugController::class, 'slugAction']);
111
112
        $request->attributes->set('_template', $template);
113
114
        return $renderContext->getArrayCopy();
115
    }
116
117
    /**
118
     * @param Request                $request
119
     * @param bool                   $preview
120
     * @param EntityManagerInterface $em
121
     * @param NodeTranslation        $nodeTranslation
122
     *
123
     * @return \Kunstmaan\NodeBundle\Entity\HasNodeInterface
124
     */
125
    private function getPageEntity(Request $request, $preview, EntityManagerInterface $em, NodeTranslation $nodeTranslation)
126
    {
127
        /* @var HasNodeInterface $entity */
128
        $entity = null;
129
        if ($preview) {
130
            $version = $request->get('version');
131
            if (!empty($version) && is_numeric($version)) {
132
                $nodeVersion = $em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
133
                if (!\is_null($nodeVersion)) {
134
                    $entity = $nodeVersion->getRef($em);
135
                }
136
            }
137
        }
138
        if (\is_null($entity)) {
139
            $entity = $nodeTranslation->getPublicNodeVersion()->getRef($em);
140
141
            return $entity;
142
        }
143
144
        return $entity;
145
    }
146
}
147