Completed
Pull Request — 2.x (#281)
by Grégoire
01:37
created

SonataTranslationExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Twig\Extension;
15
16
use Sonata\TranslationBundle\Checker\TranslatableChecker;
17
use Symfony\Component\Intl\Intl;
18
use Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface;
19
20
/**
21
 * @author Nicolas Bastien <[email protected]>
22
 */
23
class SonataTranslationExtension extends \Twig_Extension
24
{
25
    /**
26
     * @var TranslatableChecker
27
     */
28
    protected $translatableChecker;
29
30
    /**
31
     * @var LocaleBundleInterface
32
     */
33
    private $localeBundle;
34
35
    /**
36
     * @param TranslatableChecker $translatableChecker
37
     */
38
    public function __construct(TranslatableChecker $translatableChecker)
39
    {
40
        $this->translatableChecker = $translatableChecker;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getName()
47
    {
48
        return 'sonata_translation';
49
    }
50
51
    /**
52
     * @param TranslatableChecker $translatableChecker
53
     */
54
    public function setTranslatableChecker($translatableChecker)
55
    {
56
        $this->translatableChecker = $translatableChecker;
57
    }
58
59
    /**
60
     * @return TranslatableChecker
61
     */
62
    public function getTranslatableChecker()
63
    {
64
        return $this->translatableChecker;
65
    }
66
67
    public function getLocaleBundle(): LocaleBundleInterface
68
    {
69
        return $this->localeBundle instanceof LocaleBundleInterface ? $this->localeBundle : ($this->localeBundle = Intl::getLocaleBundle());
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Intl\R...e\LocaleBundleInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getTests()
76
    {
77
        return [
78
            new \Twig_SimpleTest('translatable', [$this, 'isTranslatable']),
79
        ];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getFilters()
86
    {
87
        return [
88
            new \Twig_SimpleFilter('localeName', [$this, 'getLocaleName']),
89
        ];
90
    }
91
92
    /**
93
     * Check if $object is translatable.
94
     *
95
     * @param mixed $object
96
     *
97
     * @return bool
98
     */
99
    public function isTranslatable($object)
100
    {
101
        return $this->getTranslatableChecker()->isTranslatable($object);
102
    }
103
104
    public function getLocaleName(string $locale, ?string $displayLocale = null): ?string
105
    {
106
        return $this->getLocaleBundle()->getLocaleName($locale, $displayLocale);
107
    }
108
}
109