UserForm::render()   F
last analyzed

Complexity

Conditions 20
Paths 36

Size

Total Lines 285

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
dl 0
loc 285
ccs 0
cts 126
cp 0
rs 3.3333
c 0
b 0
f 0
cc 20
nc 36
nop 1
crap 420

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
use function count;
9
use function json_decode;
10
11
/**
12
 * children class to create the user form
13
 *
14
 * @since 3.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class UserForm extends ViewAbstract
22
{
23
	/**
24
	 * render the view
25
	 *
26
	 * @since 3.0.0
27
	 *
28
	 * @param int $userId identifier of the user
29
	 *
30
	 * @return string
31
	 */
32
33
	public function render(int $userId = null) : string
34
	{
35
		$output = Module\Hook::trigger('adminUserFormStart');
36
		$userModel = new Admin\Model\User();
37
		$user = $userModel->getById($userId);
38
		$loginValidator = new Validator\Login();
39
		$helperOption = new Helper\Option($this->_language);
40
41
		/* html element */
42
43
		$titleElement = new Html\Element();
44
		$titleElement
45
			->init('h2',
46
			[
47
				'class' => 'rs-admin-title-content',
48
			])
49
			->text($user->name ? : $this->_language->get('user_new'));
50
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
51
		$formElement->init(
52
		[
53
			'form' =>
54
			[
55
				'class' => 'rs-admin-js-validate rs-admin-fn-tab rs-admin-component-tab rs-admin-form-default'
56
			],
57
			'button' =>
58
			[
59
				'create' =>
60
				[
61
					'name' => self::class
62
				],
63
				'save' =>
64
				[
65
					'name' => self::class
66
				]
67
			],
68
			'link' =>
69
			[
70
				'cancel' =>
71
				[
72
					'href' => $this->_registry->get('usersEdit') && $this->_registry->get('usersDelete') ? $this->_registry->get('parameterRoute') . 'admin/view/users' : $this->_registry->get('parameterRoute') . 'admin'
73
				],
74
				'delete' =>
75
				[
76
					'href' => $user->id ? $this->_registry->get('parameterRoute') . 'admin/delete/users/' . $user->id . '/' . $this->_registry->get('token') : null
77
				]
78
			]
79
		]);
80
81
		/* create the form */
82
83
		$formElement
84
85
			/* user */
86
87
			->radio(
88
			[
89
				'id' => self::class . '\User',
90
				'class' => 'rs-admin-fn-status-tab',
91
				'name' => self::class . '\Tab',
92
				'checked' => 'checked'
93
			])
94
			->label($this->_language->get('user'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('user') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
95
			[
96
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
97
				'for' => self::class . '\User'
98
			])
99
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
100
			->label($this->_language->get('name'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('name') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
101
			[
102
				'for' => 'name'
103
			])
104
			->text(
105
			[
106
				'autofocus' => 'autofocus',
107
				'id' => 'name',
108
				'name' => 'name',
109
				'required' => 'required',
110
				'value' => $user->name
111
			])
112
			->append('</li>');
113
		if (!$user->id)
114
		{
115
			$formElement
116
				->append('<li>')
117
				->label($this->_language->get('user'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('user') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
118
				[
119
					'for' => 'user'
120
				])
121
				->text(
122
				[
123
					'id' => 'user',
124
					'name' => 'user',
125
					'pattern' => $loginValidator->getFormPattern(),
126
					'required' => 'required',
127
					'value' => $user->user
128
				])
129
				->append('</li>');
130
		}
131
		$formElement
132
			->append('<li>')
133
			->label($this->_language->get('description'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('description') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
134
			[
135
				'for' => 'description'
136
			])
137
			->textarea(
138
			[
139
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
140
				'id' => 'description',
141
				'name' => 'description',
142
				'rows' => 1,
143
				'value' => $user->description
144
			])
145
			->append('</li><li>')
146
			->label($this->_language->get('password'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('password') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
147
			[
148
				'for' => 'password'
149
			])
150
			->password(!$user->id ?
151
			[
152
				'id' => 'password',
153
				'pattern' => $loginValidator->getFormPattern(),
154
				'name' => 'password',
155
				'autocomplete' => 'new-password',
156
				'required' => 'required'
157
			] :
158
			[
159
				'id' => 'password',
160
				'pattern' => $loginValidator->getFormPattern(),
161
				'name' => 'password',
162
				'autocomplete' => 'new-password'
163
			])
164
			->append('</li><li>')
165
			->label($this->_language->get('password_confirm'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('password_confirm') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
166
			[
167
				'for' => 'password_confirm'
168
			])
169
			->password(!$user->id ?
170
			[
171
				'id' => 'password_confirm',
172
				'pattern' => $loginValidator->getFormPattern(),
173
				'name' => 'password_confirm',
174
				'autocomplete' => 'new-password',
175
				'required' => 'required'
176
			] :
177
			[
178
				'id' => 'password_confirm',
179
				'pattern' => $loginValidator->getFormPattern(),
180
				'name' => 'password_confirm',
181
				'autocomplete' => 'new-password'
182
			])
183
			->append('</li><li>')
184
			->label($this->_language->get('email'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('email') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
185
			[
186
				'for' => 'email'
187
			])
188
			->email(
189
			[
190
				'id' => 'email',
191
				'name' => 'email',
192
				'required' => 'required',
193
				'value' => $user->email
194
			])
195
			->append('</li></ul>')
196
197
			/* general */
198
199
			->radio(
200
			[
201
				'id' => self::class . '\General',
202
				'class' => 'rs-admin-fn-status-tab',
203
				'name' => self::class . '\Tab'
204
			])
205
			->label($this->_language->get('general'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('general') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
206
			[
207
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
208
				'for' => self::class . '\General'
209
			])
210
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
211
			->label($this->_language->get('language'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('language') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
212
			[
213
				'for' => 'language'
214
			])
215
			->select($helperOption->getLanguageArray(),
216
			[
217
				$user->language
218
			],
219
			[
220
				'id' => 'language',
221
				'name' => 'language'
222
			])
223
			->append('</li></ul>');
224
		if (!$user->id || $user->id > 1)
225
		{
226
			$formElement
227
228
				/* customize */
229
230
				->radio(
231
				[
232
					'id' => self::class . '\Customize',
233
					'class' => 'rs-admin-fn-status-tab',
234
					'name' => self::class . '\Tab'
235
				])
236
				->label($this->_language->get('customize'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('customize') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
237
				[
238
					'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
239
					'for' => self::class . '\Customize'
240
				])
241
				->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
242
				->label($this->_language->get('status'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('status') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
243
				[
244
					'for' => 'status'
245
				])
246
				->checkbox(!$user->id || $user->status ?
247
				[
248
					'id' => 'status',
249
					'class' => 'rs-admin-fn-status-switch',
250
					'name' => 'status',
251
					'checked' => 'checked'
252
				] :
253
				[
254
					'id' => 'status',
255
					'class' => 'rs-admin-fn-status-switch',
256
					'name' => 'status'
257
				])
258
				->label(null,
259
				[
260
					'class' => 'rs-admin-label-switch',
261
					'for' => 'status',
262
					'data-on' => $this->_language->get('enable'),
263
					'data-off' => $this->_language->get('disable')
264
				])
265
				->append('</li>');
266
			if ($this->_registry->get('groupsEdit'))
267
			{
268
				$formElement
269
					->append('<li>')
270
					->label($this->_language->get('groups'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('groups') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
271
					[
272
						'for' => 'groups'
273
					])
274
					->select($helperOption->getGroupArray(),
275
					(array)json_decode($user->groups),
276
					[
277
						'id' => 'groups',
278
						'name' => 'groups[]',
279
						'multiple' => 'multiple',
280
						'size' => count($helperOption->getGroupArray())
281
					])
282
					->append('</li>');
283
			}
284
			$formElement->append('</ul>');
285
		}
286
		$formElement
287
			->hidden(
288
			[
289
				'name' => 'id',
290
				'value' => $user->id
291
			])
292
			->token()
293
			->append('<div class="rs-admin-wrapper-button">')
294
			->cancel();
295
		if ($user->id)
296
		{
297
			if (($this->_registry->get('usersDelete') || $this->_registry->get('myId') === $user->id) && $user->id > 1)
298
			{
299
				$formElement->delete();
300
			}
301
			if ($this->_registry->get('usersEdit') || $this->_registry->get('myId') === $user->id)
302
			{
303
				$formElement->save();
304
			}
305
		}
306
		else if ($this->_registry->get('usersNew'))
307
		{
308
			$formElement->create();
309
		}
310
		$formElement->append('</div>');
311
312
		/* collect output */
313
314
		$output .= $titleElement . $formElement;
315
		$output .= Module\Hook::trigger('adminUserFormEnd');
316
		return $output;
317
	}
318
}
319