TranslationExtension::getGlobals()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
class TranslationExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
12
{
13
    protected $blockHelper;
14
    protected $container;
15
    protected $cmsHelper;
16
17
    public function __construct(CMSHelper $cmsHelper, BlockHelper $blockHelper, $container)
18
    {
19
        $this->cmsHelper = $cmsHelper;
20
        $this->container = $container;
21
        $this->blockHelper = $blockHelper;
22
    }
23
24
    public function getName()
25
    {
26
        return 'translation';
27
    }
28
29
    public function getGlobals()
30
    {
31
        return [
32
            'alpixel_cms_languages' => ($this->container->hasParameter('lunetics_locale.allowed_locales') ? $this->container->getParameter('lunetics_locale.allowed_locales') : null),
33
        ];
34
    }
35
36
    public function getFunctions()
37
    {
38
        return [
39
            new \Twig_SimpleFunction('alpixel_cms_get_translation', [$this, 'cmsHasTranslation']),
40
        ];
41
    }
42
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter('iso_to_country_name', [$this, 'isoToCountryName']),
47
            new \Twig_SimpleFilter('is_translatable', [$this, 'isTranslatable']),
48
            new \Twig_SimpleFilter('translation_source_property', [$this, 'translationSourceProperty']),
49
        ];
50
    }
51
52
    public function isoToCountryName($iso)
53
    {
54
        return \Locale::getDisplayLanguage($iso, $this->container->getParameter('default_locale'));
55
    }
56
57
    public function isTranslatable($object)
58
    {
59
        return $object instanceof TranslatableInterface;
60
    }
61
62
    public function cmsHasTranslation(TranslatableInterface $object, $locale)
63
    {
64
        if ($object instanceof Node) {
65
            return $this->cmsHelper->nodeGetTranslation($object, $locale);
66
        } else {
67
            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...
68
        }
69
    }
70
71
    public function translationSourceProperty($object, $property)
72
    {
73 View Code Duplication
        if (!is_object($object)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            throw new \InvalidArgumentException('The "$object" parameter must be an object.');
75
        } elseif (empty($property) || !is_string($property)) {
76
            throw new \InvalidArgumentException('The "$property" parameter must be a non empty string.');
77
        }
78
79
        if ($this->isTranslatable($object) && $object->getTranslationSource() !== null) {
80
            $object = $object->getTranslationSource();
81
        }
82
83
        $accessor = PropertyAccess::createPropertyAccessor();
84
85
        return $accessor->getValue($object, $property);
86
    }
87
}
88