Completed
Push — master ( 4cfef8...0aaff2 )
by
unknown
08:02
created

DatabaseLogReader::getLog()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.243

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 20
cp 0.7
rs 9.488
cc 3
nc 3
nop 0
crap 3.243
1
<?php
2
3
namespace SESP;
4
5
use ArrayIterator;
6
use DatabaseLogEntry;
7
use DatabaseBase;
8
use MWTimestamp;
9
use Title;
10
use User;
11
12
class DatabaseLogReader {
13
14
	/**
15
	 * @var array
16
	 */
17
	private static $titleCache = [];
18
19
	/**
20
	 * @var DatabaseBase
21
	 */
22
	private $dbr;
23
24
	/**
25
	 * @var string
26
	 */
27
	private $query;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $log;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $dbKey;
38
39
	/**
40
	 * @var string
41
	 */
42
	private $type;
43
44
	/**
45
	 * @since 2.0
46
	 *
47
	 * @param DatabaseaBase $dbr injected connection
48
	 * @param Title|null $title
49
	 * @param string $type of log (default: approval)
50
	 */
51 6
	public function __construct( DatabaseBase $dbr, Title $title = null , $type = 'approval' ) {
52 6
		$this->dbr = $dbr;
53 6
		$this->dbKey = $title instanceof Title ? $title->getDBkey() : null;
0 ignored issues
show
Bug introduced by
The class Title does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
54 6
		$this->type = $type;
55 6
	}
56
57
	/**
58
	 * @since 2.0
59
	 */
60 1
	public function clearCache() {
61 1
		self::$titleCache = [];
62 1
	}
63
64
	/**
65
	 * Fetch the query parameters for later calls
66
	 *
67
	 * @since 2.0
68
	 *
69
	 * @return array of parameters for SELECT call
70
	 */
71 5
	public function getQuery() {
72 5
		return $this->query;
73
	}
74
75
	/**
76
	 * @since 2.0
77
	 *
78
	 * @return User
79
	 */
80 4
	public function getUserForLogEntry() {
81 4
		$this->init();
82 4
		$logLine = $this->getLog()->current();
83
84 4
		if ( $logLine && $logLine->user_id ) {
85 2
			return User::newFromID( $logLine->user_id );
86
		}
87 2
	}
88
89
	/**
90
	 * @since 2.0
91
	 *
92
	 * @return Timestamp
93
	 */
94 2
	public function getDateOfLogEntry() {
95 2
		$this->init();
96 2
		$logLine = $this->getLog()->current();
97
98 2
		if ( $logLine && $logLine->log_timestamp ) {
99 1
			return new MWTimestamp( $logLine->log_timestamp );
100
		}
101 1
	}
102
103
	/**
104
	 * @since 2.0
105
	 *
106
	 * @return string
107
	 */
108 2
	public function getStatusOfLogEntry() {
109 2
		$this->init();
110 2
		$logLine = $this->getLog()->current();
111
112 2
		if ( $logLine && $logLine->log_action ) {
113 1
			return $logLine->log_action;
114
		}
115 1
	}
116
117
	/**
118
	 * Take care of loading from the cache or filling the query.
119
	 */
120 4
	private function init() {
121
122 4
		if ( $this->query ) {
123 3
			return;
124
		}
125
126 4
		if ( !isset( self::$titleCache[ $this->dbKey ] ) ) {
127 3
			$this->query = DatabaseLogEntry::getSelectQueryData();
128
129 3
			$this->query['conds'] = [
130 3
				'log_type' => $this->type,
131 3
				'log_title' => $this->dbKey
132 3
			];
133 3
			$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
134 3
			self::$titleCache[ $this->dbKey ] = $this;
135 3
		} else {
136 1
			$cache = self::$titleCache[ $this->dbKey ];
137 1
			$this->query = $cache->getQuery();
138 1
			$this->log = $cache->getLog();
139
		}
140
141 4
	}
142
143
	/**
144
	 * Fetch the results using our conditions
145
	 *
146
	 * @return IResultWrapper
147
	 * @throws DBError
148
	 */
149 4
	private function getLog() {
150 4
		if ( !$this->log ) {
151
152 3
			$query = $this->getQuery();
153
154 3
			$this->log = $this->dbr->select(
155 3
				$query['tables'],
156 3
				$query['fields'],
157 3
				$query['conds'],
158 3
				__METHOD__,
159 3
				$query['options'],
160 3
				$query['join_conds']
161 3
			);
162
163 3
			if ( $this->log === null ) {
164
				$this->log = new ArrayIterator(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayIterator(array...'log_action' => null))) of type object<ArrayIterator> is incompatible with the declared type string of property $log.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
165
					[ (object)[
166
						'user_id' => null,
167
						'log_timestamp' => null,
168
						'log_action' => null
169
					] ]
170
				);
171
			}
172 3
		}
173
174 4
		return $this->log;
175
	}
176
177
}
178