Completed
Branch develop (c722dc)
by Filipe
02:21 queued 01:10
created

Translator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
ccs 0
cts 0
cp 0
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of slick/i18n package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\I18n;
11
12
use Zend\I18n\Translator\Translator as ZendTranslator;
13
14
/**
15
 * Translator
16
 *
17
 * @package Slick\I18n
18
 */
19
class Translator implements TranslatorInterface
20
{
21
    /**
22
     * @var Translation[]
23
     */
24
    private $translations = [];
25
26
    /**
27
     * @var TranslatorInterface|ZendTranslator
28
     */
29
    private $service;
30
31
    /**
32
     * @var string
33
     */
34
    private $domain;
35
36
    /**
37
     * @var Language
38
     */
39
    private $currentLanguage;
40
41
    /**
42
     * @var array
43
     */
44
    private $types = [
45
        Translation::TYPE_GETTEXT   => '.mo',
46
        Translation::TYPE_PHP_ARRAY => '.php'
47
    ];
48
49
    /**
50
     * Translator constructor.
51
     * @param Translation $translation
52
     * @param ZendTranslator|null $service
53
     */
54
    public function __construct(Translation $translation, ZendTranslator $service = null)
55
    {
56
        $this->service = ( null == $service) ? new ZendTranslator() : $service;
57
58
        $this->currentLanguage = $translation->language();
59
        $this->domain = $translation->domain();
60
61
        $this->addTranslation($translation);
62
    }
63
64
    /**
65
     * The translation service
66
     *
67
     * @return ZendTranslator
68
     */
69
    public function translationService()
70
    {
71
        return $this->service;
72
    }
73
74
    /**
75
     * The current default language
76
     *
77
     * @return Language
78
     */
79
    public function currentLanguage()
80
    {
81
        return $this->currentLanguage;
82
    }
83
84
    /**
85
     * Current default domain
86
     *
87
     * @return string
88
     */
89
    public function domain()
90
    {
91
        return $this->domain;
92
    }
93 2
94
    /**
95 2
     * Translate a message.
96 2
     *
97
     * @param  string $message
98
     * @param  string $textDomain
99
     * @param  string $locale
100
     * @return string
101
     */
102 View Code Duplication
    public function translate($message, $textDomain = 'default', $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
        $textDomain = $textDomain !== 'default' ? $textDomain : $this->domain();
105
        $locale = $locale ?: (string) $this->currentLanguage();
106
107
        return $this->translationService()->translate($message, $textDomain, $locale);
108
    }
109
110
    /**
111
     * Translate a plural message.
112
     *
113
     * @param  string $singular
114
     * @param  string $plural
115
     * @param  int $number
116
     * @param  string $textDomain
117
     * @param  string|null $locale
118
     * @return string
119
     */
120 View Code Duplication
    public function translatePlural(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
121
        $singular,
122
        $plural,
123
        $number,
124
        $textDomain = 'default',
125
        $locale = null
126
    )
127 2
    {
128
        $textDomain = $textDomain !== 'default' ?: $this->domain();
129 2
        $locale = $locale ?: (string) $this->currentLanguage();
130 2
131 2
        return $this->translationService()->translatePlural($singular, $plural, $number, $textDomain, $locale);
0 ignored issues
show
Bug introduced by
It seems like $textDomain defined by $textDomain !== 'default' ?: $this->domain() on line 128 can also be of type boolean; however, Zend\I18n\Translator\Translator::translatePlural() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132 2
    }
133
134
    /**
135
     * Adds a translation to the translator
136
     *
137
     * @param Translation $translation
138
     *
139
     * @return self|Translator
140 2
     */
141
    public function addTranslation(Translation $translation)
142 2
    {
143 2
        $key = "{$translation->language()}::{$translation->domain()}";
144 2
        $name = "{$translation->domain()}{$this->types[$translation->type()]}";
145 2
        $this->translationService()
146 2
            ->addTranslationFilePattern(
147 2
                $translation->type(),
148 2
                $translation->path(),
149 2
                "%s/{$name}",
150 2
                $translation->domain()
151 2
            );
152 2
        $this->translations[$key] = $translation;
153
        return $this;
154
    }
155
156
}
157