Passed
Push — master ( 2b3c87...8b15b1 )
by Adrian
02:17
created

DbConfig::getMasterDataConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Author: Adrian Dumitru
5
 * Date: 5/27/2017 1:07 PM
6
 */
7
8
namespace Qpdb\QueryBuilder\DB;
9
10
11
class DbConfig
12
{
13
14
	private static $instance;
15
16
	/**
17
	 * @var array
18
	 */
19
	private $dbConfig;
20
21
	/**
22
	 * @var bool
23
	 */
24
	private $replicationEnable;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $masterDataConnect;
30
31
	/**
32
	 * @var array
33
	 */
34
	private $slaveDataConnect;
35
36
	/**
37
	 * @var bool
38
	 */
39
	private $enableLogErrors = false;
40
41
	/**
42
	 * @var bool
43
	 */
44
	private $enableLogQueryDuration = false;
45
46
	/**
47
	 * @var string
48
	 */
49
	private $logPathErrors;
50
51
	/**
52
	 * @var string
53
	 */
54
	private $logPathQueryDuration;
55
56
57
	/**
58
	 * DbConfig constructor.
59
	 */
60
	private function __construct()
61
	{
62
		$vendorCfg = __DIR__ . '/../../../../../vendor-cfg/qpdb_db_config.php';
63
		if ( file_exists( $vendorCfg ) )
64
			$this->dbConfig = require $vendorCfg;
65
		else
66
			$this->dbConfig = require __DIR__ . '/../../config/qpdb_db_config.php';
67
68
		$this->buildConfig();
69
	}
70
71
	/**
72
	 * @param string $fileConfig
73
	 * @return $this
74
	 * @deprecated
75
	 */
76
	public function withFileConfig( $fileConfig )
77
	{
78
		$this->dbConfig = require $fileConfig;
79
		$this->buildConfig();
80
81
		return $this;
82
	}
83
84
	/**
85
	 * @param $fileConfig
86
	 * @return $this
87
	 */
88
	public function withConfigPath( $fileConfig )
89
	{
90
		$this->dbConfig = require $fileConfig;
91
		$this->buildConfig();
92
93
		return $this;
94
	}
95
96
	/**
97
	 * @param array $dbConfig
98
	 * @return $this
99
	 */
100
	public function withConfigArray ( array $dbConfig )
101
	{
102
		$this->dbConfig = $dbConfig;
103
		$this->buildConfig();
104
105
		return $this;
106
	}
107
108
	/**
109
	 * @return bool
110
	 */
111
	public function getReplicationEnable()
112
	{
113
		return $this->replicationEnable;
114
	}
115
116
	/**
117
	 * @return array
118
	 */
119
	public function getMasterDataConnect()
120
	{
121
		return $this->masterDataConnect;
122
	}
123
124
	/**
125
	 * @return array
126
	 */
127
	public function getSlaveDataConnect()
128
	{
129
		return $this->slaveDataConnect;
130
	}
131
132
	/**
133
	 * @return array
134
	 */
135
	public function getLogConfig()
136
	{
137
		return $this->dbConfig[ 'db_log' ];
138
	}
139
140
	/**
141
	 * @return bool
142
	 */
143
	public function isEnableLogErrors()
144
	{
145
		return $this->enableLogErrors;
146
	}
147
148
	/**
149
	 * @return bool
150
	 */
151
	public function isEnableLogQueryDuration()
152
	{
153
		return $this->enableLogQueryDuration;
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getLogPathErrors()
160
	{
161
		return $this->logPathErrors;
162
	}
163
164
	/**
165
	 * @return string
166
	 */
167
	public function getLogPathQueryDuration()
168
	{
169
		return $this->logPathQueryDuration;
170
	}
171
172
	public function useTablePrefix()
173
	{
174
		if ( !empty( $this->dbConfig[ 'use_table_prefix' ] ) )
175
			return $this->dbConfig[ 'use_table_prefix' ];
176
177
		return false;
178
	}
179
180
	public function getTablePrefix()
181
	{
182
		return $this->dbConfig[ 'table_prefix' ];
183
	}
184
185
186
	private function buildConfig()
187
	{
188
		$this->replicationEnable = $this->dbConfig[ 'replicationEnable' ];
189
		$this->readMasterDataConnect();
190
		$this->readSlaveDataConnect();
191
		$this->configLogger();
192
	}
193
194
	private function configLogger()
195
	{
196
		$this->enableLogErrors = $this->dbConfig[ 'db_log' ][ 'enable_log_errors' ];
197
		$this->enableLogQueryDuration = $this->dbConfig[ 'db_log' ][ 'enable_log_query_duration' ];
198
		$this->logPathErrors = $this->dbConfig[ 'db_log' ][ 'log_path_errors' ];
199
		$this->logPathQueryDuration = $this->dbConfig[ 'db_log' ][ 'log_path_query_duration' ];
200
201
	}
202
203
	/**
204
	 * @return bool
205
	 * @throws DbException
206
	 */
207
	private function readMasterDataConnect()
208
	{
209
210
		if ( !isset( $this->dbConfig[ 'master_data_connect' ][ 0 ] ) )
211
			throw new DbException( 'Master data connect is missing', DbException::DB_ERROR_MASTER_DATA_CONNECTION_MISSING );
212
213
		$dataConnection = $this->dbConfig[ 'master_data_connect' ];
214
215
		if ( !$this->replicationEnable || count( $dataConnection ) == 1 ) {
216
			$this->masterDataConnect = $dataConnection[ 0 ];
217
218
			return true;
219
		}
220
221
		shuffle( $dataConnection );
222
		$this->masterDataConnect = $dataConnection[ 0 ];
223
224
		return true;
225
	}
226
227
228
	/**
229
	 * @return bool
230
	 */
231
	private function readSlaveDataConnect()
232
	{
233
234
		if ( !isset( $this->dbConfig[ 'slave_data_connect' ][ 0 ] ) ) {
235
			$this->slaveDataConnect = $this->masterDataConnect;
236
237
			return true;
238
		}
239
240
		$dataConnection = $this->dbConfig[ 'slave_data_connect' ];
241
242
		shuffle( $dataConnection );
243
		$this->slaveDataConnect = $dataConnection[ 0 ];
244
245
		return true;
246
	}
247
248
	/**
249
	 * @return DbConfig
250
	 */
251
	public static function getInstance()
252
	{
253
		if ( null === self::$instance ) {
254
			self::$instance = new self();
255
		}
256
257
		return self::$instance;
258
	}
259
260
261
}