Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

modules/Demo/Demo.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\Demo;
3
4
use Redaxscript\Auth;
5
use Redaxscript\Db;
6
use Redaxscript\Installer;
7
use Redaxscript\Module;
8
use Redaxscript\View;
9
use function is_dir;
10
11
/**
12
 * enable anonymous login
13
 *
14
 * @since 2.2.0
15
 *
16
 * @package Redaxscript
17
 * @category Modules
18
 * @author Henry Ruhs
19
 */
20
21
class Demo extends Module\Notification
22
{
23
	/**
24
	 * array of the module
25
	 *
26
	 * @var array
27
	 */
28
29
	protected static $_moduleArray =
30
	[
31
		'name' => 'Demo',
32
		'alias' => 'Demo',
33
		'author' => 'Redaxmedia',
34
		'description' => 'Enable demo login',
35
		'version' => '4.0.0'
36
	];
37
38
	/**
39
	 * array of the option
40
	 *
41
	 * @var array
42
	 */
43
44
	protected $_optionArray =
45
	[
46
		'modules' =>
47
		[
48
			'Analytics' => 'Redaxscript\Modules\Analytics\Analytics',
49
			'Demo' => 'Redaxscript\Modules\Demo\Demo',
50
			'Editor' => 'Redaxscript\Modules\Editor\Editor'
51
		]
52
	];
53
54
	/**
55
	 * renderStart
56
	 *
57
	 * @since 4.0.0
58
	 */
59
60
	public function renderStart() : void
61
	{
62
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'demo' && $this->_registry->get('thirdParameter') === 'reinstall')
63
		{
64
			$this->_registry->set('renderBreak', true);
65
			$this->_reinstall();
66
		}
67
	}
68
69
	/**
70
	 * routeHeader
71
	 *
72
	 * @since 3.3.0
73
	 */
74
75
	public function routeHeader() : void
76
	{
77
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'demo' && $this->_registry->get('thirdParameter') === 'login')
78
		{
79
			$this->_registry->set('useTitle', $this->_language->get('login'));
80
			$this->_registry->set('routerBreak', true);
81
		}
82
	}
83
84
	/**
85
	 * routeContent
86
	 *
87
	 * @since 3.3.0
88
	 */
89
90
	public function routeContent() : void
91
	{
92
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'demo' && $this->_registry->get('thirdParameter') === 'login')
93
		{
94
			echo $this->process();
95
		}
96
	}
97
98
	/**
99
	 * adminNotification
100
	 *
101
	 * @since 3.0.0
102
	 *
103
	 * @return array|null
104
	 */
105
106
	public function adminNotification() : ?array
107
	{
108
		$auth = new Auth($this->_request);
109
		$auth->init();
110
111
		/* demo user */
112
113
		if ($auth->getUser('user') === 'demo')
114
		{
115
			$this->setNotification('success', $this->_language->get('logged_in') . $this->_language->get('point'));
116
		}
117
		return $this->getNotification();
118
	}
119
120
	/**
121
	 * process
122
	 *
123
	 * @since 3.0.0
124
	 *
125
	 * @return string
126
	 */
127
128
	public function process() : string
129
	{
130
		$auth = new Auth($this->_request);
131
		$tableArray =
132
		[
133
			'categories',
134
			'articles',
135
			'extras',
136
			'comments',
137
			'groups',
138
			'users'
139
		];
140
141
		/* set the user */
142
143
		$auth->setUser('name', 'Demo');
144
		$auth->setUser('user', 'demo');
145
		$auth->setUser('email', 'demo@localhost');
146
147
		/* set the permission */
148
149
		foreach ($tableArray as $value)
150
		{
151
			$auth->setPermission($value,
152
			[
153
				1,
154
				2,
155
				3
156
			]);
157
		}
158
		$auth->setPermission('settings',
159
		[
160
			1
161
		]);
162
163
		/* save user and permission */
164
165
		$auth->save();
166
167
		/* handle success */
168
169
		if ($auth->getStatus())
170
		{
171
			return $this->_success();
172
		}
173
		return $this->_error();
174
	}
175
176
	/**
177
	 * messenger factory
178
	 *
179
	 * @since 4.0.0
180
	 *
181
	 * @return View\Helper\Messenger
182
	 */
183
184
	protected function _messengerFactory() : View\Helper\Messenger
185
	{
186
		return new View\Helper\Messenger($this->_registry);
187
	}
188
189
	/**
190
	 * success
191
	 *
192
	 * @since 3.0.0
193
	 *
194
	 * @return string
195
	 */
196
197
	protected function _success() : string
198
	{
199
		$messenger = $this->_messengerFactory();
200
		$route = $this->_request->getQuery('redirect') ? : 'admin';
201
		return $messenger
202
			->setRoute($this->_language->get('continue'), $route)
0 ignored issues
show
It seems like $this->_language->get('continue') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::setRoute() 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...
It seems like $route defined by $this->_request->getQuery('redirect') ?: 'admin' on line 200 can also be of type array; however, Redaxscript\View\Helper\Messenger::setRoute() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
203
			->doRedirect(0)
204
			->success($this->_language->get('logged_in'), $this->_language->get('welcome'));
0 ignored issues
show
It seems like $this->_language->get('welcome') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::success() 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...
205
	}
206
207
	/**
208
	 * error
209
	 *
210
	 * @since 3.0.0
211
	 *
212
	 * @return string
213
	 */
214
215
	protected function _error() : string
216
	{
217
		$messenger = $this->_messengerFactory();
218
		return $messenger
219
			->setRoute($this->_language->get('back'), 'login')
0 ignored issues
show
It seems like $this->_language->get('back') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::setRoute() 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...
220
			->error($this->_language->get('something_wrong'), $this->_language->get('error_occurred'));
0 ignored issues
show
It seems like $this->_language->get('error_occurred') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::error() 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...
221
	}
222
223
	/**
224
	 * reinstall
225
	 *
226
	 * @since 2.4.0
227
	 */
228
229
	protected function _reinstall() : void
230
	{
231
		$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
232
		$installer->init();
233
		$installer->rawDrop();
234
		$installer->rawCreate();
235
		$installer->insertData(
236
		[
237
			'adminName' => 'Admin',
238
			'adminUser' => 'admin',
239
			'adminPassword' => 'admin',
240
			'adminEmail' => 'admin@localhost'
241
		]);
242
243
		/* process modules */
244
245
		foreach ($this->_optionArray['modules'] as $key => $moduleClass)
246
		{
247
			if (is_dir('modules/' . $key))
248
			{
249
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
250
				$module->install();
251
			}
252
		}
253
254
		/* access and filter */
255
256
		Db::forTablePrefix('groups')
257
			->findMany()
258
			->set(
259
			[
260
				'modules' => null,
261
				'filter' => 1
262
			])
263
			->save();
264
	}
265
}
266