Completed
Push — master ( 0d72ed...db6a9f )
by Henry
14:21 queued 04:57
created

includes/Model/Comment.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * get the articles by category and language and order and step
55
	 *
56
	 * @since 4.0.0
57
	 *
58
	 * @param int $articleId identifier of the article
59
	 * @param string $language
60
	 * @param string $orderColumn
61
	 * @param int $limitStep
62
	 *
63
	 * @return object|null
64
	 */
65
66 5
	public function getByArticleAndLanguageAndOrderAndStep(int $articleId = null, string $language = null, string $orderColumn = 'rank', int $limitStep = null) : ?object
67
	{
68
		$query = $this
69 5
			->query()
70 5
			->where(
71
			[
72 5
				'article' => $articleId,
73 5
				'status' => 1
74
			])
75 5
			->whereLanguageIs($language)
76 5
			->orderBySetting($orderColumn);
77 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...
78
		{
79 1
			$query->limitBySetting($limitStep);
80
		}
81 5
		return $query->findMany() ? : null;
82
	}
83
84
	/**
85
	 * get the comment route by id
86
	 *
87
	 * @since 3.3.0
88
	 *
89
	 * @param int $commentId identifier of the comment
90
	 *
91
	 * @return string|null
92
	 */
93
94 4
	public function getRouteById(int $commentId = null) : ?string
95
	{
96 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...
97
		{
98
			$routeArray = $this
99 3
				->query()
100 3
				->tableAlias('comment')
101 3
				->leftJoinPrefix('articles', 'comment.article = article.id', 'article')
102 3
				->leftJoinPrefix('categories', 'article.category = category.id', 'category')
103 3
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
104 3
				->select('parent.alias', 'parentAlias')
105 3
				->select('category.alias', 'categoryAlias')
106 3
				->select('article.alias', 'articleAlias')
107 3
				->where('comment.id', $commentId)
108 3
				->findArray();
109
110
			/* handle route */
111
112 3
			if (is_array($routeArray[0]))
113
			{
114 3
				return implode('/', array_filter($routeArray[0])) . '#comment-' . $commentId;
115
			}
116
		}
117 1
		return null;
118
	}
119
120
	/**
121
	 * create the comment by array
122
	 *
123
	 * @since 3.3.0
124
	 *
125
	 * @param array $createArray array of the create
126
	 *
127
	 * @return bool
128
	 */
129
130 2
	public function createByArray(array $createArray = []) : bool
131
	{
132
		try
133
		{
134
			return $this
135 2
				->query()
136 2
				->create()
137 2
				->set($createArray)
138 2
				->save();
139
		}
140 1
		catch (PDOException $exception)
141
		{
142 1
			return false;
143
		}
144
	}
145
}
146