Completed
Push — master ( 7be510...f1b2bb )
by Paula
01:26
created

Translator::loadCurrentLocale()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\ApiPlatformTranslationBundle\Translation;
6
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Contracts\Translation\TranslatorInterface;
9
10
/**
11
 * Class Translator
12
 *
13
 * @package Locastic\ApiPlatformTranslationBundle\Translation
14
 */
15
class Translator implements TranslatorInterface
16
{
17
    /**
18
     * @var TranslatorInterface
19
     */
20
    private $translator;
21
22
    /**
23
     * @var RequestStack
24
     */
25
    private $requestStack;
26
27
    /**
28
     * @var string
29
     */
30
    private $defaultLocale;
31
32
    /**
33
     * Translator constructor.
34
     *
35
     * @param TranslatorInterface $translator
36
     * @param RequestStack $requestStack
37
     * @param string $defaultLocale
38
     */
39
    public function __construct(TranslatorInterface $translator, RequestStack $requestStack, string $defaultLocale)
40
    {
41
        $this->translator = $translator;
42
        $this->requestStack = $requestStack;
43
        $this->defaultLocale = $defaultLocale;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function trans($id, array $parameters = array(), $domain = null, $locale = null): string
50
    {
51
        if (!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $locale = $this->loadCurrentLocale();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $locale. This often makes code more readable.
Loading history...
53
        }
54
55
        return $this->translator->trans($id, $parameters, $domain, $locale);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function loadCurrentLocale(): string
62
    {
63
        $request = $this->requestStack->getCurrentRequest();
64
65
        if (!$request) {
66
            return $this->defaultLocale;
67
        }
68
69
        $localeCode = $request->query->get('locale');
70
71
        if (!$localeCode) {
72
            $preferredLanguage = $request->getPreferredLanguage();
73
            $preferredLanguage = '' === $preferredLanguage ? null : $preferredLanguage;
74
75
            return $preferredLanguage ?? $this->defaultLocale;
76
        }
77
78
        return $localeCode;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     * @codeCoverageIgnore
84
     */
85
    public function setLocale($locale = 'en'): void
86
    {
87
        $this->translator->setLocale($locale);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     * @codeCoverageIgnore
93
     */
94
    public function getLocale(): string
95
    {
96
        return $this->translator->getLocale();
97
    }
98
}
99