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

WidgetsManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 12.5%

Importance

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