Completed
Push — master ( 9e1211...19df0f )
by Benjamin
02:32
created

BlockExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 12
Bugs 4 Features 0
Metric Value
wmc 7
c 12
b 4
f 0
lcom 1
cbo 4
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getFunctions() 0 9 1
B displayBlock() 0 22 4
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Twig\Extension;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Block;
6
use Alpixel\Bundle\CMSBundle\Helper\BlockHelper;
7
use Doctrine\Bundle\DoctrineBundle\Registry;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
10
class BlockExtension extends \Twig_Extension
11
{
12
    private $blockHelper;
13
    private $container;
14
    private $blockConfiguration;
15
16
    public function __construct(BlockHelper $blockHelper, $container, $blockConfiguration)
17
    {
18
        $this->blockHelper = $blockHelper;
19
        $this->container = $container;
20
        $this->blockConfiguration = $blockConfiguration;
21
    }
22
23
    public function getName()
24
    {
25
        return 'cms_block';
26
    }
27
28
    public function getFunctions()
29
    {
30
        return [
31
            new \Twig_SimpleFunction('cms_block', [$this, 'displayBlock'], [
32
                'is_safe' => ['html'],
33
                'needs_environment' => true,
34
            ]),
35
        ];
36
    }
37
38
    public function displayBlock(\Twig_Environment $twig, $blockName)
39
    {
40
        $block = $this->blockHelper->loadBlock($blockName);
41
42
        if ($block === null)
43
            return null;
44
45
        $blockConf = $this->blockConfiguration[$blockName];
46
        if (!empty($blockConf['service'])) {
47
            $controller = $this->container->get($blockConf['service']);
48
            return $controller->renderAction();
49
        } else {
50
            $template = $blockConf['template'];
51
            if ($template === null) {
52
                $template = 'AlpixelCMSBundle:front:blocks/base_block.html.twig';
53
            }
54
55
            return $twig->render($template, [
56
                'block' => $block,
57
            ]);
58
        }
59
    }
60
}
61