UserForm::render()   F
last analyzed

Complexity

Conditions 19
Paths 36

Size

Total Lines 269

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 131
CRAP Score 19

Importance

Changes 0
Metric Value
dl 0
loc 269
ccs 131
cts 131
cp 1
rs 3.6133
c 0
b 0
f 0
cc 19
nc 36
nop 1
crap 19

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