Comment   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.78%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 151
ccs 39
cts 46
cp 0.8478
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A countByArticleAndLanguage() 0 12 2
A getByArticleAndLanguageAndOrderAndStep() 0 17 3
A maxIdByArticleAndLanguage() 0 12 2
A getRouteById() 0 25 3
A createByArray() 0 15 2
1
<?php
2
namespace Redaxscript\Model;
3
4
use PDOException;
5
use function array_filter;
6
use function implode;
7
use function is_array;
8
9
/**
10
 * parent class to provide the comment model
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Model
16
 * @author Henry Ruhs
17
 */
18
19
class Comment extends ContentAbstract
20
{
21
	/**
22
	 * name of the table
23
	 *
24
	 * @var string
25
	 */
26
27
	protected $_table = 'comments';
28
29
	/**
30
	 * count the comments by article and language
31
	 *
32
	 * @since 4.0.0
33
	 *
34
	 * @param int $articleId identifier of the article
35
	 * @param string $language
36
	 *
37
	 * @return int|null
38
	 */
39
40 4
	public function countByArticleAndLanguage(int $articleId = null, string $language = null) : ?int
41
	{
42
		return $this
43 4
			->query()
44 4
			->where(
45
			[
46 4
				'article' => $articleId,
47 4
				'status' => 1
48
			])
49 4
			->whereLanguageIs($language)
50 4
			->count() ? : null;
51
	}
52
53
	/**
54
	 * max id by article and language
55
	 *
56
	 * @since 4.5.0
57
	 *
58
	 * @param int $articleId identifier of the article
59
	 * @param string $language
60
	 *
61
	 * @return int|null
62
	 */
63
64
	public function maxIdByArticleAndLanguage(int $articleId = null, string $language = null) : ?int
65
	{
66
		return $this
67
			->query()
68
			->where(
69
			[
70
				'article' => $articleId,
71
				'status' => 1
72
			])
73
			->whereLanguageIs($language)
74
			->max('id') ? : null;
75
	}
76
77
	/**
78
	 * get the comments by article and language and order and step
79
	 *
80
	 * @since 4.0.0
81
	 *
82
	 * @param int $articleId identifier of the article
83
	 * @param string $language
84
	 * @param string $orderColumn
85
	 * @param int $limitStep
86
	 *
87
	 * @return object|null
88
	 */
89
90 5
	public function getByArticleAndLanguageAndOrderAndStep(int $articleId = null, string $language = null, string $orderColumn = 'rank', int $limitStep = null) : ?object
91
	{
92
		$query = $this
93 5
			->query()
94 5
			->where(
95
			[
96 5
				'article' => $articleId,
97 5
				'status' => 1
98
			])
99 5
			->whereLanguageIs($language)
100 5
			->orderBySetting($orderColumn);
101 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...
102
		{
103 1
			$query->limitBySetting($limitStep);
104
		}
105 5
		return $query->findMany() ? : null;
106
	}
107
108
	/**
109
	 * get the comment route by id
110
	 *
111
	 * @since 3.3.0
112
	 *
113
	 * @param int $commentId identifier of the comment
114
	 *
115
	 * @return string|null
116
	 */
117
118 4
	public function getRouteById(int $commentId = null) : ?string
119
	{
120 4
		if ($commentId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $commentId 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...
121
		{
122
			$routeArray = $this
123 3
				->query()
124 3
				->tableAlias('comment')
125 3
				->leftJoinPrefix('articles', 'comment.article = article.id', 'article')
126 3
				->leftJoinPrefix('categories', 'article.category = category.id', 'category')
127 3
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
128 3
				->select('parent.alias', 'parentAlias')
129 3
				->select('category.alias', 'categoryAlias')
130 3
				->select('article.alias', 'articleAlias')
131 3
				->where('comment.id', $commentId)
132 3
				->findArray();
133
134
			/* handle route */
135
136 3
			if (is_array($routeArray[0]))
137
			{
138 3
				return implode('/', array_filter($routeArray[0])) . '#comment-' . $commentId;
139
			}
140
		}
141 1
		return null;
142
	}
143
144
	/**
145
	 * create the comment by array
146
	 *
147
	 * @since 3.3.0
148
	 *
149
	 * @param array $createArray array of the create
150
	 *
151
	 * @return bool
152
	 */
153
154 2
	public function createByArray(array $createArray = []) : bool
155
	{
156
		try
157
		{
158
			return $this
159 2
				->query()
160 2
				->create()
161 2
				->set($createArray)
162 2
				->save();
163
		}
164 1
		catch (PDOException $exception)
165
		{
166 1
			return false;
167
		}
168
	}
169
}
170