Test Failed
Push — master ( 539796...4ba9a1 )
by Gabor
04:03
created

ServiceAdapter::setLocale()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\I18n\ServiceAdapter\Base;
15
16
use InvalidArgumentException;
17
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
18
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
19
use WebHemi\I18n\ServiceInterface;
20
21
/**
22
 * Class ServiceAdapter.
23
 */
24
class ServiceAdapter implements ServiceInterface
25
{
26
    /** @var ConfigurationInterface */
27
    protected $configuration;
28
    /** @var EnvironmentInterface */
29
    protected $environmentManager;
30
    /** @var string */
31
    protected $language;
32
    /** @var string */
33
    protected $territory;
34
    /** @var string */
35
    protected $codeSet;
36
    /** @var string */
37
    protected $locale;
38
    /** @var string */
39
    protected $timeZone;
40
41
    /**
42
     * ServiceAdapter constructor.
43
     *
44
     * @param ConfigurationInterface $configuration
45
     * @param EnvironmentInterface $environmentManager
46
     */
47 10
    public function __construct(
48
        ConfigurationInterface $configuration,
49
        EnvironmentInterface $environmentManager
50
    ) {
51 10
        $this->configuration = $configuration;
52 10
        $this->environmentManager = $environmentManager;
53
54
        $currentApplicationConfig = $configuration
55 10
            ->getConfig('applications')
56 10
            ->getData($environmentManager->getSelectedApplication());
57
58 10
        $this->setLocale($currentApplicationConfig['locale']);
59 10
        $this->setTimeZone($currentApplicationConfig['timezone']);
60 10
    }
61
62
    /**
63
     * Gets the language.
64
     *
65
     * @return string
66
     */
67 4
    public function getLanguage() : string
68
    {
69 4
        return $this->language;
70
    }
71
72
    /**
73
     * Sets the language.
74
     *
75
     * @param string $language
76
     */
77 6
    protected function setLanguage(string $language) : void
78
    {
79 6
        $this->language = $language;
80
81 6
        putenv('LANGUAGE='.$language);
82 6
        putenv('LANG='.$language);
83 6
        setlocale(LC_TIME, '');
84 6
    }
85
86
    /**
87
     * Gets the territory.
88
     *
89
     * @return string
90
     */
91 3
    public function getTerritory() : string
92
    {
93 3
        return $this->territory;
94
    }
95
96
    /**
97
     * Gets the Locale.
98
     *
99
     * @return string
100
     */
101 3
    public function getLocale() : string
102
    {
103 3
        return $this->locale;
104
    }
105
106
    /**
107
     * Gets the code set.
108
     *
109
     * @return string
110
     */
111 3
    public function getCodeSet() : string
112
    {
113 3
        return $this->codeSet;
114
    }
115
116
    /**
117
     * Sets the locale.
118
     *
119
     * @param string $locale
120
     * @throws InvalidArgumentException
121
     * @return ServiceAdapter
122
     */
123 6
    public function setLocale(string $locale) : ServiceAdapter
124
    {
125 6
        $matches = [];
126
127 6
        if (preg_match('/^(?P<language>[a-z]+)_(?P<territory>[A-Z]+)(?:\.(?P<codeSet>.*))?/', $locale, $matches)) {
128 6
            $language = $matches['language'];
129 6
            $this->setLanguage($language);
130
131 6
            $this->territory = $matches['territory'];
132
133 6
            $this->codeSet = $matches['codeSet'] ?? 'UTF-8';
134 6
            $localeCodeSet = strtolower(str_replace('-', '', $this->codeSet));
135
136 6
            $composedLocale = $language.'_'.$this->territory.'.'.$localeCodeSet;
137
138 6
            putenv('LC_ALL='.$composedLocale);
139 6
            setlocale(LC_ALL, $composedLocale);
140 6
            setlocale(LC_MESSAGES, $composedLocale);
141 6
            setlocale(LC_CTYPE, $composedLocale);
142
143 6
            $this->locale = $composedLocale;
144
        } else {
145 3
            throw new InvalidArgumentException(sprintf('Invalid locale: %s', $locale), 1000);
146
        }
147
148 6
        return $this;
149
    }
150
151
    /**
152
     * Sets the time zone.
153
     *
154
     * @param string $timeZone
155
     * @return ServiceAdapter
156
     */
157 6
    public function setTimeZone(string $timeZone) : ServiceAdapter
158
    {
159 6
        $this->timeZone = $timeZone;
160
161 6
        date_default_timezone_set($timeZone);
162
163 6
        return $this;
164
    }
165
166
    /**
167
     * Gets the time zone.
168
     *
169
     * @return string
170
     */
171 3
    public function getTimeZone() : string
172
    {
173 3
        return $this->timeZone;
174
    }
175
}
176