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

CMSExtension::cmsGetDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Twig\Extension;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Node;
6
use Alpixel\Bundle\CMSBundle\Helper\CMSHelper;
7
8
class CMSExtension extends \Twig_Extension
9
{
10
    protected $contentTypes;
11
    protected $container;
12
    protected $cmsHelper;
13
14
    public function __construct(CMSHelper $cmsHelper, $container, $contentTypes = null)
15
    {
16
        $this->cmsHelper = $cmsHelper;
17
        $this->container = $container;
18
        $this->contentTypes = $contentTypes;
19
    }
20
21
    public function getName()
22
    {
23
        return 'cms';
24
    }
25
26
    public function getGlobals()
27
    {
28
        return [
29
            'cms_contentTypes' => $this->contentTypes,
30
            'cms_languages'    => $this->container->getParameter('lunetics_locale.allowed_locales'),
31
        ];
32
    }
33
34
    public function getFunctions()
35
    {
36
        return [
37
            new \Twig_SimpleFunction('cms_get_translation', [$this, 'cmsHasTranslation']),
38
            new \Twig_SimpleFunction('cms_contentType_get_description', [$this, 'cmsGetDescription']),
39
        ];
40
    }
41
42
    public function cmsGetDescription(Node $node)
43
    {
44
        $contentType = $this->cmsHelper->getContentTypeFromNodeElementClass($node);
45
        if ($contentType !== null) {
46
            return $contentType['description'];
47
        }
48
    }
49
50
    public function getFilters()
51
    {
52
        return [
53
            new \Twig_SimpleFilter('iso_to_country_name', [$this, 'isoToCountryName']),
54
        ];
55
    }
56
57
    public function isoToCountryName($iso)
58
    {
59
        return \Locale::getDisplayLanguage($iso, $this->container->getParameter('default_locale'));
60
    }
61
62
    public function cmsHasTranslation(Node $node, $locale)
63
    {
64
        return $this->cmsHelper->nodeGetTranslation($node, $locale);
65
    }
66
}
67