Completed
Push — master ( 1b88c6...1458ce )
by Benjamin
02:20
created

NodeController::dispatchAction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 19
nc 4
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
            if (count($controller) !== 2) {
29
                throw new \LogicException('The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"');
30
            } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) {
31
                throw new \LogicException(sprintf(
32
                    'Unable to find the "%s" controller or the method "%s" doesn\'t exist.',
33
                    $controller[0],
34
                    $controller[1]
35
                ));
36
            }
37
38
            return $this->forward($contentType['controller'], [
39
                '_route'        => $request->attributes->get('_route'),
40
                '_route_params' => $request->attributes->get('_route_params'),
41
                'object'        => $node,
42
            ]);
43
        }
44
45
        throw $this->createNotFoundException();
46
    }
47
48
    public function displayNodeAdminBarAction(Node $node)
49
    {
50
        $canEdit = $this->get('request')->cookies->get('can_edit');
51
52
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
53
            return $this->render('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
54
                'link' => $this->generateUrl('cms_node_editContent', ['id' => $node->getId()]),
55
            ]);
56
        }
57
58
        return new Response();
59
    }
60
61
    public function displayCustomAdminBarAction($link)
62
    {
63
        $canEdit = $this->get('request')->cookies->get('can_edit');
64
65
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
66
            return $this->render('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
67
                'link' => $link,
68
            ]);
69
        }
70
71
        return new Response();
72
    }
73
}
74