Completed
Pull Request — master (#98)
by Mark A.
02:00
created

DatabaseLogReader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 119
ccs 0
cts 55
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 18 4
A getLog() 0 9 2
A getQuery() 0 3 1
A getUser() 0 7 2
A getDate() 0 7 2
A getStatus() 0 7 2
1
<?php
2
3
namespace SESP;
4
5
use DatabaseLogEntry;
6
use DatabaseBase;
7
use MWTimestamp;
8
use Title;
9
use User;
10
11
class DatabaseLogReader {
12
13
	// The parameters for our query.
14
	private $query;
15
16
	// The current log
17
	private $log;
18
19
	// Don't run multiple queries if we don't have to
20
	private static $titleCache = [];
21
22
	// The db connection
23
	private $db;
24
25
	// The title key
26
	private $titlekey;
27
28
	// The type of query being performed
29
	private $type;
30
31
	/**
32
	 * Constructor for reading the log.
33
	 *
34
	 * @param AppFactory $appFactory injected AppFactory
0 ignored issues
show
Bug introduced by
There is no parameter named $appFactory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
	 * @param Title $title page
36
	 * @param string $type of log (default: approval)
37
	 */
38
	public function __construct( DatabaseBase $db, Title $title, $type = 'approval' ) {
39
		$this->db = $db;
40
		$this->titlekey = $title->getDBKey();
41
		$this->type = $type;
42
	}
43
44
	/**
45
	 * Take care of loading from the cache or filling the query.
46
	 */
47
	private function init() {
48
		if ( !$this->query ) {
49
			if ( !isset( self::$titleCache[ $this->titlekey ] ) ) {
50
				$this->query = DatabaseLogEntry::getSelectQueryData();
51
52
				$this->query['conds'] = [
53
					'log_type' => $this->type,
54
					'log_title' => $this->titlekey
55
				];
56
				$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
57
				self::$titleCache[ $this->titlekey ] = $this;
58
			} elseif ( $this->query ) {
59
				$cache = self::$titleCache[ $this->titlekey ];
60
				$this->query = $cache->getQuery();
61
				$this->log = $cache->getLog();
62
			}
63
		}
64
	}
65
66
	/**
67
	 * Fetch the results using our conditions
68
	 *
69
	 * @return IResultWrapper
70
	 * @throws DBError
71
	 */
72
	private function getLog() {
73
		if ( !$this->log ) {
74
			$this->log = $this->db->select(
75
				$this->query['tables'], $this->query['fields'], $this->query['conds'],
76
				__METHOD__, $this->query['options'], $this->query['join_conds']
77
			);
78
		}
79
		return $this->log;
80
	}
81
82
	/**
83
	 * Fetch the query for later calls
84
	 *
85
	 * @return array
86
	 */
87
	public function getQuery() {
88
		return $this->query;
89
	}
90
91
	/**
92
	 * Get the person who made the last for this page
93
	 *
94
	 * @return User
95
	 */
96
	public function getUser() {
97
		$this->init();
98
		$logLine = $this->getLog()->current();
99
		if ( $logLine ) {
100
			return User::newFromID( $logLine->user_id );
101
		}
102
	}
103
104
	/**
105
	 * Get the date of the last entry in the log for this page
106
	 *
107
	 * @return Timestamp
108
	 */
109
	public function getDate() {
110
		$this->init();
111
		$logLine = $this->getLog()->current();
112
		if ( $logLine ) {
113
			return new MWTimestamp( $logLine->log_timestamp );
114
		}
115
	}
116
117
	/**
118
	 * Get the status of the last entry in the log for this page
119
	 *
120
	 * @return Timestamp
121
	 */
122
	public function getStatus() {
123
		$this->init();
124
		$logLine = $this->getLog()->current();
125
		if ( $logLine ) {
126
			return $logLine->log_action;
127
		}
128
	}
129
}
130