DecoratorsManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 120
ccs 8
cts 18
cp 0.4444
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A get() 0 4 2
A register() 0 8 1
A unregister() 0 6 2
A getIterator() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
/**
3
 * DecoratorsManager.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Managers
10
 * @since          1.0.0
11
 *
12
 * @date           16.06.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Managers;
18
19
use Nette;
20
21
use IPub\Widgets\Decorators;
22
23
/**
24
 * Registered widgets decorators manager
25
 *
26
 * @package        iPublikuj:Widgets!
27
 * @subpackage     Managers
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31 1
final class DecoratorsManager implements \ArrayAccess, \IteratorAggregate
32
{
33
	/**
34
	 * Implement nette smart magic
35
	 */
36 1
	use Nette\SmartObject;
37
38
	/**
39
	 * @var Decorators\IFactory[]
40
	 */
41
	protected $decorators = [];
42
43
	/**
44
	 * Check if a decorator is registered
45
	 *
46
	 * @param string $name
47
	 *
48
	 * @return boolean
49
	 */
50
	public function has(string $name) : bool
51
	{
52
		return isset($this->decorators[$name]);
53
	}
54
55
	/**
56
	 * Returns a registered decorator class
57
	 *
58
	 * @param string $name
59
	 *
60
	 * @return Decorators\IFactory|NULL
61
	 */
62
	public function get(string $name) : ?Decorators\IFactory
63
	{
64
		return $this->has($name) ? $this->decorators[$name] : NULL;
65
	}
66
67
	/**
68
	 * Register a decorator component factory
69
	 *
70
	 * @param Decorators\IFactory $decorator
71
	 * @param string $name
72
	 *
73
	 * @return void
74
	 */
75
	public function register(Decorators\IFactory $decorator, string $name) : void
76
	{
77 1
		$this->unregister($name);
78
79 1
		$this->decorators[$name] = $decorator;
80
81 1
		krsort($this->decorators);
82 1
	}
83
84
	/**
85
	 * Un-registers a decorator
86
	 *
87
	 * @param string $name
88
	 *
89
	 * @return void
90
	 */
91
	public function unregister(string $name) : void
92
	{
93 1
		if (array_key_exists($name, $this->decorators)) {
94
			unset($this->decorators[$name]);
95
		}
96 1
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function getIterator()
102
	{
103
		return new \ArrayIterator($this->decorators);
104
	}
105
106
	/**
107
	 * Implements the \ArrayAccess
108
	 *
109
	 * @param  string $offset
110
	 *
111
	 * @return bool
112
	 */
113
	public function offsetExists($offset)
114
	{
115
		return isset($this->decorators[$offset]);
116
	}
117
118
	/**
119
	 * Implements the \ArrayAccess
120
	 *
121
	 * @param string $offset
122
	 *
123
	 * @return Decorators\IFactory
124
	 */
125
	public function offsetGet($offset)
126
	{
127
		return $this->decorators[$offset];
128
	}
129
130
	/**
131
	 * Implements the \ArrayAccess
132
	 *
133
	 * @param string $offset
134
	 * @param Decorators\IFactory|NULL $value
135
	 */
136
	public function offsetSet($offset, $value)
137
	{
138
		$this->decorators[$offset] = $value;
139
	}
140
141
	/**
142
	 * Implements the \ArrayAccess
143
	 *
144
	 * @param string $offset
145
	 */
146
	public function offsetUnset($offset)
147
	{
148
		unset($this->decorators[$offset]);
149
	}
150
}
151