Completed
Pull Request — master (#18)
by
unknown
13:28
created

Translator::getMessages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
/**
10
 * TODO may be it must be taken out from yiisoft/i18n?
11
 */
12
class Translator implements TranslatorInterface
13
{
14
    /**
15
     * @var \Yiisoft\I18n\Translator\TranslationsLoaderInterface
16
     */
17
    private $translationsLoader;
18
    /**
19
     * @var \Psr\EventDispatcher\EventDispatcherInterface
20
     */
21
    private $eventDispatcher;
22
23
    /**
24
     * @var array
25
     */
26
    private $messages = [];
27
28
    public function __construct(
29
        EventDispatcherInterface $eventDispatcher,
30
        TranslationsLoaderInterface $translationsLoader
31
    ) {
32
        $this->translationsLoader = $translationsLoader;
33
        $this->eventDispatcher = $eventDispatcher;
34
    }
35
36
    /**
37
     * Translates a message to the specified language.
38
     * If a translation is not found, a {{@see \Yiisoft\I18n\Event\MissingTranslationEvent} event will be triggered.
39
     * @param string $message the message to be translated
40
     * @param string $category the message category
41
     * @param string $locale the target locale
42
     * @return string|null the translated message or false if translation wasn't found or isn't required
43
     */
44
    public function translate(?string $message, string $category = null, string $locale = null): ?string
45
    {
46
        if ($locale === null) {
47
            $locale = $this->getDefaultLocale();
48
        }
49
50
        if ($category === null) {
51
            $category = $this->getDefaultCategory();
52
        }
53
54
        $messages = $this->getMessages($category, $locale);
55
56
        if (array_key_exists($message, $messages)) {
57
            return $messages[$message];
58
        }
59
60
        $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

60
        $missingTranslation = new MissingTranslationEvent($category, $locale, /** @scrutinizer ignore-type */ $message);
Loading history...
61
        $this->eventDispatcher->dispatch($missingTranslation);
62
63
        if ($missingTranslation->hasFallback()) {
64
            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...
65
        }
66
67
        return $messages[$message] = null;
68
    }
69
70
    private function getMessages(string $category, string $language): array
71
    {
72
        $key = $language . '/' . $category;
73
74
        if (!array_key_exists($key, $this->messages)) {
75
            $this->messages[$key] = $this->translationsLoader->load($category, $language);
76
        }
77
78
        return $this->messages[$key];
79
    }
80
81
    protected function getDefaultCategory(): string
82
    {
83
        return 'default';
84
    }
85
86
    protected function getDefaultLocale(): string
87
    {
88
        return \Locale::getDefault();
89
    }
90
}
91