Completed
Push — master ( f598b1...764fcb )
by Henry
117:58
created

Comment::getByArticleAndLanguageAndOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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