Completed
Push — master ( 38e383...432f1e )
by Vojtěch
03:37
created

RemoteAccessManagerExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 24 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\RemoteAccessManager\DI;
6
7
use Nette\DI\CompilerExtension;
8
use SixtyEightPublishers\Application\RemoteAccessManager\RemoteAccessManager;
9
use SixtyEightPublishers\Application\Environment\ConfigurationException;
10
use SixtyEightPublishers\Application\RemoteAccessManager\Handler\DefaultAccessHandler;
11
12
class RemoteAccessManagerExtension extends CompilerExtension
13
{
14
	/**
15
	 * @var array
16
	 */
17
	private $defaults = [
18
		'disable' => FALSE,
19
		'allowAll' => RemoteAccessManager::ALLOWED_ALL,
20
		'secretKey' => RemoteAccessManager::COOKIE_SECRET,
21
		'whitelist' => NULL,
22
		'blacklist' => NULL,
23
		'handler' => DefaultAccessHandler::class,
24
	];
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function loadConfiguration()
30
	{
31
		$builder = $this->getContainerBuilder();
32
		$config = $this->getConfig($this->defaults);
33
34
		if (!is_bool($config['allowAll'])) {
35
			throw new ConfigurationException("Config key 'allowAll' must be a bool value.");
36
		}
37
38
		if ($config['disable'] === TRUE) {
39
			return;
40
		}
41
42
		$builder->addDefinition($this->prefix('remoteAccess'))
43
			->setClass(RemoteAccessManager::class, [
44
				'whitelist' => $config['whitelist'],
45
				'blacklist' => $config['blacklist'],
46
				'key' => $config['secretKey'],
47
				'mode' => $config['allowAll'],
48
				'handler' => new $config['handler'],
49
			])
50
			->addTag('run', TRUE)
51
			->addSetup('process');
52
	}
53
}
54