Completed
Pull Request — master (#15)
by Alexis
02:21
created

FrontNodeController::dispatchAction()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 28
rs 6.7272
cc 7
eloc 18
nc 5
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 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
     * @Route("/page/{slug}", name="alpixel_cms")
18
     * @MetaTag("node", providerClass="Alpixel\Bundle\CMSBundle\Entity\Node", title="Page de contenu")
19
     * @ParamConverter("node", options={"mapping" : {"slug": "slug"}})
20
     * @Method("GET")
21
     */
22
    public function dispatchAction(Request $request, Node $node)
23
    {
24
        $object = $this->get('cms.helper')->getNodeElementEntityFromNode($node);
25
26
        if ($object !== null && $object->getNode()->getPublished()) {
27
            if (stripos($request->getLocale(), $object->getNode()->getLocale()) !== false) {
28
                $contentType = $this->get('cms.helper')->getContentTypeFromNodeElementClass($object);
29
                $controller = explode('::', $contentType['controller']);
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
                } else if (!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'        => $object,
44
                ]);
45
            }
46
        }
47
48
        throw $this->createNotFoundException();
49
    }
50
51
    public function displayNodeAdminBarAction(Node $node)
52
    {
53
        $canEdit = $this->get('request')->cookies->get('can_edit');
54
55
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
56
            return $this->render('CMSBundle:admin:blocks/admin_bar_page.html.twig', [
57
                'link' => $this->generateUrl('admin_alpixel_cms_node_editContent', ['id' => $node->getId()]),
58
            ]);
59
        }
60
61
        return new Response();
62
    }
63
64
    public function displayCustomAdminBarAction($link)
65
    {
66
        $canEdit = $this->get('request')->cookies->get('can_edit');
67
68
        if ($canEdit !== null && $canEdit === hash('sha256', 'can_edit'.$this->container->getParameter('secret'))) {
69
            return $this->render('CMSBundle:admin:blocks/admin_bar_page.html.twig', [
70
                'link' => $link,
71
            ]);
72
        }
73
74
        return new Response();
75
    }
76
}
77