Completed
Branchmaster (8f7741)
created

Breadcrumb   B

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 4

Complexity

Total Complexity 37

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 4
dl 0
loc 350
ccs 137
cts 137
cp 1
rs 8.6
c 0
b 0
f 0

8 Methods

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