Completed
Branchmaster (48c558)
created

Module::__construct()   A

↳ Parent: Module

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 7
Code Lines 5

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
namespace Redaxscript;
3
4
/**
5
 * parent class to create a module
6
 *
7
 * @since 2.2.0
8
 *
9
 * @package Redaxscript
10
 * @category Module
11
 * @author Henry Ruhs
12
 */
13
14
class Module
15
{
16
	/**
17
	 * instance of the registry class
18
	 *
19
	 * @var object
20
	 */
21
22
	protected $_registry;
23
24
	/**
25
	 * instance of the request class
26
	 *
27
	 * @var object
28
	 */
29
30
	protected $_request;
31
32
	/**
33
	 * instance of the language class
34
	 *
35
	 * @var object
36
	 */
37
38
	protected $_language;
39
40
	/**
41
	 * instance of the config class
42
	 *
43
	 * @var object
44 5
	 */
45
46
	protected $_config;
47
48 5
	/**
49 5
	 * array of the module
50 5
	 *
51 5
	 * @var array
52
	 */
53
54
	protected static $_moduleArray =
55 5
	[
56 5
		'status' => 1,
57 5
		'access' => null
58 5
	];
59 5
60
	/**
61 5
	 * array of the notification
62 5
	 *
63 5
	 * @var array
64 5
	 */
65 5
66
	protected static $_notificationArray = [];
67
68
	/**
69
	 * constructor of the class
70
	 *
71
	 * @since 3.0.0
72
	 *
73
	 * @param Registry $registry instance of the registry class
74
	 * @param Request $request instance of the request class
75
	 * @param Language $language instance of the language class
76
	 * @param Config $config instance of the config class
77 3
	 */
78
79 3
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
80 3
	{
81 1
		$this->_registry = $registry;
82
		$this->_request = $request;
83 2
		$this->_language = $language;
84 2
		$this->_config = $config;
85 1
	}
86
87 1
	/**
88
	 * init the class
89
	 *
90
	 * @since 2.4.0
91
	 *
92
	 * @param array $moduleArray custom module setup
93
	 */
94
95
	public function init($moduleArray = [])
96
	{
97
		/* merge module setup */
98
99 2
		if (is_array($moduleArray))
100
		{
101 2
			static::$_moduleArray = array_merge(static::$_moduleArray, $moduleArray);
102 2
		}
103 2
104
		/* load the language */
105
106
		if (array_key_exists('alias', static::$_moduleArray))
107
		{
108
			$registry = Registry::getInstance();
109
			$language = Language::getInstance();
110
			$language->load(
111 6
			[
112
				'modules/' . static::$_moduleArray['alias'] . '/languages/en.json',
113 6
				'modules/' . static::$_moduleArray['alias'] . '/languages/' . $registry->get('language') . '.json'
114 6
			]);
115 6
		}
116 6
	}
117 6
118
	/**
119
	 * get message from notification
120
	 *
121 6
	 * @since 3.0.0
122 6
	 *
123 6
	 * @param string $type type of the notification
124 2
	 *
125 2
	 * @return mixed
126 2
	 */
127 2
128 6
	public function getNotification($type = null)
129 6
	{
130
		if (array_key_exists($type, self::$_notificationArray))
131
		{
132
			return self::$_notificationArray[$type];
133
		}
134
		else if (!$type)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $type of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
135
		{
136
			return self::$_notificationArray;
137 2
		}
138
		return false;
139 2
	}
140 2
141 2
	/**
142
	 * set message to notification
143
	 *
144
	 * @since 3.0.0
145 2
	 *
146 2
	 * @param string $type type of the notification
147 2
	 * @param mixed $message message of the notification
148 2
	 */
149 2
150 2
	public function setNotification($type = null, $message = null)
151 2
	{
152 2
		$moduleName = static::$_moduleArray['name'];
153 2
		static::$_notificationArray[$type][$moduleName][] = $message;
154
	}
155
156
	/**
157
	 * install the module
158
	 *
159
	 * @since 2.6.0
160
	 */
161
162
	public function install()
163
	{
164
		if (array_key_exists('alias', static::$_moduleArray))
165
		{
166
			$module = Db::forTablePrefix('modules')->create();
167
			$module->set(static::$_moduleArray);
168
			$module->save();
169
170
			/* create from sql */
171
172
			$directory = 'modules/' . static::$_moduleArray['alias'] . '/database';
173
			if (is_dir($directory))
174
			{
175
				$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
176
				$installer->init($directory);
177
				$installer->rawCreate();
178
			}
179
		}
180
	}
181
182
	/**
183
	 * uninstall the module
184
	 *
185
	 * @since 2.6.0
186
	 */
187
188
	public function uninstall()
189
	{
190
		if (array_key_exists('alias', static::$_moduleArray))
191
		{
192
			Db::forTablePrefix('modules')->where('alias', static::$_moduleArray['alias'])->deleteMany();
193
194
			/* drop from sql */
195
196
			$directory = 'modules/' . static::$_moduleArray['alias'] . '/database';
197
			if (is_dir($directory))
198
			{
199
				$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
200
				$installer->init($directory);
201
				$installer->rawDrop();
202
			}
203
		}
204
	}
205
}