Completed
Push — master ( 14c945...6811c1 )
by Henry
05:37
created

Hook   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 214
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 7 1
A init() 0 20 4
A getModuleArray() 0 4 1
A getEventArray() 0 4 1
A trigger() 0 16 3
A _call() 0 15 2
A collect() 0 16 3
1
<?php
2
namespace Redaxscript\Module;
3
4
use Redaxscript\Config;
5
use Redaxscript\Db;
6
use Redaxscript\Filesystem;
7
use Redaxscript\Language;
8
use Redaxscript\Registry;
9
use Redaxscript\Request;
10
use Redaxscript\Validator;
11
use function array_merge;
12
use function call_user_func_array;
13
use function in_array;
14
use function is_array;
15
use function method_exists;
16
use function strlen;
17
18
/**
19
 * parent class to handle module hooks
20
 *
21
 * @since 2.2.0
22
 *
23
 * @package Redaxscript
24
 * @category Module
25
 * @author Henry Ruhs
26
 */
27
28
class Hook
29
{
30
	/**
31
	 * instance of the registry class
32
	 *
33
	 * @var Registry
34
	 */
35
36
	protected static $_registry;
37
38
	/**
39
	 * instance of the request class
40
	 *
41
	 * @var Request
42
	 */
43
44
	protected static $_request;
45
46
	/**
47
	 * instance of the language class
48
	 *
49
	 * @var Language
50
	 */
51
52
	protected static $_language;
53
54
	/**
55
	 * instance of the config class
56
	 *
57
	 * @var Config
58
	 */
59
60
	protected static $_config;
61
62
	/**
63
	 * module namespace
64
	 *
65
	 * @var string
66
	 */
67
68
	protected static $_namespace = 'Redaxscript\Modules';
69
70
	/**
71
	 * array of installed and enabled modules
72
	 *
73
	 * @var array
74
	 */
75
76
	protected static $_moduleArray = [];
77
78
	/**
79
	 * array of triggered events
80
	 *
81
	 * @var array
82
	 */
83
84
	protected static $_eventArray = [];
85
86
	/**
87
	 * constructor of the class
88
	 *
89
	 * @since 3.0.0
90
	 *
91
	 * @param Registry $registry instance of the registry class
92
	 * @param Request $request instance of the request class
93
	 * @param Language $language instance of the language class
94
	 * @param Config $config instance of the config class
95
	 */
96
97 5
	public static function construct(Registry $registry, Request $request, Language $language, Config $config) : void
98
	{
99 5
		self::$_registry = $registry;
100 5
		self::$_request = $request;
101 5
		self::$_language = $language;
102 5
		self::$_config = $config;
103 5
	}
104
105
	/**
106
	 * init the class
107
	 *
108
	 * @since 2.6.0
109
	 */
110
111 5
	public static function init() : void
112
	{
113 5
		$accessValidator = new Validator\Access();
114 5
		$modulesFilesystem = new Filesystem\Filesystem();
115 5
		$modulesFilesystem->init('modules');
116 5
		$modulesFilesystemArray = $modulesFilesystem->getSortArray();
117 5
		$modulesInstalled = Db::forTablePrefix('modules')->where('status', 1)->findMany();
118
119
		/* process modules */
120
121 5
		foreach ($modulesInstalled as $module)
122
		{
123
			/* validate access */
124
125 5
			if (in_array($module->alias, $modulesFilesystemArray) && $accessValidator->validate($module->access, self::$_registry->get('myGroups')))
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like self::$_registry->get('myGroups') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Validator\Access::validate() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
126
			{
127 5
				self::$_moduleArray[$module->alias] = $module->alias;
128
			}
129
		}
130 5
	}
131
132
	/**
133
	 * get the module array
134
	 *
135
	 * @since 2.2.0
136
	 *
137
	 * @return array
138
	 */
139
140 1
	public static function getModuleArray() : array
141
	{
142 1
		return self::$_moduleArray;
143
	}
144
145
	/**
146
	 * get the event array
147
	 *
148
	 * @since 3.0.0
149
	 *
150
	 * @return array
151
	 */
152
153 1
	public static function getEventArray() : array
154
	{
155 1
		return self::$_eventArray;
156
	}
157
158
	/**
159
	 * collect from the module hook
160
	 *
161
	 * @since 3.2.0
162
	 *
163
	 * @param string $eventName name of the module event
164
	 * @param array $parameterArray parameter of the module hook
165
	 *
166
	 * @return array
167
	 */
168
169 1
	public static function collect(string $eventName = null, array $parameterArray = []) : array
170
	{
171 1
		$collectArray = [];
172
173
		/* process modules */
174
175 1
		foreach (self::$_moduleArray as $moduleName)
176
		{
177 1
			$callArray = self::_call($moduleName, $eventName, $parameterArray);
178 1
			if (is_array($callArray))
179
			{
180
				$collectArray = array_merge($collectArray, $callArray);
181
			}
182
		}
183 1
		return $collectArray;
184
	}
185
186
	/**
187
	 * trigger the module hook
188
	 *
189
	 * @since 3.2.0
190
	 *
191
	 * @param string $eventName name of the module event
192
	 * @param array $parameterArray parameter of the module hook
193
	 *
194
	 * @return string|null
195
	 */
196
197 3
	public static function trigger(string $eventName = null, array $parameterArray = []) : ?string
198
	{
199 3
		$output = null;
200
201
		/* process modules */
202
203 3
		foreach (self::$_moduleArray as $moduleName)
204
		{
205 3
			$outputCall = self::_call($moduleName, $eventName, $parameterArray);
206 3
			if (strlen($outputCall))
207
			{
208 2
				$output .= $outputCall;
209
			}
210
		}
211 3
		return $output;
212
	}
213
214
	/**
215
	 * call the module
216
	 *
217
	 * @since 3.2.0
218
	 *
219
	 * @param string $moduleName
220
	 * @param string $eventName
221
	 * @param array $parameterArray
222
	 *
223
	 * @return string|array|null
224
	 */
225
226 4
	protected static function _call(string $moduleName = null, string $eventName = null, array $parameterArray = [])
227
	{
228 4
		$moduleClass = self::$_namespace . '\\' . $moduleName . '\\' . $moduleName;
229 4
		if (method_exists($moduleClass, $eventName))
230
		{
231 2
			self::$_eventArray[$eventName][$moduleName] = true;
232 2
			$module = new $moduleClass(self::$_registry, self::$_request, self::$_language, self::$_config);
233
			return call_user_func_array(
234
			[
235 2
				$module,
236 2
				$eventName
237 2
			], $parameterArray);
238
		}
239 2
		return null;
240
	}
241
}
242