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

Breadcrumb::_createAdmin()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 8.3306
c 0
b 0
f 0
cc 7
nc 10
nop 1
crap 7
1
<?php
2
namespace Redaxscript\View\Helper;
3
4
use Redaxscript\Db;
5
use Redaxscript\Html;
6
use Redaxscript\Model;
7
use Redaxscript\Module;
8
use Redaxscript\View\ViewAbstract;
9
use function array_key_exists;
10
use function array_keys;
11
use function array_replace_recursive;
12
use function end;
13
use function is_array;
14
15
/**
16
 * helper class to create a breadcrumb navigation
17
 *
18
 * @since 2.1.0
19
 *
20
 * @package Redaxscript
21
 * @category View
22
 * @author Henry Ruhs
23
 * @author Gary Aylward
24
 */
25
26
class Breadcrumb extends ViewAbstract
27
{
28
	/**
29
	 * array of the breadcrumb
30
	 *
31
	 * @var array
32
	 */
33
34
	protected $_breadcrumbArray = [];
35
36
	/**
37
	 * options of the breadcrumb
38
	 *
39
	 * @var array
40
	 */
41
42
	protected $_optionArray =
43
	[
44
		'className' =>
45
		[
46
			'list' => 'rs-list-breadcrumb',
47
			'divider' => 'rs-item-divider'
48
		],
49
		'divider' => null
50
	];
51
52
	/**
53
	 * stringify the breadcrumb
54
	 *
55
	 * @since 3.0.0
56
	 *
57
	 * @return string
58
	 */
59
60 2
	public function __toString() : string
61
	{
62 2
		return $this->render();
63
	}
64
65
	/**
66
	 * init the class
67
	 *
68
	 * @since 2.6.0
69
	 *
70
	 * @param array $optionArray options of the breadcrumb
71
	 *
72
	 * @return self
73 16
	 */
74
75 16
	public function init(array $optionArray = []) : self
76 16
	{
77 16
		$settingModel = new Model\Setting();
78
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
79 16
		if (!$this->_optionArray['divider'])
80
		{
81 16
			$this->_optionArray['divider'] = $settingModel->get('divider');
82 16
		}
83
		$this->_create();
84
		return $this;
85
	}
86
87
	/**
88
	 * get the breadcrumb array
89
	 *
90
	 * @since 2.1.0
91
	 *
92 14
	 * @return array
93
	 */
94 14
95
	public function getArray() : array
96
	{
97
		return $this->_breadcrumbArray;
98
	}
99
100
	/**
101
	 * render the breadcrumb
102
	 *
103
	 * @since 2.1.0
104
	 *
105 2
	 * @return string
106
	 */
107 2
108 2
	public function render() : string
109
	{
110
		$output = Module\Hook::trigger('breadcrumbStart');
111
		$parameterRoute = $this->_registry->get('parameterRoute');
112 2
113
		/* html element */
114 2
115 2
		$element = new Html\Element();
116
		$listElement = $element
117 2
			->copy()
118
			->init('ul',
119 2
			[
120 2
				'class' => $this->_optionArray['className']['list']
121
			]);
122
		$itemElement = $element->copy()->init('li');
123
		$linkElement = $element->copy()->init('a');
124 2
125 2
		/* breadcrumb keys */
126
127
		$breadcrumbKeys = array_keys($this->_breadcrumbArray);
128
		$lastKey = end($breadcrumbKeys);
129 2
130
		/* process breadcrumb */
131 2
132 2
		foreach ($this->_breadcrumbArray as $key => $valueArray)
133 2
		{
134
			$title = is_array($valueArray) && array_key_exists('title', $valueArray) ? $valueArray['title'] : null;
135 2
			$route = is_array($valueArray) && array_key_exists('route', $valueArray) ? $valueArray['route'] : null;
136
			if ($title)
137
			{
138
				$listElement->append(
139 2
					$itemElement
140
						->clear()
141 1
						->append(
142
							$route ? $linkElement
143 1
								->attr('href', $parameterRoute . $route)
144 1
								->text($title) : $title
145
						)
146
				);
147
148
				/* append divider */
149
150
				if ($key !== $lastKey && $this->_optionArray['divider'])
151
				{
152 2
					$listElement->append(
153
						$itemElement
154 2
							->copy()
155
							->addClass($this->_optionArray['className']['divider'])
156
							->text($this->_optionArray['divider'])
157
					);
158 2
				}
159
			}
160 1
		}
161
162 1
		/* collect list output */
163 1
164 1
		$output .= $listElement;
165
		$output .= Module\Hook::trigger('breadcrumbEnd');
166
		return $output;
167
	}
168
169
	/**
170
	 * create the breadcrumb array
171
	 *
172 2
	 * @since 2.1.0
173 2
	 *
174 2
	 * @param int $key key of the item
175
	 */
176
177
	protected function _create(int $key = 0) : void
178
	{
179
		$title = $this->_registry->get('useTitle');
180
		$firstParameter = $this->_registry->get('firstParameter');
181
		$secondParameter = $this->_registry->get('secondParameter');
182
		$firstTable = $this->_registry->get('firstTable');
183
		$fullRoute = $this->_registry->get('fullRoute');
184
		$lastId = $this->_registry->get('lastId');
185 16
186
		/* handle title */
187 16
188 16
		if ($title)
189 16
		{
190 16
			$this->_breadcrumbArray[$key]['title'] = $title;
191 16
		}
192 16
193
		/* handle home */
194
195
		else if (!$fullRoute)
196 16
		{
197
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('home');
198 1
		}
199
200
		/* handle administration */
201
202
		else if ($firstParameter === 'admin')
203 15
		{
204
			$this->_createAdmin($key);
205 1
		}
206
207
		/* handle login */
208
209
		else if ($firstParameter === 'login')
210 14
		{
211
			if ($secondParameter === 'recover')
212 2
			{
213
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('recovery');
214
			}
215
			else if ($secondParameter === 'reset')
216
			{
217 12
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('reset');
218
			}
219 3
			else
220
			{
221 1
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get('login');
222
			}
223 2
		}
224
225 1
		/* handle logout */
226
227
		else if ($firstParameter === 'logout')
228
		{
229 3
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('logout');
230
		}
231
232
		/* handle register */
233
234
		else if ($firstParameter === 'register')
235 9
		{
236
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('registration');
237
		}
238
239
		/* handle module */
240
241
		else if ($firstParameter === 'module')
242 9
		{
243
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('module');
244 1
		}
245
246
		/* handle search */
247
248
		else if ($firstParameter === 'search')
249 8
		{
250
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('search');
251 1
		}
252
253
		/* handle error */
254
255
		else if (!$lastId)
256 7
		{
257
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get('error');
258 1
		}
259
260
		/* handle content */
261
262
		else if ($firstTable)
263 6
		{
264
			$this->_createContent($key);
265 1
		}
266
	}
267
268
	/**
269
	 * create the breadcrumb array for the administration
270 5
	 *
271
	 * @since 2.1.0
272 5
	 *
273
	 * @param int $key key of the item
274 16
	 */
275
276
	protected function _createAdmin(int $key = 0) : void
277
	{
278
		$adminParameter = $this->_registry->get('adminParameter');
279
		$tableParameter = $this->_registry->get('tableParameter');
280
		$lastParameter = $this->_registry->get('lastParameter');
281
		$fullRoute = $this->_registry->get('fullRoute');
282
283
		/* join first title */
284 2
285
		$this->_breadcrumbArray[$key]['title'] = $this->_language->get('administration');
286 2
287 2
		/* admin parameter */
288 2
289 2
		if ($adminParameter)
290
		{
291
			$this->_breadcrumbArray[$key]['route'] = 'admin';
292
		}
293 2
294
		/* join admin title */
295
296
		if ($adminParameter && $this->_language->get($adminParameter))
0 ignored issues
show
Bug introduced by
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 278 can also be of type array; however, Redaxscript\Language::get() 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...
297 2
		{
298
			$key++;
299 1
			$this->_breadcrumbArray[$key]['title'] = $this->_language->get($adminParameter);
0 ignored issues
show
Bug introduced by
It seems like $adminParameter defined by $this->_registry->get('adminParameter') on line 278 can also be of type array; however, Redaxscript\Language::get() 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...
300
301
			/* set the route */
302
303
			if ($adminParameter !== $lastParameter)
304 2
			{
305
				$this->_breadcrumbArray[$key]['route'] = $fullRoute;
306 1
			}
307 1
308
			/* join table title */
309
310
			if ($tableParameter && $this->_language->get($tableParameter))
0 ignored issues
show
Bug introduced by
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 279 can also be of type array; however, Redaxscript\Language::get() 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...
311 1
			{
312
				$key++;
313 1
				$this->_breadcrumbArray[$key]['title'] = $this->_language->get($tableParameter);
0 ignored issues
show
Bug introduced by
It seems like $tableParameter defined by $this->_registry->get('tableParameter') on line 279 can also be of type array; however, Redaxscript\Language::get() 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...
314
			}
315
		}
316
	}
317
318 1
	/**
319
	 * create the breadcrumb array for the content
320 1
	 *
321 1
	 * @since 2.1.0
322
	 *
323
	 * @param int $key
324 2
	 */
325
326
	protected function _createContent(int $key = 0) : void
327
	{
328
		$firstParameter = $this->_registry->get('firstParameter');
329
		$secondParameter = $this->_registry->get('secondParameter');
330
		$thirdParameter = $this->_registry->get('thirdParameter');
331
		$lastParameter = $this->_registry->get('lastParameter');
332
		$firstTable = $this->_registry->get('firstTable');
333
		$secondTable = $this->_registry->get('secondTable');
334 5
		$thirdTable = $this->_registry->get('thirdTable');
335
336 5
		/* join first title */
337 5
338 5
		$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($firstTable)->where('alias', $firstParameter)->findOne()->title;
339 5
340 5
		/* set the route */
341 5
342 5
		if ($firstParameter !== $lastParameter)
343
		{
344
			$this->_breadcrumbArray[$key]['route'] = $firstParameter;
345
		}
346 5
347
		/* join second title */
348
349
		if ($secondTable)
350 5
		{
351
			$key++;
352 3
			$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($secondTable)->where('alias', $secondParameter)->findOne()->title;
353
354
			/* set the route */
355
356
			if ($secondParameter !== $lastParameter)
357 5
			{
358
				$this->_breadcrumbArray[$key]['route'] = $firstParameter . '/' . $secondParameter;
359 3
			}
360 3
361
			/* join third title */
362
363
			if ($thirdTable)
364 3
			{
365
				$key++;
366 1
				$this->_breadcrumbArray[$key]['title'] = Db::forTablePrefix($thirdTable)->where('alias', $thirdParameter)->findOne()->title;
367
			}
368
		}
369
	}
370
}
371