Completed
Push — master ( f6659b...84ef1f )
by Jeroen De
14s queued 10s
created

LazyDBConnectionProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
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
}