Completed
Pull Request — master (#24)
by
unknown
02:18
created

ActiveProfile::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\Environment;
6
7
use Nette\SmartObject;
8
9
/**
10
 * @property-read string    $country
11
 * @property-read string    $language
12
 * @property-read string    $currency
13
 */
14 1
class ActiveProfile implements IProfile
15
{
16 1
	use SmartObject;
17
18
	/** @var \SixtyEightPublishers\Application\Environment\IProfile  */
19
	private $profile;
20
21
	/** @var \SixtyEightPublishers\Application\Environment\IProfileStorage  */
22
	private $profileStorage;
23
24
	/** @var null|string */
25
	private $country;
26
27
	/** @var null|string */
28
	private $language;
29
30
	/** @var null|string */
31
	private $currency;
32
33
	/**
34
	 * @param \SixtyEightPublishers\Application\Environment\IProfile            $profile
35
	 * @param \SixtyEightPublishers\Application\Environment\IProfileStorage     $profileStorage
36
	 */
37
	public function __construct(IProfile $profile, IProfileStorage $profileStorage)
38
	{
39 1
		$this->profile = $profile;
40 1
		$this->profileStorage = $profileStorage;
41 1
		$this->country = $profile->getCountries()[0] ?? NULL;
42 1
		$this->language = $profile->getLanguages()[0] ?? NULL;
43 1
		$this->currency = $profile->getCurrencies()[0] ?? NULL;
44 1
	}
45
46
	/**
47
	 * @return string
48
	 */
49
	public function getCountry()
50
	{
51 1
		return $this->country;
52
	}
53
54
	/**
55
	 * @return string
56
	 */
57
	public function getLanguage()
58
	{
59 1
		return $this->language;
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function getCurrency()
66
	{
67 1
		return $this->currency;
68
	}
69
70
	/**
71
	 * @param string        $country
72
	 * @param bool          $persist
73
	 *
74
	 * @return $this
75
	 */
76 View Code Duplication
	public function changeCountry($country, $persist = TRUE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
77
	{
78 1
		if (!in_array($country, $this->getCountries())) {
79 1
			throw new ProfileConfigurationException("Country with code \"{$country}\" is not defined in active profile.");
80
		}
81
82 1
		$this->country = $country;
83
84 1
		if ($persist) {
85 1
			$this->profileStorage->persist();
86
		}
87
88 1
		return $this;
89
	}
90
91
	/**
92
	 * @param string        $language
93
	 * @param bool          $persist
94
	 *
95
	 * @return $this
96
	 */
97 View Code Duplication
	public function changeLanguage($language, $persist = TRUE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
	{
99 1
		if (!in_array($language, $this->getLanguages())) {
100 1
			throw new ProfileConfigurationException("Language with code \"{$language}\" is not defined in active profile.");
101
		}
102
103 1
		$this->language = $language;
104
105 1
		if ($persist) {
106 1
			$this->profileStorage->persist();
107
		}
108
109 1
		return $this;
110
	}
111
112
	/**
113
	 * @param string        $currency
114
	 * @param bool          $persist
115
	 *
116
	 * @return $this
117
	 */
118 View Code Duplication
	public function changeCurrency($currency, $persist = TRUE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
119
	{
120 1
		if (!in_array($currency, $this->getCurrencies())) {
121 1
			throw new ProfileConfigurationException("Currency with code \"{$currency}\" is not defined in active profile.");
122
		}
123
124 1
		$this->currency = $currency;
125
126 1
		if ($persist) {
127 1
			$this->profileStorage->persist();
128
		}
129
130 1
		return $this;
131
	}
132
133
	/***************** interface \SixtyEightPublishers\Application\Environment\IProfile *****************/
134
135
	/**
136
	 * {@inheritdoc}
137
	 */
138
	public function getName() : string
139
	{
140 1
		return $this->profile->getName();
141
	}
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146
	public function getCountries() : array
147
	{
148 1
		return $this->profile->getCountries();
149
	}
150
151
	/**
152
	 * {@inheritdoc}
153
	 */
154
	public function getLanguages() : array
155
	{
156 1
		return $this->profile->getLanguages();
157
	}
158
159
	/**
160
	 * {@inheritdoc}
161
	 */
162
	public function getCurrencies() : array
163
	{
164 1
		return $this->profile->getCurrencies();
165
	}
166
167
	/**
168
	 * {@inheritdoc}
169
	 */
170
	public function getDomains() : array
171
	{
172 1
		return $this->profile->getDomains();
173
	}
174
}
175