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

TranslationExtension::isoToCountryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Entity\TranslatableInterface;
7
use Alpixel\Bundle\CMSBundle\Helper\BlockHelper;
8
use Alpixel\Bundle\CMSBundle\Helper\CMSHelper;
9
10
class TranslationExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
11
{
12
    protected $blockHelper;
13
    protected $container;
14
    protected $cmsHelper;
15
16
    public function __construct(CMSHelper $cmsHelper, BlockHelper $blockHelper, $container)
17
    {
18
        $this->cmsHelper = $cmsHelper;
19
        $this->container = $container;
20
        $this->blockHelper = $blockHelper;
21
    }
22
23
    public function getName()
24
    {
25
        return 'translation';
26
    }
27
28
    public function getGlobals()
29
    {
30
        return [
31
            'alpixel_cms_languages' => ($this->container->hasParameter('lunetics_locale.allowed_locales') ? $this->container->getParameter('lunetics_locale.allowed_locales') : null),
32
        ];
33
    }
34
35
    public function getFunctions()
36
    {
37
        return [
38
            new \Twig_SimpleFunction('alpixel_cms_get_translation', [$this, 'cmsHasTranslation']),
39
        ];
40
    }
41
42
    public function getFilters()
43
    {
44
        return [
45
            new \Twig_SimpleFilter('iso_to_country_name', [$this, 'isoToCountryName']),
46
            new \Twig_SimpleFilter('is_translatable', [$this, 'isTranslatable']),
47
        ];
48
    }
49
50
    public function isoToCountryName($iso)
51
    {
52
        return \Locale::getDisplayLanguage($iso, $this->container->getParameter('default_locale'));
53
    }
54
55
    public function isTranslatable($object)
56
    {
57
        return $object instanceof TranslatableInterface;
58
    }
59
60
    public function cmsHasTranslation(TranslatableInterface $object, $locale)
61
    {
62
        if ($object instanceof Node) {
63
            return $this->cmsHelper->nodeGetTranslation($object, $locale);
64
        } else {
65
            return $this->blockHelper->blockGetTranslation($object, $locale);
0 ignored issues
show
Bug introduced by
The method blockGetTranslation() does not seem to exist on object<Alpixel\Bundle\CM...dle\Helper\BlockHelper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
    }
68
}
69