Completed
Push — master ( a848da...37f5b2 )
by Henry
05:28
created

Module::reinstall()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace Redaxscript\Module;
3
4
use Redaxscript\Config;
5
use Redaxscript\Installer;
6
use Redaxscript\Language;
7
use Redaxscript\Model;
8
use Redaxscript\Registry;
9
use Redaxscript\Request;
10
use function array_key_exists;
11
use function array_merge;
12
use function is_array;
13
use function is_dir;
14
15
/**
16
 * parent class to create a module
17
 *
18
 * @since 2.2.0
19
 *
20
 * @package Redaxscript
21
 * @category Module
22
 * @author Henry Ruhs
23
 */
24
25
class Module
26
{
27
	/**
28
	 * instance of the registry class
29
	 *
30
	 * @var Registry
31
	 */
32
33
	protected $_registry;
34
35
	/**
36
	 * instance of the request class
37
	 *
38
	 * @var Request
39
	 */
40
41
	protected $_request;
42
43
	/**
44
	 * instance of the language class
45
	 *
46
	 * @var Language
47
	 */
48
49
	protected $_language;
50
51
	/**
52
	 * instance of the config class
53
	 *
54
	 * @var Config
55
	 */
56
57
	protected $_config;
58
59
	/**
60
	 * array of the module
61
	 *
62
	 * @var array
63
	 */
64
65
	protected static $_moduleArray = [];
66
67
	/**
68
	 * constructor of the class
69
	 *
70
	 * @since 3.0.0
71
	 *
72
	 * @param Registry $registry instance of the registry class
73
	 * @param Request $request instance of the request class
74
	 * @param Language $language instance of the language class
75
	 * @param Config $config instance of the config class
76
	 */
77
78 3
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
79
	{
80 3
		$this->_registry = $registry;
81 3
		$this->_request = $request;
82 3
		$this->_language = $language;
83 3
		$this->_config = $config;
84 3
	}
85
86
	/**
87
	 * init the class
88
	 *
89
	 * @since 2.4.0
90
	 *
91
	 * @param array $moduleArray custom module setup
92
	 */
93
94 3
	public function init(array $moduleArray = []) : void
95
	{
96 3
		static::$_moduleArray = array_merge(static::$_moduleArray, $moduleArray);
97
98
		/* load the language */
99
100 3
		if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray))
101
		{
102 2
			$this->_language->load(
103
			[
104 2
				'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'en.json',
105 2
				'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $this->_registry->get('language') . '.json'
106
			]);
107
		}
108 3
	}
109
110
	/**
111
	 * install the module
112
	 *
113
	 * @since 2.6.0
114
	 *
115
	 * @return bool
116
	 */
117
118 2
	public function install() : bool
119
	{
120 2
		if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray))
121
		{
122 1
			$moduleModel = new Model\Module();
123 1
			$moduleModel->createByArray(static::$_moduleArray);
124
125
			/* create from sql */
126
127 1
			$directory = 'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'database';
128 1
			if (is_dir($directory))
129
			{
130 1
				$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
131 1
				$installer->init($directory);
132 1
				$installer->rawCreate();
133
			}
134 1
			$moduleModel->clearCache();
135 1
			return $moduleModel->query()->where('alias', static::$_moduleArray['alias'])->count() === 1;
136
		}
137 1
		return false;
138
	}
139
140
	/**
141
	 * uninstall the module
142
	 *
143
	 * @since 2.6.0
144
	 *
145
	 * @return bool
146
	 */
147
148 2
	public function uninstall() : bool
149
	{
150 2
		if (is_array(static::$_moduleArray) && array_key_exists('alias', static::$_moduleArray))
151
		{
152 1
			$moduleModel = new Model\Module();
153 1
			$moduleModel->deleteByAlias(static::$_moduleArray['alias']);
154
155
			/* drop from sql */
156
157 1
			$directory = 'modules' . DIRECTORY_SEPARATOR . static::$_moduleArray['alias'] . DIRECTORY_SEPARATOR . 'database';
158 1
			if (is_dir($directory))
159
			{
160 1
				$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
161 1
				$installer->init($directory);
162 1
				$installer->rawDrop();
163
			}
164 1
			$moduleModel->clearCache();
165 1
			return $moduleModel->query()->where('alias', static::$_moduleArray['alias'])->count() === 0;
166
		}
167 1
		return false;
168
	}
169
170
	/**
171
	 * reinstall the module
172
	 *
173
	 * @since 4.3.0
174
	 *
175
	 * @return bool
176
	 */
177
178
	public function reinstall() : bool
179
	{
180
		return $this->uninstall() && $this->install();
181
	}
182
}
183