Completed
Pull Request — master (#19)
by Benjamin
06:09
created

NodeController::displayNodeAdminBarAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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