Completed
Push — master ( 4d6583...31ed54 )
by Vojtěch
02:09
created

ActiveProfile::changeLanguage()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 12
nc 5
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\Environment;
6
7
use Nette\SmartObject;
8
9
/**
10
 * @property-read NULL|string    $country
11
 * @property-read NULL|string    $language
12
 * @property-read NULL|string    $currency
13
 * @property-read NULL|string    $defaultCountry
14
 * @property-read NULL|string    $defaultLanguage
15
 * @property-read NULL|string    $defaultCurrency
16
 *
17
 * @method void onChangeLanguage(string $language)
18
 */
19 1
class ActiveProfile implements IProfile
20
{
21 1
	use SmartObject;
22
23
	/** @var \SixtyEightPublishers\Application\Environment\IProfile  */
24
	private $profile;
25
26
	/** @var \SixtyEightPublishers\Application\Environment\IProfileStorage  */
27
	private $profileStorage;
28
29
	/** @var NULL|string */
30
	private $country;
31
32
	/** @var NULL|string */
33
	private $language;
34
35
	/** @var NULL|string */
36
	private $currency;
37
38
	/** @var NULL|string */
39
	private $defaultCountry;
40
41
	/** @var NULL|string */
42
	private $defaultLanguage;
43
44
	/** @var NULL|string */
45
	private $defaultCurrency;
46
47
	/** @var NULL|callable[] */
48
	public $onChangeLanguage;
49
50
	/**
51
	 * @param \SixtyEightPublishers\Application\Environment\IProfile            $profile
52
	 * @param \SixtyEightPublishers\Application\Environment\IProfileStorage     $profileStorage
53
	 */
54
	public function __construct(IProfile $profile, IProfileStorage $profileStorage)
55
	{
56 1
		$this->profile = $profile;
57 1
		$this->profileStorage = $profileStorage;
58 1
		$this->defaultCountry = $profile->getCountries()[0] ?? NULL;
59 1
		$this->defaultLanguage = $profile->getLanguages()[0] ?? NULL;
60 1
		$this->defaultCurrency = $profile->getCurrencies()[0] ?? NULL;
61 1
	}
62
63
	/**
64
	 * @param bool $useDefault
65
	 *
66
	 * @return NULL|string
67
	 */
68
	public function getCountry(bool $useDefault = TRUE) : ?string
69
	{
70 1
		return $useDefault ? ($this->country ?? $this->defaultCountry) : $this->country;
71
	}
72
73
	/**
74
	 * @param bool $useDefault
75
	 *
76
	 * @return NULL|string
77
	 */
78
	public function getLanguage(bool $useDefault = TRUE) : ?string
79
	{
80 1
		return $useDefault ? ($this->language ?? $this->defaultLanguage) : $this->language;
81
	}
82
83
	/**
84
	 * @param bool $useDefault
85
	 *
86
	 * @return NULL|string
87
	 */
88
	public function getCurrency(bool $useDefault = TRUE) : ?string
89
	{
90 1
		return $useDefault ? ($this->currency ?? $this->defaultCurrency) : $this->currency;
91
	}
92
93
	/**
94
	 * @return NULL|string
95
	 */
96
	public function getDefaultCountry() : ?string
97
	{
98
		return $this->defaultCountry;
99
	}
100
101
	/**
102
	 * @return NULL|string
103
	 */
104
	public function getDefaultLanguage() : ?string
105
	{
106
		return $this->defaultLanguage;
107
	}
108
109
	/**
110
	 * @return NULL|string
111
	 */
112
	public function getDefaultCurrency() : ?string
113
	{
114
		return $this->defaultCurrency;
115
	}
116
117
	/**
118
	 * @param string        $country
119
	 * @param bool          $persist
120
	 *
121
	 * @return $this
122
	 */
123
	public function changeCountry($country, $persist = TRUE)
124
	{
125 1
		if (!in_array($country, $this->getCountries())) {
126 1
			throw new ProfileConfigurationException("Country with code \"{$country}\" is not defined in active profile.");
127
		}
128
129 1
		$this->country = $country;
130
131 1
		if ($persist) {
132 1
			$this->profileStorage->persist();
133
		}
134
135 1
		return $this;
136
	}
137
138
	/**
139
	 * @param string        $language
140
	 * @param bool          $persist
141
	 *
142
	 * @return $this
143
	 */
144
	public function changeLanguage($language, $persist = TRUE)
145
	{
146 1
		if (!in_array($language, $this->getLanguages())) {
147 1
			if (is_string($language)) {
148 1
				foreach ($this->getLanguages() as $available) {
149 1
					if (substr($available, 0, 2) === substr($language, 0, 2)) {
150 1
						return $this->changeLanguage($available, $persist);
151
					}
152
				}
153
			}
154
155 1
			throw new ProfileConfigurationException("Language with code \"{$language}\" is not defined in active profile.");
156
		}
157
158 1
		$this->language = $language;
159
160 1
		if ($persist) {
161 1
			$this->profileStorage->persist();
162
		}
163 1
		$this->onChangeLanguage($language);
164
165 1
		return $this;
166
	}
167
168
	/**
169
	 * @param string        $currency
170
	 * @param bool          $persist
171
	 *
172
	 * @return $this
173
	 */
174
	public function changeCurrency($currency, $persist = TRUE)
175
	{
176 1
		if (!in_array($currency, $this->getCurrencies())) {
177 1
			throw new ProfileConfigurationException("Currency with code \"{$currency}\" is not defined in active profile.");
178
		}
179
180 1
		$this->currency = $currency;
181
182 1
		if ($persist) {
183 1
			$this->profileStorage->persist();
184
		}
185
186 1
		return $this;
187
	}
188
189
	/***************** interface \SixtyEightPublishers\Application\Environment\IProfile *****************/
190
191
	/**
192
	 * {@inheritdoc}
193
	 */
194
	public function getName() : string
195
	{
196 1
		return $this->profile->getName();
197
	}
198
199
	/**
200
	 * {@inheritdoc}
201
	 */
202
	public function getCountries() : array
203
	{
204 1
		return $this->profile->getCountries();
205
	}
206
207
	/**
208
	 * {@inheritdoc}
209
	 */
210
	public function getLanguages() : array
211
	{
212 1
		return $this->profile->getLanguages();
213
	}
214
215
	/**
216
	 * {@inheritdoc}
217
	 */
218
	public function getCurrencies() : array
219
	{
220 1
		return $this->profile->getCurrencies();
221
	}
222
223
	/**
224
	 * {@inheritdoc}
225
	 */
226
	public function getDomains() : array
227
	{
228 1
		return $this->profile->getDomains();
229
	}
230
}
231