Completed
Push — master ( 0636af...e088fc )
by Adam
07:34
created

FiltersManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 8%

Importance

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