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

ProfileContainer::addProfile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\Environment;
6
7
/**
8
 * @internal
9
 */
10
class ProfileContainer implements \IteratorAggregate
11
{
12
	const DEFAULT_PROFILE_NAME = 'default';
13
14
	/** @var null|Profile */
15
	private $defaultProfile;
16
17
	/** @var Profile[] */
18
	private $profiles = [];
19
20
	/**
21
	 * @param string        $name
22
	 * @param array         $countries
23
	 * @param array         $languages
24
	 * @param array         $currencies
25
	 * @param array         $domains
26
	 * @param bool          $isEnabled
27
	 *
28
	 * @return void
29
	 */
30
	public function addProfile($name, array $countries, array $languages, array $currencies, array $domains, $isEnabled = true)
31
	{
32
		$profile = new Profile($name, $countries, $languages, $currencies, $domains);
33
34
		if (!$isEnabled) {
35
			$profile->setEnabled(FALSE);
36
		}
37
38
		if ($name === self::DEFAULT_PROFILE_NAME || !$this->defaultProfile) {
39
			$this->defaultProfile = $profile;
40
		}
41
42
		$this->profiles[$name] = $profile;
43
	}
44
45
	/**
46
	 * @return null|\SixtyEightPublishers\Application\Environment\Profile
47
	 */
48
	public function getDefaultProfile()
49
	{
50
		return $this->defaultProfile;
51
	}
52
53
	/**
54
	 * @return \SixtyEightPublishers\Application\Environment\Profile[]
55
	 */
56
	public function getProfiles()
57
	{
58
		return $this->profiles;
59
	}
60
61
	/**
62
	 * @param string $code
63
	 *
64
	 * @return \SixtyEightPublishers\Application\Environment\Profile
65
	 */
66
	public function getProfile($code)
67
	{
68
		if (!array_key_exists($code, $this->profiles)) {
69
			throw new NonExistentProfileException("Profile with name \"{$code}\" doesn't exists.");
70
		}
71
72
		return $this->profiles[$code];
73
	}
74
75
	/********************* interface \IteratorAggregate *********************/
76
77
	/**
78
	 * @return \ArrayIterator
79
	 */
80
	public function getIterator()
81
	{
82
		return new \ArrayIterator($this->profiles);
83
	}
84
}
85