DefinitionRegistry   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A keys() 0 2 1
A registerString() 0 2 1
A has() 0 2 1
A registerEntity() 0 2 1
A get() 0 5 2
A register() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of the OneGuard DynamicConfigurationBundle.
5
 *
6
 * (c) OneGuard <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OneGuard\Bundle\DynamicConfigurationBundle;
13
14
class DefinitionRegistry {
15
	/**
16
	 * @var Definition[]
17
	 */
18
	private $definitions = [];
19
20
	/**
21
	 * @param Definition $definition
22
	 * @throws \InvalidArgumentException
23
	 */
24
	public function register(Definition $definition) {
25
		if (array_key_exists($definition->getKey(), $this->definitions)) {
26
			throw new \InvalidArgumentException('Definition already exists (Key: ' . $definition->getKey() . ').');
27
		}
28
		$this->definitions[$definition->getKey()] = $definition;
29
	}
30
31
	/**
32
	 * @param string $key
33
	 * @return Definition
34
	 * @throws \InvalidArgumentException
35
	 */
36
	public function get(string $key) : Definition {
37
		if (!array_key_exists($key, $this->definitions)) {
38
			throw new \InvalidArgumentException('Definition not found (Key: ' . $key . ').');
39
		}
40
		return $this->definitions[$key];
41
	}
42
43
	/**
44
	 * @param string $key
45
	 * @return bool
46
	 */
47
	public function has(string $key) : bool {
48
		return array_key_exists($key, $this->definitions);
49
	}
50
51
	/**
52
	 * @return string[]
53
	 */
54
	public function keys() : array {
55
		return array_keys($this->definitions);
56
	}
57
58
	public function registerEntity($key, $class, $choiceLabel) {
59
		$this->register(new EntityDefinition($key, $class, $choiceLabel));
60
	}
61
62
	public function registerString($key) {
63
		$this->register(new StringDefinition($key));
64
	}
65
}
66