Completed
Push — master ( 14c945...6811c1 )
by Henry
05:37
created

Category::getByAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace Redaxscript\Model;
3
4
use function array_filter;
5
use function implode;
6
use function is_array;
7
8
/**
9
 * parent class to provide the category model
10
 *
11
 * @since 3.3.0
12
 *
13
 * @package Redaxscript
14
 * @category Model
15
 * @author Henry Ruhs
16
 */
17
18
class Category extends ContentAbstract
19
{
20
	/**
21
	 * name of the table
22
	 *
23
	 * @var string
24
	 */
25
26
	protected $_table = 'categories';
27
28
	/**
29
	 * get the category by alias
30
	 *
31
	 * @since 4.0.0
32
	 *
33
	 * @param string $categoryAlias alias of the category
34
	 *
35
	 * @return object|null
36
	 */
37
38
	public function getByAlias(string $categoryAlias = null) : ?object
39
	{
40
		$category = $this->query()->where('alias', $categoryAlias)->findOne();
41
		return $category ? : null;
42
	}
43
44
	/**
45
	 * get the category route by id
46
	 *
47
	 * @since 3.3.0
48
	 *
49
	 * @param int $categoryId identifier of the category
50
	 *
51
	 * @return string|null
52
	 */
53
54
	public function getRouteById(int $categoryId = null) : ?string
55
	{
56
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57
		{
58
			$routeArray = $this
59
				->query()
60
				->tableAlias('category')
61
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
62
				->select('parent.alias', 'parentAlias')
63
				->select('category.alias', 'categoryAlias')
64
				->where('category.id', $categoryId)
65
				->findArray();
66
67
			/* handle route */
68
69
			if (is_array($routeArray[0]))
70
			{
71
				return implode('/', array_filter($routeArray[0]));
72
			}
73
		}
74
		return null;
75
	}
76
}
77