ProfileContainer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A toArray() 0 3 1
A __construct() 0 8 2
A get() 0 14 3
A addProfile() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\i18n\ProfileContainer;
6
7
use ArrayIterator;
8
use Nette\SmartObject;
9
use SixtyEightPublishers\i18n\Profile\ProfileInterface;
10
use SixtyEightPublishers\i18n\Exception\InvalidArgumentException;
11
12
final class ProfileContainer implements ProfileContainerInterface
13
{
14
	use SmartObject;
15
16
	/** @var \SixtyEightPublishers\i18n\Profile\ProfileInterface  */
17
	private $defaultProfile;
18
19
	/** @var \SixtyEightPublishers\i18n\Profile\ProfileInterface[] */
20
	private $profiles = [];
21
22
	/**
23
	 * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface   $defaultProfile
24
	 * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface[] $profiles
25
	 */
26
	public function __construct(ProfileInterface $defaultProfile, array $profiles)
27
	{
28
		$this->defaultProfile = $defaultProfile;
29
30
		$this->addProfile($defaultProfile);
31
32
		foreach ($profiles as $profile) {
33
			$this->addProfile($profile);
34
		}
35
	}
36
37
	/**
38
	 * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface $profile
39
	 *
40
	 * @return void
41
	 */
42
	private function addProfile(ProfileInterface $profile): void
43
	{
44
		$this->profiles[$profile->getName()] = $profile;
45
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50
	public function get(?string $name = NULL): ProfileInterface
51
	{
52
		if (NULL === $name) {
53
			return $this->defaultProfile;
54
		}
55
56
		if (!isset($this->profiles[$name])) {
57
			throw new InvalidArgumentException(sprintf(
58
				'Profile with name "%s" is not defined.',
59
				$name
60
			));
61
		}
62
63
		return $this->profiles[$name];
64
	}
65
66
	/**
67
	 * {@inheritdoc}
68
	 */
69
	public function toArray(): array
70
	{
71
		return $this->profiles;
72
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77
	public function getIterator(): ArrayIterator
78
	{
79
		return new ArrayIterator($this->toArray());
80
	}
81
}
82