Completed
Pull Request — master (#18)
by
unknown
11:20
created

Translator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Yiisoft\I18n\Translator;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Yiisoft\I18n\Event\MissingTranslationEvent;
7
use Yiisoft\I18n\TranslatorInterface;
8
9
class Translator implements TranslatorInterface
10
{
11
    /**
12
     * @var \Yiisoft\I18n\Translator\TranslationsLoaderInterface
13
     */
14
    private $translationsLoader;
15
    /**
16
     * @var \Psr\EventDispatcher\EventDispatcherInterface
17
     */
18
    private $eventDispatcher;
19
20
    /**
21
     * @var array
22
     */
23
    private $messages = [];
24
25
    public function __construct(
26
        EventDispatcherInterface $eventDispatcher,
27
        TranslationsLoaderInterface $translationsLoader
28
    ) {
29
        $this->translationsLoader = $translationsLoader;
30
        $this->eventDispatcher = $eventDispatcher;
31
    }
32
33
    /**
34
     * Translates a message to the specified language.
35
     * If a translation is not found, a {{@see \Yiisoft\I18n\Event\MissingTranslationEvent} event will be triggered.
36
     * @param string $message the message to be translated
37
     * @param string $category the message category
38
     * @param string $locale the target locale
39
     * @return string|null the translated message or false if translation wasn't found or isn't required
40
     */
41
    public function translate(?string $message, string $category = null, string $locale = null): ?string
42
    {
43
        if ($locale === null) {
44
            $locale = $this->getDefaultLocale();
45
        }
46
47
        if ($category === null) {
48
            $category = $this->getDefaultCategory();
49
        }
50
51
        $messages = $this->getMessages($category, $locale);
52
53
        if (array_key_exists($message, $messages)) {
54
            return $messages[$message];
55
        }
56
57
        $missingTranslation = new MissingTranslationEvent($category, $locale, $message);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type null; however, parameter $message of Yiisoft\I18n\Event\Missi...ionEvent::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $missingTranslation = new MissingTranslationEvent($category, $locale, /** @scrutinizer ignore-type */ $message);
Loading history...
58
        $this->eventDispatcher->dispatch($missingTranslation);
59
60
        if ($missingTranslation->hasFallback()) {
61
            return $messages[$message] = $missingTranslation->fallback();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $messages[$message] is correct as $missingTranslation->fallback() targeting Yiisoft\I18n\Event\Missi...lationEvent::fallback() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        }
63
64
        return $messages[$message] = null;
65
    }
66
67
    private function getMessages(string $category, string $language): array
68
    {
69
        $key = $language . '/' . $category;
70
71
        if (!array_key_exists($key, $this->messages)) {
72
            $this->messages[$key] = $this->translationsLoader->load($category, $language);
73
        }
74
75
        return $this->messages[$key];
76
    }
77
78
    protected function getDefaultCategory(): string
79
    {
80
        return 'default';
81
    }
82
83
    protected function getDefaultLocale(): string
84
    {
85
        return \Locale::getDefault();
86
    }
87
}
88