Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

SystemLogQuery::buildQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 17
ccs 14
cts 14
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\SystemLog;
4
5
use Doctrine\DBAL\Query\Expression\CompositeExpression;
6
use Elgg\Database\QueryBuilder;
7
use Elgg\Database\Repository;
8
use Elgg\Database\Select;
9
use Elgg\Values;
10
11
/**
12
 * System Log database table query
13
 */
14
class SystemLogQuery extends Repository {
15
16
	/**
17
	 * @var int[]
18
	 */
19
	public $id;
20
21
	/**
22
	 * @var int[]
23
	 */
24
	public $object_id;
25
26
	/**
27
	 * @var string[]
28
	 */
29
	public $object_class;
30
31
	/**
32
	 * @var string[]
33
	 */
34
	public $object_type;
35
36
	/**
37
	 * @var string[]
38
	 */
39
	public $object_subtype;
40
41
	/**
42
	 * @var string[]
43
	 */
44
	public $event;
45
46
	/**
47
	 * @var int[]
48
	 */
49
	public $performed_by_guid;
50
51
	/**
52
	 * @var int[]
53
	 */
54
	public $owner_guid;
55
	/**
56
	 * @var int[]
57
	 */
58
	public $access_id;
59
	/**
60
	 * @var string
61
	 */
62
	public $enabled;
63
	/**
64
	 * @var int|string|\DateTime
65
	 */
66
	public $created_after;
67
68
	/**
69
	 * @var int|string|\DateTime
70
	 */
71
	public $created_before;
72
73
	/**
74
	 * @var string[]
75
	 */
76
	public $ip_address;
77
78
	/**
79
	 * @var int
80
	 */
81
	public $limit;
82
83
	/**
84
	 * @var int
85
	 */
86
	public $offset;
87
88
	/**
89
	 * Count rows
90
	 * @return int
91
	 */
92 1
	public function count() {
93 1
		$this->normalizeOptions();
94
95 1
		$qb = Select::fromTable('system_log');
96 1
		$qb->select('COUNT(*) as total');
97 1
		$wheres = $this->buildQuery($qb);
98 1
		if ($wheres) {
99 1
			$qb->where($wheres);
100
		}
101
102 1
		$result = _elgg_services()->db->getDataRow($qb);
103 1
		if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
			return 0;
105
		}
106
107 1
		return (int) $result->total;
108
	}
109
110
	/**
111
	 * Apply numeric calculation to a column
112
	 *
113
	 * @param string $function      Calculation, e.g. max, min, avg
114
	 * @param string $property      Property name
115
	 * @param string $property_type Property type
116
	 *
117
	 * @return int|float
118
	 */
119
	public function calculate($function, $property, $property_type = null) {
120
		throw new \LogicException(__METHOD__ . ' not implemented');
121
	}
122
123
	/**
124
	 * Fetch rows
125
	 *
126
	 * @param int            $limit    Number of rows to fetch
127
	 * @param int            $offset   Index of the first row
128
	 * @param callable|false $callback Callback function to run database rows through
129
	 *
130
	 * @return \ElggData[]|false
131
	 */
132 2
	public function get($limit = null, $offset = null, $callback = null) {
133
134 2
		$this->normalizeOptions();
135
136 2
		$qb = Select::fromTable('system_log');
137 2
		$qb->select('*');
138 2
		$wheres = $this->buildQuery($qb);
139 2
		if ($wheres) {
140 1
			$qb->where($wheres);
141
		}
142
143 2
		if ($limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. 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...
144
			$qb->setMaxResults($limit);
145
			$qb->setFirstResult($offset);
146
		}
147
148 2
		$qb->orderBy('time_created', 'DESC');
149
150 2
		return _elgg_services()->db->getData($qb);
151
	}
152
153
	/**
154
	 * Apply correct execution method based on calculation, count or other criteria
155
	 * @return mixed
156
	 */
157 2
	public function execute() {
158
159 2
		if ($this->count) {
160 1
			return $this->count();
161
		}
162
163 2
		return $this->get($this->limit, $this->offset);
164
	}
165
166
	/**
167
	 * Normalizes options
168
	 * @return void
169
	 */
170 2
	protected function normalizeOptions() {
171
		$defaults = [
172 2
			'limit' => elgg_get_config('default_limit'),
173 2
			'offset' => 0,
174
		];
175
176 2
		foreach ($defaults as $key => $value) {
177 2
			if (!isset($this->$key)) {
178 2
				$this->$key = $value;
179
			}
180
		}
181
182 2
		$this->performed_by_guid = Values::normalizeGuids($this->performed_by_guid);
183 2
		$this->owner_guid = Values::normalizeGuids($this->owner_guid);
184 2
		$this->object_id = Values::normalizeIds($this->object_id);
185 2
		$this->access_id = Values::normalizeIds($this->access_id);
186 2
		$this->created_after = Values::normalizeTimestamp($this->created_after);
187 2
		$this->created_before = Values::normalizeTimestamp($this->created_before);
188 2
	}
189
190
	/**
191
	 * Build where clauses
192
	 *
193
	 * @param QueryBuilder $qb Query builder
194
	 * @return CompositeExpression
195
	 */
196 2
	protected function buildQuery(QueryBuilder $qb) {
197
198 2
		$wheres = [];
199
200 2
		$wheres[] = $qb->compare('performed_by_guid', '=', $this->performed_by_guid, ELGG_VALUE_INTEGER);
201 2
		$wheres[] = $qb->compare('event', '=', $this->event, ELGG_VALUE_STRING);
202 2
		$wheres[] = $qb->compare('object_id', '=', $this->object_id, ELGG_VALUE_INTEGER);
203 2
		$wheres[] = $qb->compare('object_class', '=', $this->object_class, ELGG_VALUE_STRING);
204 2
		$wheres[] = $qb->compare('object_type', '=', $this->object_type, ELGG_VALUE_STRING);
205 2
		$wheres[] = $qb->compare('object_subtype', '=', $this->object_subtype, ELGG_VALUE_STRING);
206 2
		$wheres[] = $qb->compare('ip_address', '=', $this->ip_address, ELGG_VALUE_STRING);
207 2
		$wheres[] = $qb->compare('owner_guid', '=', $this->owner_guid, ELGG_VALUE_INTEGER);
208 2
		$wheres[] = $qb->compare('access_id', '=', $this->access_id, ELGG_VALUE_INTEGER);
209 2
		$wheres[] = $qb->compare('enabled', '=', $this->enabled, ELGG_VALUE_STRING);
210 2
		$wheres[] = $qb->between('time_created', $this->created_after, $this->created_before, ELGG_VALUE_INTEGER);
211
212 2
		return $qb->merge($wheres);
213
	}
214
}
215