Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

DbConfig::getParticularConnectionConfig()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nc 4
nop 1
dl 0
loc 39
rs 8.5806
c 0
b 0
f 0
1
<?php
2
namespace Elgg\Database;
3
4
use Elgg\Config;
5
6
/**
7
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
8
 *
9
 * @access private
10
 * @since  1.9.0
11
 */
12
class DbConfig {
13
14
	const READ = 'read';
15
	const WRITE = 'write';
16
	const READ_WRITE = 'readwrite';
17
18
	protected $db;
19
	protected $dbprefix;
20
	protected $dbhost;
21
	protected $dbuser;
22
	protected $dbpass;
23
	protected $dbname;
24
	protected $db_disable_query_cache;
25
	protected $dbencoding;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param \stdClass $config Object with keys:
31
	 *  db
32
	 *  dbprefix
33
	 *  dbhost
34
	 *  dbuser
35
	 *  dbpass
36
	 *  dbname
37
	 *  db_disable_query_cache
38
	 *  dbencoding
39
	 */
40
	public function __construct(\stdClass $config) {
41
		foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
42
			$this->{$prop} = isset($config->{$prop}) ? $config->{$prop} : null;
43
		}
44
	}
45
46
	/**
47
	 * Construct from an Elgg Config
48
	 *
49
	 * @param Config $config Elgg config
50
	 *
51
	 * @return DbConfig
52
	 */
53
	public static function fromElggConfig(Config $config) {
54
		$obj = new \stdClass();
55
		foreach (array_keys(get_class_vars(__CLASS__)) as $prop) {
56
			$obj->{$prop} = $config->{$prop};
57
		}
58
		return new self($obj);
59
	}
60
61
	/**
62
	 * Get the database table prefix
63
	 *
64
	 * @return string
65
	 */
66
	public function getTablePrefix() {
67
		return $this->dbprefix;
68
	}
69
70
	/**
71
	 * Is the query cache enabled?
72
	 *
73
	 * @return bool
74
	 */
75
	public function isQueryCacheEnabled() {
76
		if ($this->db_disable_query_cache !== null) {
77
			return !$this->db_disable_query_cache;
78
		}
79
80
		return true;
81
	}
82
83
	/**
84
	 * Are the read and write connections separate?
85
	 *
86
	 * @return bool
87
	 */
88
	public function isDatabaseSplit() {
89
		if (isset($this->db['split'])) {
90
			return $this->db['split'];
91
		}
92
93
		// this was the recommend structure from Elgg 1.0 to 1.8
94
		if (isset($this->db->split)) {
95
			return $this->db->split;
96
		}
97
98
		return false;
99
	}
100
101
	/**
102
	 * Get the connection configuration
103
	 *
104
	 * The parameters are in an array like this:
105
	 * array(
106
	 *	'host' => 'xxx',
107
	 *  'user' => 'xxx',
108
	 *  'password' => 'xxx',
109
	 *  'database' => 'xxx',
110
	 * )
111
	 *
112
	 * @param string $type The connection type: READ, WRITE, READ_WRITE
113
	 * @return array
114
	 */
115
	public function getConnectionConfig($type = self::READ_WRITE) {
116
		switch ($type) {
117
			case self::READ:
118
			case self::WRITE:
119
				$config = $this->getParticularConnectionConfig($type);
120
				break;
121
			default:
122
				$config = $this->getGeneralConnectionConfig();
123
				break;
124
		}
125
126
		$config['encoding'] = $this->dbencoding ? $this->dbencoding : 'utf8';
127
128
		return $config;
129
	}
130
131
	/**
132
	 * Get the read/write database connection information
133
	 *
134
	 * @return array
135
	 */
136
	protected function getGeneralConnectionConfig() {
137
		return [
138
			'host' => $this->dbhost,
139
			'user' => $this->dbuser,
140
			'password' => $this->dbpass,
141
			'database' => $this->dbname,
142
		];
143
	}
144
145
	/**
146
	 * Get connection information for reading or writing
147
	 *
148
	 * @param string $type Connection type: 'write' or 'read'
149
	 * @return array
150
	 */
151
	protected function getParticularConnectionConfig($type) {
152
		if (is_object($this->db[$type])) {
153
			// old style single connection (Elgg < 1.9)
154
			$config = [
155
				'host' => $this->db[$type]->dbhost,
156
				'user' => $this->db[$type]->dbuser,
157
				'password' => $this->db[$type]->dbpass,
158
				'database' => $this->db[$type]->dbname,
159
			];
160
		} else if (array_key_exists('dbhost', $this->db[$type])) {
161
			// new style single connection
162
			$config = [
163
				'host' => $this->db[$type]['dbhost'],
164
				'user' => $this->db[$type]['dbuser'],
165
				'password' => $this->db[$type]['dbpass'],
166
				'database' => $this->db[$type]['dbname'],
167
			];
168
		} else if (is_object(current($this->db[$type]))) {
169
			// old style multiple connections
170
			$index = array_rand($this->db[$type]);
171
			$config = [
172
				'host' => $this->db[$type][$index]->dbhost,
173
				'user' => $this->db[$type][$index]->dbuser,
174
				'password' => $this->db[$type][$index]->dbpass,
175
				'database' => $this->db[$type][$index]->dbname,
176
			];
177
		} else {
178
			// new style multiple connections
179
			$index = array_rand($this->db[$type]);
180
			$config = [
181
				'host' => $this->db[$type][$index]['dbhost'],
182
				'user' => $this->db[$type][$index]['dbuser'],
183
				'password' => $this->db[$type][$index]['dbpass'],
184
				'database' => $this->db[$type][$index]['dbname'],
185
			];
186
		}
187
188
		return $config;
189
	}
190
}
191
192