ProfileProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getProfile() 0 8 2
A getAllLanguages() 0 8 2
A getAllCountries() 0 8 2
A getAllCurrencies() 0 8 2
A getUniqueElements() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\i18n;
6
7
use Nette\SmartObject;
8
use SixtyEightPublishers\i18n\Profile\ActiveProfile;
9
use SixtyEightPublishers\i18n\Profile\ProfileInterface;
10
use SixtyEightPublishers\i18n\Detector\DetectorInterface;
11
use SixtyEightPublishers\i18n\Storage\ProfileStorageInterface;
12
use SixtyEightPublishers\i18n\ProfileContainer\ProfileContainerInterface;
13
14
/**
15
 * @property-read \SixtyEightPublishers\i18n\Profile\ActiveProfile $profile
16
 * @property-read array $allLanguages
17
 * @property-read array $allCountries
18
 * @property-read array $allCurrencies
19
 */
20
final class ProfileProvider implements ProfileProviderInterface
21
{
22
	use SmartObject;
23
24
	/** @var \SixtyEightPublishers\i18n\Detector\DetectorInterface  */
25
	private $detector;
26
27
	/** @var \SixtyEightPublishers\i18n\Storage\ProfileStorageInterface  */
28
	private $profileStorage;
29
30
	/** @var \SixtyEightPublishers\i18n\ProfileContainer\ProfileContainerInterface  */
31
	private $profileContainer;
32
33
	/** @var NULL|\SixtyEightPublishers\i18n\Profile\ActiveProfile */
34
	private $profile;
35
36
	/** @var NULL|array */
37
	private $languages;
38
39
	/** @var NULL|array */
40
	private $countries;
41
42
	/** @var NULL|array */
43
	private $currencies;
44
45
	/**
46
	 * @param \SixtyEightPublishers\i18n\Detector\DetectorInterface                 $detector
47
	 * @param \SixtyEightPublishers\i18n\Storage\ProfileStorageInterface            $profileStorage
48
	 * @param \SixtyEightPublishers\i18n\ProfileContainer\ProfileContainerInterface $profileContainer
49
	 */
50
	public function __construct(
51
		DetectorInterface $detector,
52
		ProfileStorageInterface $profileStorage,
53
		ProfileContainerInterface $profileContainer
54
	) {
55
		$this->detector = $detector;
56
		$this->profileStorage = $profileStorage;
57
		$this->profileContainer = $profileContainer;
58
	}
59
60
	/**
61
	 * @param callable $cb
62
	 *
63
	 * @return array
64
	 */
65
	private function getUniqueElements(callable $cb): array
66
	{
67
		return array_values(array_unique(array_merge(...array_values(array_map($cb, $this->profileContainer->toArray())))));
68
	}
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function getProfile(): ActiveProfile
74
	{
75
		if (NULL !== $this->profile) {
76
			return $this->profile;
77
		}
78
79
		return $this->profile = $this->profileStorage->makeActiveProfile(
0 ignored issues
show
Bug introduced by
The property profile is declared read-only in SixtyEightPublishers\i18n\ProfileProvider.
Loading history...
80
			$this->detector->detect($this->profileContainer) ?? $this->profileContainer->get()
81
		);
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function getAllLanguages(): array
88
	{
89
		if (is_array($this->languages)) {
90
			return $this->languages;
91
		}
92
93
		return $this->languages = $this->getUniqueElements(static function (ProfileInterface $profile) {
94
			return $profile->getLanguages();
95
		});
96
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function getAllCountries(): array
102
	{
103
		if (is_array($this->countries)) {
104
			return $this->countries;
105
		}
106
107
		return $this->countries = $this->getUniqueElements(static function (ProfileInterface $profile) {
108
			return $profile->getCountries();
109
		});
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115
	public function getAllCurrencies(): array
116
	{
117
		if (is_array($this->currencies)) {
118
			return $this->currencies;
119
		}
120
121
		return $this->currencies = $this->getUniqueElements(static function (ProfileInterface $profile) {
122
			return $profile->getCurrencies();
123
		});
124
	}
125
}
126