Article   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 113
c 0
b 0
f 0
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getByAlias() 0 4 2
A countByCategoryAndLanguage() 0 12 2
A getSiblingByCategoryAndLanguageAndOrderAndStep() 0 15 3
A getRouteById() 0 24 3
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 article model
10
 *
11
 * @since 3.3.0
12
 *
13
 * @package Redaxscript
14
 * @category Model
15
 * @author Henry Ruhs
16
 */
17
18
class Article extends ContentAbstract
19
{
20
	/**
21
	 * name of the table
22
	 *
23
	 * @var string
24
	 */
25
26
	protected $_table = 'articles';
27
28
	/**
29
	 * get the article by alias
30
	 *
31
	 * @since 4.0.0
32
	 *
33
	 * @param string $articleAlias alias of the article
34
	 *
35
	 * @return object|null
36
	 */
37
38 2
	public function getByAlias(string $articleAlias = null) : ?object
39
	{
40 2
		return $this->query()->where('alias', $articleAlias)->findOne() ? : null;
41
	}
42
43
	/**
44
	 * count the articles by category and language
45
	 *
46
	 * @since 4.0.0
47
	 *
48
	 * @param int $categoryId identifier of the category
49
	 * @param string $language
50
	 *
51
	 * @return int|null
52
	 */
53
54 3
	public function countByCategoryAndLanguage(int $categoryId = null, string $language = null) : ?int
55
	{
56
		return $this
57 3
			->query()
58 3
			->where(
59
			[
60 3
				'category' => $categoryId,
61 3
				'status' => 1
62
			])
63 3
			->whereLanguageIs($language)
64 3
			->count() ? : null;
65
	}
66
67
	/**
68
	 * get the siblings by category and language and order and step
69
	 *
70
	 * @since 4.0.0
71
	 *
72
	 * @param int $categoryId identifier of the category
73
	 * @param string $language
74
	 * @param string $orderColumn
75
	 * @param int $limitStep
76
	 *
77
	 * @return object|null
78
	 */
79
80 5
	public function getSiblingByCategoryAndLanguageAndOrderAndStep(int $categoryId = null, string $language = null, string $orderColumn = 'rank', int $limitStep = null) : ?object
81
	{
82 5
		$categoryModel = new Category();
83
		$query = $this
84 5
			->query()
85 5
			->whereIn('category', $categoryModel->getSiblingArrayById($categoryId))
86 5
			->where('status', 1)
87 5
			->whereLanguageIs($language)
88 5
			->orderBySetting($orderColumn);
89 5
		if ($limitStep)
0 ignored issues
show
Bug Best Practice introduced by
The expression $limitStep 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...
90
		{
91 1
			$query->limitBySetting($limitStep);
92
		}
93 5
		return $query->findMany() ? : null;
94
	}
95
96
	/**
97
	 * get the article route by id
98
	 *
99
	 * @since 3.3.0
100
	 *
101
	 * @param int $articleId identifier of the article
102
	 *
103
	 * @return string|null
104
	 */
105
106 6
	public function getRouteById(int $articleId = null) : ?string
107
	{
108 6
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $articleId 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...
109
		{
110
			$routeArray = $this
111 5
				->query()
112 5
				->tableAlias('article')
113 5
				->leftJoinPrefix('categories', 'article.category = category.id', 'category')
114 5
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
115 5
				->select('parent.alias', 'parentAlias')
116 5
				->select('category.alias', 'categoryAlias')
117 5
				->select('article.alias', 'articleAlias')
118 5
				->where('article.id', $articleId)
119 5
				->findArray();
120
121
			/* handle route */
122
123 5
			if (is_array($routeArray[0]))
124
			{
125 5
				return implode('/', array_filter($routeArray[0]));
126
			}
127
		}
128 1
		return null;
129
	}
130
}
131