Completed
Pull Request — master (#6)
by
unknown
11:57
created

EnvironmentExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 26 5
1
<?php
2
3
namespace SixtyEightPublishers\Application\DI;
4
5
use Nette\DI\CompilerExtension;
6
use SixtyEightPublishers\Application\ConfigurationException;
7
use SixtyEightPublishers\Application\Environment;
8
9
10
class EnvironmentExtension extends CompilerExtension
11
{
12
	/**
13
	 * @var array
14
	 */
15
	private $defaults = [
16
		'debugger' => FALSE,
17
		'localeDomain' => FALSE,
18
		'mode' => 'tolerant',
19
		'profile' => [],
20
	];
21
22
	public function loadConfiguration()
23
	{
24
		$builder = $this->getContainerBuilder();
25
		$config = $this->getConfig($this->defaults);
26
27
		$environment = $builder->addDefinition($this->prefix('environment'))
28
			->setClass(Environment::class);
29
30
		if (empty($config['profile']))
31
			throw new ConfigurationException("You must define some profile combination in your configuration.");
32
33
		$requiredProfileParams = ['country', 'language', 'currency'];
34
		foreach ($config['profile'] as $profileName => $profile)
35
		{
36
			if (!empty(array_diff($requiredProfileParams, array_keys($profile))))
37
				throw new ConfigurationException("Problem with \"{$profileName}\" profile configuration. There are missing some of the required parameters (country, language, currency).");
38
39
			$environment->addSetup('addProfile', [
40
				$profileName,
41
				(array) $profile['country'],
42
				(array) $profile['language'],
43
				(array) $profile['currency'],
44
				array_key_exists('domain', $profile) ? (array) $profile['domain'] : []
45
			]);
46
		}
47
	}
48
49
50
}