Completed
Push — master ( 115248...91d2c9 )
by Henry
78:30 queued 58:40
created

Breadcrumb::_create()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 13.0051

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 31
cts 32
cp 0.9688
rs 5.5115
c 0
b 0
f 0
cc 13
nc 13
nop 1
crap 13.0051

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