Completed
Push — master ( e03c8b...0b5511 )
by Benjamin
02:26
created

NodeController::dispatchAction()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 54
rs 7.255
cc 9
eloc 37
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class NodeController extends Controller
13
{
14
    /**
15
     * @MetaTag("node", providerClass="Alpixel\Bundle\CMSBundle\Entity\Node", title="Page de contenu")
16
     * @Method("GET")
17
     */
18
    public function dispatchAction(Request $request, $slug)
19
    {
20
        $entityManager = $this->get('doctrine.orm.entity_manager');
21
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
22
            ->findOnePublishedBySlugAndLocale($slug, $request->getLocale());
23
24
        if ($node !== null) {
25
            $contentType = $this->get('alpixel_cms.helper.cms')->getContentTypeFromNodeElementClass($node);
26
            $controller = explode('::', $contentType['controller']);
27
28
            try {
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
                } elseif (!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' => $node,
43
                ]);
44
            } catch (\LogicException $e) {
45
                $environment = $this->container->get('kernel')->getEnvironment();
46
                if ($environment === 'prod') {
47
                    $logger = $this->get('logger');
48
                    $logger->error($e->getMessage());
49
                } else {
50
                    throw $e;
51
                }
52
            }
53
        } else {
54
            //Trying to find another node with this slug, in another language
55
            $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
56
                ->findOnePublishedBySlug($slug);
57
58
            if ($node !== null) {
59
                $translation = $entityManager->getRepository('AlpixelCMSBundle:Node')
60
                    ->findTranslation($node, $request->getLocale());
61
                if ($translation !== null) {
62
                    return $this->redirect($this->generateUrl('alpixel_cms', [
63
                        'slug' => $translation->getSlug(),
64
                        '_locale' => $translation->getLocale(),
65
                    ]));
66
                }
67
            }
68
        }
69
70
        throw $this->createNotFoundException();
71
    }
72
73
    public function displayNodeAdminBarAction($node)
74
    {
75
        $entityManager = $this->get('doctrine.orm.entity_manager');
76
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')->find($node);
77
78
        $response = new Response();
79
        $response->setPrivate();
80
        $response->setMaxAge(900);
81
82
        $canEdit = $this->get('request')->cookies->get('can_edit');
83
84
        if ($node !== null && $canEdit === hash('sha256', 'can_edit' . $this->container->getParameter('secret'))) {
85
            $content = $this->renderView('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
86
                'link' => $this->generateUrl('alpixel_admin_cms_node_forwardEdit', ['type' => $node->getType(), 'id' => $node->getId()]),
87
            ]);
88
            $response->setContent($content);
89
        }
90
91
        return $response;
92
    }
93
94
    public function displayCustomAdminBarAction($link)
95
    {
96
        $response = new Response();
97
        $response->setPrivate();
98
        $response->setMaxAge(900);
99
100
        $canEdit = $this->get('request')->cookies->get('can_edit');
101
102
        if ($canEdit === hash('sha256', 'can_edit' . $this->container->getParameter('secret'))) {
103
            $content = $this->renderView('AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig', [
104
                'link' => $link,
105
            ]);
106
            $response->setContent($content);
107
        }
108
109
        return $response;
110
    }
111
}
112