1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyEightPublishers\Application; |
4
|
|
|
|
5
|
|
|
use Nette\SmartObject; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property-read string $country |
9
|
|
|
* @property-read string $language |
10
|
|
|
* @property-read string $currency |
11
|
|
|
*/ |
12
|
|
|
class ActiveProfile implements IProfile |
13
|
|
|
{ |
14
|
|
|
use SmartObject; |
15
|
|
|
|
16
|
|
|
/** @var \SixtyEightPublishers\Application\IProfile */ |
17
|
|
|
private $profile; |
18
|
|
|
|
19
|
|
|
/** @var \SixtyEightPublishers\Application\IProfileStorage */ |
20
|
|
|
private $profileStorage; |
21
|
|
|
|
22
|
|
|
/** @var null|string */ |
23
|
|
|
private $country; |
24
|
|
|
|
25
|
|
|
/** @var null|string */ |
26
|
|
|
private $language; |
27
|
|
|
|
28
|
|
|
/** @var null|string */ |
29
|
|
|
private $currency; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \SixtyEightPublishers\Application\IProfile $profile |
33
|
|
|
* @param \SixtyEightPublishers\Application\IProfileStorage $profileStorage |
34
|
|
|
*/ |
35
|
|
|
public function __construct(IProfile $profile, IProfileStorage $profileStorage) |
36
|
|
|
{ |
37
|
|
|
$this->profile = $profile; |
38
|
|
|
$this->profileStorage = $profileStorage; |
39
|
|
|
$this->country = $profile->getCountries()[0] ?? NULL; |
40
|
|
|
$this->language = $profile->getLanguages()[0] ?? NULL; |
41
|
|
|
$this->currency = $profile->getCurrencies()[0] ?? NULL; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getCountry() |
48
|
|
|
{ |
49
|
|
|
return $this->country; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getLanguage() |
56
|
|
|
{ |
57
|
|
|
return $this->language; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getCurrency() |
64
|
|
|
{ |
65
|
|
|
return $this->currency; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $country |
70
|
|
|
* @param bool $persist |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function changeCountry($country, $persist = TRUE) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (!in_array($country, $this->getCountries())) { |
77
|
|
|
throw new ProfileConfigurationException("Country with code \"{$country}\" is not defined in active profile."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->country = $country; |
81
|
|
|
|
82
|
|
|
if ($persist) { |
83
|
|
|
$this->profileStorage->persist(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $language |
91
|
|
|
* @param bool $persist |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function changeLanguage($language, $persist = TRUE) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if (!in_array($language, $this->getLanguages())) { |
98
|
|
|
throw new ProfileConfigurationException("Language with code \"{$language}\" is not defined in active profile."); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->language = $language; |
102
|
|
|
|
103
|
|
|
if ($persist) { |
104
|
|
|
$this->profileStorage->persist(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $currency |
112
|
|
|
* @param bool $persist |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function changeCurrency($currency, $persist = TRUE) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if (!in_array($currency, $this->getCurrencies())) { |
119
|
|
|
throw new ProfileConfigurationException("Currency with code \"{$currency}\" is not defined in active profile."); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->currency = $currency; |
123
|
|
|
|
124
|
|
|
if ($persist) { |
125
|
|
|
$this->profileStorage->persist(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/***************** interface \SixtyEightPublishers\Application\IProfile *****************/ |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getName() : string |
137
|
|
|
{ |
138
|
|
|
return $this->profile->getName(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getCountries() : array |
145
|
|
|
{ |
146
|
|
|
return $this->profile->getCountries(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getLanguages() : array |
153
|
|
|
{ |
154
|
|
|
return $this->profile->getLanguages(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function getCurrencies() : array |
161
|
|
|
{ |
162
|
|
|
return $this->profile->getCurrencies(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getDomains() : array |
169
|
|
|
{ |
170
|
|
|
return $this->profile->getDomains(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.