Completed
Push — master ( 324423...38feb2 )
by Benjamin
02:14
created

NodeController::dispatchAction()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 39
rs 6.7272
cc 7
eloc 27
nc 10
nop 2
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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class NodeController extends Controller
14
{
15
    /**
16
     * @MetaTag("node", providerClass="Alpixel\Bundle\CMSBundle\Entity\Node", title="Page de contenu")
17
     * @Method("GET")
18
     */
19
    public function dispatchAction(Request $request, $slug)
20
    {
21
        $entityManager = $this->get('doctrine.orm.entity_manager');
22
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
23
                              ->findOnePublishedBySlugAndLocale($slug, $request->getLocale());
24
25
        if ($node !== null) {
26
            $contentType = $this->get('alpixel_cms.helper')->getContentTypeFromNodeElementClass($node);
27
            $controller = explode('::', $contentType['controller']);
28
29
            try {
30
                if (count($controller) !== 2) {
31
                    throw new \LogicException('The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"');
32
                } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) {
33
                    throw new \LogicException(sprintf(
34
                        'Unable to find the "%s" controller or the method "%s" doesn\'t exist.',
35
                        $controller[0],
36
                        $controller[1]
37
                    ));
38
                }
39
40
                return $this->forward($contentType['controller'], [
41
                    '_route'        => $request->attributes->get('_route'),
42
                    '_route_params' => $request->attributes->get('_route_params'),
43
                    'object'        => $node,
44
                ]);
45
            } catch(\LogicException $e) {
46
                $environment = $this->container->get('kernel')->getEnvironment();
47
                if ($environment !== 'prod') {
48
                    $logger = $this->get('logger');
49
                    $logger->error($e->getMessage());
50
                } else {
51
                    throw $e;
52
                }
53
            }
54
        }
55
56
        throw $this->createNotFoundException();
57
    }
58
59
    public function displayNodeAdminBarAction(Node $node)
60
    {
61
        $canEdit = $this->get('request')->cookies->get('can_edit');
62
63
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
64
            return $this->render('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
65
                'link' => $this->generateUrl('cms_node_editContent', ['id' => $node->getId()]),
66
            ]);
67
        }
68
69
        return new Response();
70
    }
71
72
    public function displayCustomAdminBarAction($link)
73
    {
74
        $canEdit = $this->get('request')->cookies->get('can_edit');
75
76
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
77
            return $this->render('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
78
                'link' => $link,
79
            ]);
80
        }
81
82
        return new Response();
83
    }
84
}
85