LazyDBConnectionProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 9 2
A releaseConnection() 0 5 3
1
<?php
2
3
namespace SubPageList;
4
5
use DatabaseBase;
6
7
/**
8
 * Lazy database connection provider.
9
 * The connection is fetched when needed using the id provided in the constructor.
10
 *
11
 * @since 1.0
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class LazyDBConnectionProvider implements DBConnectionProvider {
17
18
	/**
19
	 * @var DatabaseBase|null
20
	 */
21
	private $connection = null;
22
23
	/**
24
	 * @var int|null
25
	 */
26
	private $connectionId = null;
27
28
	/**
29
	 * @var string|array
30
	 */
31
	private $groups;
32
33
	/**
34
	 * @var string|boolean $wiki
35
	 */
36
	private $wiki;
37
38
	/**
39
	 * Constructor.
40
	 *
41
	 * @since 1.0
42
	 *
43
	 * @param int $connectionId
44
	 * @param string|array $groups
45
	 * @param string|boolean $wiki
46
	 */
47 26
	public function __construct( $connectionId, $groups = [], $wiki = false ) {
48 26
		$this->connectionId = $connectionId;
49 26
		$this->groups = $groups;
50 26
		$this->wiki = $wiki;
51 26
	}
52
53
	/**
54
	 * @see DBConnectionProvider::getConnection
55
	 *
56
	 * @since 1.0
57
	 *
58
	 * @return DatabaseBase
59
	 */
60 25
	public function getConnection() {
61 25
		if ( $this->connection === null ) {
62 25
			$this->connection = wfGetLB( $this->wiki )->getConnection( $this->connectionId, $this->groups, $this->wiki );
63
		}
64
65 25
		assert( $this->connection instanceof DatabaseBase );
0 ignored issues
show
Bug introduced by
The class DatabaseBase 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...
66
67 25
		return $this->connection;
68
	}
69
70
	/**
71
	 * @see DBConnectionProvider::releaseConnection
72
	 *
73
	 * @since 1.0
74
	 */
75 21
	public function releaseConnection() {
76 21
		if ( $this->wiki !== false && $this->connection !== null ) {
77
			wfGetLB( $this->wiki )->reuseConnection( $this->connection );
78
		}
79 21
	}
80
81
}