WidgetsManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 117
ccs 2
cts 16
cp 0.125
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 2
A get() 0 9 2
A register() 0 7 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
 * WidgetsManager.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.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Managers;
18
19
use Nette;
20
21
use IPub\Widgets\Widgets;
22
23
/**
24
 * Registered widgets manager
25
 *
26
 * @package        iPublikuj:Widgets!
27
 * @subpackage     Managers
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31 1
final class WidgetsManager implements \ArrayAccess, \IteratorAggregate
32
{
33
	/**
34
	 * Implement nette smart magic
35
	 */
36 1
	use Nette\SmartObject;
37
38
	/**
39
	 * @var Widgets\IFactory[][]
40
	 */
41
	private $widgets = [
42
		'default' => []
43
	];
44
45
	/**
46
	 * Checks whether a widget is registered
47
	 *
48
	 * @param string $type
49
	 * @param string $group
50
	 *
51
	 * @return bool
52
	 */
53
	public function has(string $type, string $group = 'default') : bool
54
	{
55
		return isset($this->widgets[$group]) && isset($this->widgets[$group][$type]);
56
	}
57
58
	/**
59
	 * Gets a widget
60
	 *
61
	 * @param string $type
62
	 * @param string $group
63
	 *
64
	 * @return Widgets\IFactory|NULL
65
	 */
66
	public function get(string $type, string $group = 'default') : ?Widgets\IFactory
67
	{
68
		// Check if widget exists or not...
69
		if ($this->has($type, $group)) {
70
			return $this->widgets[$group][$type];
71
		}
72
73
		return NULL;
74
	}
75
76
	/**
77
	 * Helper method for widgets factories registering
78
	 *
79
	 * @param Widgets\IFactory $factory
80
	 * @param string $type
81
	 * @param string $group
82
	 *
83
	 * @return void
84
	 */
85
	public function register(Widgets\IFactory $factory, string $type, string $group = 'default') : void
86
	{
87
		// Check if widget is already registered
88
		if (!$this->has($type, $group)) {
89
			$this->widgets[$group][$type] = $factory;
90
		}
91
	}
92
93
	/**
94
	 * Implements the \IteratorAggregate
95
	 *
96
	 * @return \Iterator
97
	 */
98
	public function getIterator()
99
	{
100
		return new \ArrayIterator($this->widgets);
101
	}
102
103
	/**
104
	 * Implements the \ArrayAccess
105
	 *
106
	 * @param  string $offset
107
	 *
108
	 * @return bool
109
	 */
110
	public function offsetExists($offset)
111
	{
112
		return isset($this->widgets[$offset]);
113
	}
114
115
	/**
116
	 * Implements the \ArrayAccess
117
	 *
118
	 * @param string $offset
119
	 *
120
	 * @return Widgets\IFactory[]
121
	 */
122
	public function offsetGet($offset)
123
	{
124
		return $this->widgets[$offset];
125
	}
126
127
	/**
128
	 * Implements the \ArrayAccess
129
	 *
130
	 * @param string $offset
131
	 * @param Widgets\IFactory[]|NULL $value
132
	 */
133
	public function offsetSet($offset, $value)
134
	{
135
		$this->widgets[$offset] = $value;
136
	}
137
138
	/**
139
	 * Implements the \ArrayAccess
140
	 *
141
	 * @param string $offset
142
	 */
143
	public function offsetUnset($offset)
144
	{
145
		unset($this->widgets[$offset]);
146
	}
147
}
148