Completed
Branch master (174b3a)
by
unknown
26:51
created

LoadBalancerSingle   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 6
A newFromConnection() 0 3 1
A reallyOpenConnection() 0 3 1
1
<?php
2
/**
3
 * Simple generator of database connections that always returns the same object.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Database
22
 */
23
24
/**
25
 * Trivial LoadBalancer that always returns an injected connection handle
26
 */
27
class LoadBalancerSingle extends LoadBalancer {
28
	/** @var IDatabase */
29
	private $db;
30
31
	/**
32
	 * @param array $params An associative array with one member:
33
	 *   - connection: An IDatabase connection object
34
	 */
35
	public function __construct( array $params ) {
36
		if ( !isset( $params['connection'] ) ) {
37
			throw new InvalidArgumentException( "Missing 'connection' argument." );
38
		}
39
40
		$this->db = $params['connection'];
41
42
		parent::__construct( [
43
			'servers' => [
44
				[
45
					'type' => $this->db->getType(),
46
					'host' => $this->db->getServer(),
47
					'dbname' => $this->db->getDBname(),
48
					'load' => 1,
49
				]
50
			],
51
			'trxProfiler' => isset( $params['trxProfiler'] ) ? $params['trxProfiler'] : null,
52
			'srvCache' => isset( $params['srvCache'] ) ? $params['srvCache'] : null,
53
			'wanCache' => isset( $params['wanCache'] ) ? $params['wanCache'] : null
54
		] );
55
56
		if ( isset( $params['readOnlyReason'] ) ) {
57
			$this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
58
		}
59
	}
60
61
	/**
62
	 * @param IDatabase $db Live connection handle
63
	 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
64
	 * @return LoadBalancerSingle
65
	 * @since 1.28
66
	 */
67
	public static function newFromConnection( IDatabase $db, array $params = [] ) {
68
		return new static( [ 'connection' => $db ] + $params );
69
	}
70
71
	/**
72
	 *
73
	 * @param string $server
74
	 * @param bool $dbNameOverride
75
	 *
76
	 * @return IDatabase
77
	 */
78
	protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
79
		return $this->db;
80
	}
81
}
82