UserTable::_renderTable()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 5.0209

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 48
cts 53
cp 0.9057
rs 7.8416
c 0
b 0
f 0
cc 5
nc 4
nop 0
crap 5.0209

How to fix   Long Method   

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\Dater;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use function count;
9
use function json_decode;
10
11
/**
12
 * children class to create the admin user table
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class UserTable extends ViewAbstract
22
{
23
	/**
24
	 * render the view
25
	 *
26
	 * @since 4.0.0
27
	 *
28
	 * @return string
29
	 */
30
31 4
	public function render() : string
32
	{
33 4
		$output = Module\Hook::trigger('adminUserTableStart');
34 4
		$parameterRoute = $this->_registry->get('parameterRoute');
35 4
		$usersNew = $this->_registry->get('usersNew');
36
37
		/* html element */
38
39 4
		$element = new Html\Element();
40
		$titleElement = $element
41 4
			->copy()
42 4
			->init('h2',
43
			[
44 4
				'class' => 'rs-admin-title-content',
45
			])
46 4
			->text($this->_language->get('users'));
47
		$linkElement = $element
48 4
			->copy()
49 4
			->init('a',
50
			[
51 4
				'class' => 'rs-admin-button-default rs-admin-button-create',
52 4
				'href' => $parameterRoute . 'admin/new/users'
53
			])
54 4
			->text($this->_language->get('user_new'));
55
56
		/* collect output */
57
58 4
		$output .= $titleElement;
59 4
		if ($usersNew)
60
		{
61 1
			$output .= $linkElement;
62
		}
63 4
		$output .= $this->_renderTable();
64 4
		$output .= Module\Hook::trigger('adminUserTableEnd');
65 4
		return $output;
66
	}
67
68
	/**
69
	 * render the table
70
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @return string|null
74
	 */
75
76 4
	protected function _renderTable() : ?string
77
	{
78 4
		$output = null;
79 4
		$outputHead = null;
80 4
		$outputBody = null;
81 4
		$outputFoot = null;
82
		$tableArray =
83
		[
84 4
			'name' => $this->_language->get('name'),
85 4
			'user' => $this->_language->get('user'),
86 4
			'session' => $this->_language->get('session'),
87 4
			'groups' => $this->_language->get('groups')
88
		];
89 4
		$adminControl = new Helper\Control($this->_registry, $this->_language);
90 4
		$adminControl->init();
91 4
		$userModel = new Admin\Model\User();
92 4
		$users = $userModel->getAll();
93 4
		$usersTotal = $users->count();
94
95
		/* html element */
96
97 4
		$element = new Html\Element();
98
		$wrapperElement = $element
99 4
			->copy()
100 4
			->init('div',
101
			[
102 4
				'class' => 'rs-admin-wrapper-table'
103
			]);
104
		$tableElement = $element
105 4
			->copy()
106 4
			->init('table',
107
			[
108 4
				'class' => 'rs-admin-table-default rs-admin-table-user'
109
			]);
110 4
		$theadElement = $element->copy()->init('thead');
111 4
		$tbodyElement = $element->copy()->init('tbody');
112 4
		$tfootElement = $element->copy()->init('tfoot');
113 4
		$trElement = $element->copy()->init('tr');
114 4
		$thElement = $element->copy()->init('th');
115 4
		$tdElement = $element->copy()->init('td');
116
117
		/* process table */
118
119 4
		foreach ($tableArray as $key => $value)
120
		{
121 4
			$outputHead .= $thElement->copy()->text($value);
122 4
			$outputFoot .= $tdElement->copy()->text($value);
123
		}
124
125
		/* process categories */
126
127 4
		if ($usersTotal)
128
		{
129 4
			foreach ($users as $key => $value)
130
			{
131
				$outputBody .= $trElement
132 4
					->copy()
133 4
					->attr('id', 'row-' . $value->id)
134 4
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
135 4
					->html(
136 4
						$tdElement->copy()->html($value->name . $adminControl->render('users', $value->id, $value->alias, $value->status)) .
137 4
						$tdElement->copy()->text($value->user) .
138 4
						$tdElement->copy()->text($this->_renderSession($value->last)) .
139 4
						$tdElement->copy()->html($this->_renderGroup($value->groups))
140
					);
141
			}
142
		}
143
		else
144
		{
145
			$outputBody .= $trElement
146
				->copy()
147
				->html(
148
					$tdElement
149
						->copy()
150
						->attr('colspan', count($tableArray))
151
						->text($this->_language->get('user_no'))
152
				);
153
		}
154
155
		/* collect output */
156
157 4
		$outputHead = $theadElement->html(
158 4
			$trElement->html($outputHead)
159
		);
160 4
		$outputBody = $tbodyElement->html($outputBody);
161 4
		$outputFoot = $tfootElement->html(
162 4
			$trElement->html($outputFoot)
163
		);
164 4
		$output .= $wrapperElement->copy()->html(
165 4
			$tableElement->html($outputHead . $outputBody . $outputFoot)
166
		);
167 4
		return $output;
168
	}
169
170
	/**
171
	 * render the session
172
	 *
173
	 * @since 4.0.0
174
	 *
175
	 * @param string $last
176
	 *
177
	 * @return string
178
	 */
179
180 4
	protected function _renderSession(string $last = null) : string
181
	{
182 4
		$daterLast = new Dater();
183 4
		$daterLast->init($last);
184 4
		$daterNow = new Dater();
185 4
		$daterNow->init($this->_registry->get('now'));
186
187
		/* handle session */
188
189 4
		if (!$last)
0 ignored issues
show
Bug Best Practice introduced by
The expression $last of type null|string 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...
190
		{
191
			return $this->_language->get('session_no');
192
		}
193 4
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('-1 minute'))
194
		{
195 2
			return $this->_language->get('online');
196
		}
197 2
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('+1 minute -1 day'))
198
		{
199 1
			return $this->_language->get('today') . ' ' . $this->_language->get('at') . ' ' . $daterLast->formatTime();
200
		}
201 1
		return $daterLast->formatDate();
202
	}
203
204
	/**
205
	 * render the group
206
	 *
207
	 * @since 4.0.0
208
	 *
209
	 * @param string groups
210
	 *
211
	 * @return string|null
212
	 */
213
214 4
	protected function _renderGroup(string $groups = null) : ?string
215
	{
216 4
		$output = null;
217 4
		$groupModel = new Admin\Model\Group();
218 4
		$groupArray = (array)json_decode($groups);
219
220
		/* html element */
221
222 4
		$linkElement = new Html\Element();
223 4
		$linkElement->init('a');
224
225
		/* process groups */
226
227 4
		if ($groupArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
228
		{
229 4
			foreach ($groupArray as $groupId)
230
			{
231
				$output .= $linkElement
232 4
					->copy()
233 4
					->attr('href', $this->_registry->get('parameterRoute') . 'admin/edit/groups/' . $groupId)
234 4
					->text($groupModel->getById($groupId)->name);
235
			}
236
		}
237
		else
238
		{
239
			$output = $this->_language->get('none');
240
		}
241 4
		return $output;
242
	}
243
}
244