Completed
Push — master ( a27783...92f70e )
by Antonio
22s
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\Component\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
     * {@inheritdoc}
60
     */
61
    public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null): string
62
    {
63
        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...
64
            $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...
65
        }
66
67
        return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function loadCurrentLocale(): string
74
    {
75
        $request = $this->requestStack->getCurrentRequest();
76
77
        if (!$request) {
78
            return $this->defaultLocale;
79
        }
80
81
        $localeCode = $request->query->get('locale');
82
83
        if (!$localeCode) {
84
            $preferredLanguage = $request->getPreferredLanguage();
85
            $preferredLanguage = '' === $preferredLanguage ? null : $preferredLanguage;
86
87
            return $preferredLanguage ?? $this->defaultLocale;
88
        }
89
90
        return $localeCode;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     * @codeCoverageIgnore
96
     */
97
    public function setLocale($locale = 'en'): void
98
    {
99
        $this->translator->setLocale($locale);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     * @codeCoverageIgnore
105
     */
106
    public function getLocale(): string
107
    {
108
        return $this->translator->getLocale();
109
    }
110
}
111