FiltersManager   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 8%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 148
ccs 2
cts 25
cp 0.08
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 4 1
A get() 0 4 2
A register() 0 7 1
A unregister() 0 6 2
A getIterator() 0 18 3
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
/**
3
 * FilterManager.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           26.06.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Managers;
18
19
use Nette;
20
21
use IPub\Widgets\Exceptions;
22
use IPub\Widgets\Filters;
23
24
/**
25
 * Registered widgets filters manager
26
 *
27
 * @package        iPublikuj:Widgets!
28
 * @subpackage     Managers
29
 *
30
 * @author         Adam Kadlec <[email protected]>
31
 */
32 1
final class FiltersManager implements \ArrayAccess, \IteratorAggregate
33
{
34
	/**
35
	 * Implement nette smart magic
36
	 */
37 1
	use Nette\SmartObject;
38
39
	/**
40
	 * @var \SplPriorityQueue
41
	 */
42
	private $filters = [];
43
44
	/**
45
	 * @var Filters\IFactory[]
46
	 */
47
	private $factories = [];
48
49
	public function __construct()
50
	{
51
		$this->filters = new \SplPriorityQueue();
52
	}
53
54
	/**
55
	 * Check if a filter is registered
56
	 *
57
	 * @param string $name
58
	 *
59
	 * @return boolean
60
	 */
61
	public function has(string $name) : bool
62
	{
63
		return isset($this->factories[$name]);
64
	}
65
66
	/**
67
	 * Returns a registered filter class
68
	 *
69
	 * @param string $name
70
	 *
71
	 * @return Filters\IFactory|NULL
72
	 */
73
	public function get(string $name) : ?Filters\IFactory
74
	{
75
		return $this->has($name) ? $this->factories[$name] : NULL;
76
	}
77
78
	/**
79
	 * Register a filter class name
80
	 *
81
	 * @param Filters\IFactory $filter
82
	 * @param string $name
83
	 * @param int $priority
84
	 *
85
	 * @return void
86
	 */
87
	public function register(Filters\IFactory $filter, string $name, int $priority = 0) : void
88
	{
89
		$this->unregister($name);
90
91
		$this->filters->insert($name, $priority);
92
		$this->factories[$name] = $filter;
93
	}
94
95
	/**
96
	 * Unregisters a filter
97
	 *
98
	 * @param string $name
99
	 *
100
	 * @return void
101
	 */
102
	public function unregister(string $name) : void
103
	{
104
		if ($this->has($name)) {
105
			unset($this->factories[$name]);
106
		}
107
	}
108
109
	/**
110
	 * {@inheritdoc}
111
	 */
112
	public function getIterator()
113
	{
114
		$filters = [];
115
116
		$this->filters->rewind();
117
118
		while ($this->filters->valid()) {
119
			$filter = $this->filters->current();
120
121
			if ($this->has($filter)) {
122
				$filters[$filter] = $this->get($filter);
123
			}
124
125
			$this->filters->next();
126
		}
127
128
		return new \ArrayIterator($filters);
129
	}
130
131
	/**
132
	 * Implements the \ArrayAccess
133
	 *
134
	 * @param  string $offset
135
	 *
136
	 * @return bool
137
	 */
138
	public function offsetExists($offset)
139
	{
140
		return isset($this->factories[$offset]);
141
	}
142
143
	/**
144
	 * Implements the \ArrayAccess
145
	 *
146
	 * @param string $offset
147
	 *
148
	 * @return mixed
149
	 */
150
	public function offsetGet($offset)
151
	{
152
		return $this->factories[$offset];
153
	}
154
155
	/**
156
	 * Implements the \ArrayAccess
157
	 *
158
	 * @param string $offset
159
	 * @param string[]|NULL $value
160
	 *
161
	 * @throws Exceptions\InvalidStateException
162
	 */
163
	public function offsetSet($offset, $value)
164
	{
165
		throw new Exceptions\InvalidStateException('Use "register" method for adding item.');
166
	}
167
168
	/**
169
	 * Implements the \ArrayAccess
170
	 *
171
	 * @param string $offset
172
	 *
173
	 * @throws Exceptions\InvalidStateException
174
	 */
175
	public function offsetUnset($offset)
176
	{
177
		throw new Exceptions\InvalidStateException('Use "unregister" method for adding item.');
178
	}
179
}
180