Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

UserTable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 0
loc 223
ccs 91
cts 98
cp 0.9286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 36 2
B _renderTable() 0 93 5
A _renderSession() 0 23 4
A _renderGroup() 0 29 3
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'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('users') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
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'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('user_new') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
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);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 119 can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
122 4
			$outputFoot .= $tdElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 119 can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
123
		}
124
125
		/* process categories */
126
127 4
		if ($usersTotal)
128
		{
129 4
			foreach ($users as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $users of type array|object<IdiormResultSet>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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))
0 ignored issues
show
Bug introduced by
It seems like $this->_renderGroup($value->groups) targeting Redaxscript\Admin\View\UserTable::_renderGroup() can also be of type array; however, Redaxscript\Html\HtmlAbstract::html() 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...
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'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('user_no') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
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'));
0 ignored issues
show
Bug introduced by
It seems like $this->_registry->get('now') targeting Redaxscript\Registry::get() can also be of type array or string; however, Redaxscript\Dater::init() does only seem to accept null|integer, 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...
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