Completed
Branch master (d2c4e1)
by
unknown
09:55 waited 07:09
created

Canonical::process()   B

↳ Parent: Canonical

Complexity

Conditions 4
Paths 6

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 29
Code Lines 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 15
nc 6
nop 0
1
<?php
2
namespace Redaxscript\Template\Helper;
3
4
use Redaxscript\Db;
5
6
/**
7
 * helper class to provide a canonical helper
8
 *
9
 * @since 3.0.0
10
 *
11
 * @package Redaxscript
12
 * @category Template
13
 * @author Henry Ruhs
14
 */
15
16
class Canonical extends HelperAbstract
17
{
18
	/**
19
	 * process
20
	 *
21
	 * @since 3.0.0
22
	 *
23
	 * @return string
24
	 */
25
26
	public function process()
27
	{
28
		$lastTable = $this->_registry->get('lastTable');
29
		$lastId = $this->_registry->get('lastId');
30
		$parameterRoute = $this->_registry->get('parameterRoute');
31
		$root = $this->_registry->get('root');
32
33
		/* find route */
34
35
		if ($lastTable === 'categories')
36
		{
37
			$articles = Db::forTablePrefix('articles')->where('category', $lastId);
38
			$articlesTotal = $articles->findMany()->count();
39
			if ($articlesTotal === 1)
40
			{
41
				$lastTable = 'articles';
42
				$lastId = $articles->findOne()->id;
43
			}
44
		}
45
		$canonicalRoute = build_route($lastTable, $lastId);
46
47
		/* handle route */
48
49
		if ($canonicalRoute)
50
		{
51
			return $root . '/' . $parameterRoute . $canonicalRoute;
52
		}
53
		return $root;
54
	}
55
}
56