Completed
Pull Request — 2.x (#281)
by
unknown
01:32
created

SonataTranslationExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    protected $localeBundle;
34
35
    /**
36
     * SonataTranslationExtension constructor.
37
     * @param TranslatableChecker $translatableChecker
38
     */
39
    public function __construct(TranslatableChecker $translatableChecker)
40
    {
41
        $this->translatableChecker = $translatableChecker;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getName()
48
    {
49
        return 'sonata_translation';
50
    }
51
52
    /**
53
     * @param TranslatableChecker $translatableChecker
54
     */
55
    public function setTranslatableChecker($translatableChecker)
56
    {
57
        $this->translatableChecker = $translatableChecker;
58
    }
59
60
    /**
61
     * @return TranslatableChecker
62
     */
63
    public function getTranslatableChecker()
64
    {
65
        return $this->translatableChecker;
66
    }
67
68
    /**
69
     * @return LocaleBundleInterface
70
     */
71
    public function getLocaleBundle(): LocaleBundleInterface
72
    {
73
        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...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getTests()
80
    {
81
        return [
82
            new \Twig_SimpleTest('translatable', [$this, 'isTranslatable'])
83
        ];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getFilters()
90
    {
91
        return [
92
            new \Twig_SimpleFilter('localeName', [$this, 'getLocaleName'])
93
        ];
94
    }
95
96
    /**
97
     * Check if $object is translatable.
98
     *
99
     * @param mixed $object
100
     *
101
     * @return bool
102
     */
103
    public function isTranslatable($object)
104
    {
105
        return $this->getTranslatableChecker()->isTranslatable($object);
106
    }
107
108
    /**
109
     * @param string $locale
110
     * @param string|null $displayLocale
111
     * @return string|null
112
     */
113
    public function getLocaleName(string $locale, ?string $displayLocale = null)
114
    {
115
        return $this->getLocaleBundle()->getLocaleName($locale, $displayLocale);
116
    }
117
}
118