Completed
Push — master ( 485f99...72c2cb )
by Benjamin
02:12
created

FrontNodeController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 13
c 8
b 2
f 0
lcom 1
cbo 7
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C dispatchAction() 0 28 7
A displayNodeAdminBarAction() 0 12 3
A displayCustomAdminBarAction() 0 12 3
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Controller;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Node;
6
use Alpixel\Bundle\SEOBundle\Annotation\MetaTag;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class FrontNodeController extends Controller
15
{
16
    /**
17
     * @MetaTag("node", providerClass="Alpixel\Bundle\CMSBundle\Entity\Node", title="Page de contenu")
18
     * @ParamConverter("node", options={"mapping" : {"slug": "slug"}})
19
     * @Method("GET")
20
     */
21
    public function dispatchAction(Request $request, Node $node)
22
    {
23
        $object = $this->get('cms.helper')->getNodeElementEntityFromNode($node);
24
25
        if ($object !== null && $object->getNode()->getPublished()) {
26
            if (stripos($request->getLocale(), $object->getNode()->getLocale()) !== false) {
27
                $contentType = $this->get('cms.helper')->getContentTypeFromNodeElementClass($object);
28
                $controller = explode('::', $contentType['controller']);
29
                if (count($controller) !== 2) {
30
                    throw new \LogicException('The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"');
31
                } else if (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) {
32
                    throw new \LogicException(sprintf(
33
                        'Unable to find the "%s" controller or the method "%s" doesn\'t exist.',
34
                        $controller[0],
35
                        $controller[1]
36
                    ));
37
                }
38
39
                return $this->forward($contentType['controller'], [
40
                    '_route'        => $request->attributes->get('_route'),
41
                    '_route_params' => $request->attributes->get('_route_params'),
42
                    'object'        => $object,
43
                ]);
44
            }
45
        }
46
47
        throw $this->createNotFoundException();
48
    }
49
50
    public function displayNodeAdminBarAction(Node $node)
51
    {
52
        $canEdit = $this->get('request')->cookies->get('can_edit');
53
54
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
55
            return $this->render('CMSBundle:admin:blocks/admin_bar_page.html.twig', [
56
                'link' => $this->generateUrl('admin_alpixel_cms_node_editContent', ['id' => $node->getId()]),
57
            ]);
58
        }
59
60
        return new Response();
61
    }
62
63
    public function displayCustomAdminBarAction($link)
64
    {
65
        $canEdit = $this->get('request')->cookies->get('can_edit');
66
67
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
68
            return $this->render('CMSBundle:admin:blocks/admin_bar_page.html.twig', [
69
                'link' => $link,
70
            ]);
71
        }
72
73
        return new Response();
74
    }
75
}
76