Completed
Push — master ( 5fdfe9...8a6726 )
by Ondřej
03:21
created

Connection   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 8

Importance

Changes 0
Metric Value
wmc 45
lcom 6
cbo 8
dl 0
loc 279
rs 7.4545
c 0
b 0
f 0

45 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getName() 0 4 1
A getConfig() 0 4 1
A getParameters() 0 4 1
A isConnected() 0 4 1
A isConnectedWait() 0 4 1
A connect() 0 4 1
A connectWait() 0 4 1
A disconnect() 0 4 1
A getTypeRegister() 0 4 1
A getTypeDictionary() 0 4 1
A flushTypeDictionary() 0 4 1
A query() 0 4 1
A command() 0 4 1
A rawQuery() 0 4 1
A rawMultiQuery() 0 4 1
A runScript() 0 4 1
A getStatementExceptionFactory() 0 4 1
A copyFromFile() 0 4 1
A copyFromProgram() 0 4 1
A copyFromInput() 0 4 1
A copyToFile() 0 4 1
A copyToProgram() 0 4 1
A copyToArray() 0 4 1
A inTransaction() 0 4 1
A startTransaction() 0 4 1
A setupTransaction() 0 4 1
A setupSubsequentTransactions() 0 4 1
A commit() 0 4 1
A rollback() 0 4 1
A savepoint() 0 4 1
A rollbackToSavepoint() 0 4 1
A releaseSavepoint() 0 4 1
A setTransactionSnapshot() 0 4 1
A exportTransactionSnapshot() 0 4 1
A prepareTransaction() 0 4 1
A commitPreparedTransaction() 0 4 1
A rollbackPreparedTransaction() 0 4 1
A listPreparedTransactions() 0 4 1
A getBackendPID() 0 4 1
A notify() 0 4 1
A listen() 0 4 1
A unlisten() 0 4 1
A unlistenAll() 0 4 1
A pollNotification() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Connection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Connection, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Ivory\Connection;
3
4
use Ivory\Exception\InvalidStateException;
5
use Ivory\Utils\NotSerializable;
6
7
// TODO: consider renaming to Database or Session or... - to reflect it is mere a facade, the single entry point
8
// TODO: consider introducing factory methods for abbreviating relation creating; e.g., new QueryRelation($conn, $query)
9
class Connection implements IConnection
10
{
11
	use NotSerializable; // TODO: implement connection serialization instead of giving up
12
13
	private $name;
14
	private $connCtl;
15
	private $typeCtl;
16
	private $stmtExec;
17
	private $copyCtl;
18
	private $txCtl;
19
	private $ipcCtl;
20
21
	private $config;
22
23
24
	/**
25
	 * @param string $name name for the connection
26
	 * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array
27
	 *                                                    of parameters for {@link ConnectionParameters::__construct()},
28
	 *                                                    or a URL for {@link ConnectionParameters::fromUrl()},
29
	 *                                                    or a PostgreSQL connection string (see {@link pg_connect()})
30
	 */
31
	public function __construct($name, $params)
32
	{
33
		$this->name = $name;
34
		$this->connCtl = new ConnectionControl($params); // TODO: extract all usages of ConnectionControl::requireConnection() - consider introducing an interface specifying the method, named like PGSQLDriver or ConnectionManager or ConnectionPool
35
		$this->typeCtl = new TypeControl($this, $this->connCtl);
36
		$this->stmtExec = new StatementExecution($this->connCtl, $this->typeCtl);
37
		$this->copyCtl = new CopyControl();
38
		$this->txCtl = new TransactionControl($this->connCtl, $this->stmtExec);
39
		$this->ipcCtl = new IPCControl($this->connCtl);
40
		$this->config = new ConnConfig($this->connCtl, $this->stmtExec, $this->txCtl);
41
	}
42
43
	final public function getName()
44
	{
45
		return $this->name;
46
	}
47
48
	final public function getConfig()
49
	{
50
		return $this->config;
51
	}
52
53
54
	//region Connection Control
55
56
	public function getParameters()
57
	{
58
		return $this->connCtl->getParameters();
59
	}
60
61
	public function isConnected()
62
	{
63
		return $this->connCtl->isConnected();
64
	}
65
66
	public function isConnectedWait()
67
	{
68
		return $this->connCtl->isConnectedWait();
69
	}
70
71
	public function connect()
72
	{
73
		return $this->connCtl->connect();
74
	}
75
76
	public function connectWait()
77
	{
78
		return $this->connCtl->connectWait();
79
	}
80
81
	public function disconnect()
82
	{
83
		return $this->connCtl->disconnect();
84
	}
85
86
	//endregion
87
88
	//region Type Control
89
90
	public function getTypeRegister()
91
	{
92
		return $this->typeCtl->getTypeRegister();
93
	}
94
95
	public function getTypeDictionary()
96
	{
97
		return $this->typeCtl->getTypeDictionary();
98
	}
99
100
	public function flushTypeDictionary()
101
	{
102
		$this->typeCtl->flushTypeDictionary();
103
	}
104
105
	//endregion
106
107
	//region Statement Execution
108
109
    public function query($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap)
110
    {
111
        return $this->stmtExec->query($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap);
112
    }
113
114
    public function command($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap)
115
    {
116
        return $this->stmtExec->command($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap);
117
    }
118
119
	public function rawQuery(string $sqlStatement)
120
	{
121
		return $this->stmtExec->rawQuery($sqlStatement);
122
	}
123
124
	public function rawMultiQuery($sqlStatements)
125
	{
126
		return $this->stmtExec->rawMultiQuery($sqlStatements);
127
	}
128
129
	public function runScript($sqlScript)
130
	{
131
		return $this->stmtExec->runScript($sqlScript);
132
	}
133
134
    public function getStatementExceptionFactory()
135
   	{
136
   		return $this->stmtExec->getStatementExceptionFactory();
137
   	}
138
139
	//endregion
140
141
	//region Copy Control
142
143
	public function copyFromFile($file, $table, $columns = null, $options = [])
144
	{
145
		return $this->copyCtl->copyFromFile($file, $table, $columns, $options);
146
	}
147
148
	public function copyFromProgram($program, $table, $columns = null, $options = [])
149
	{
150
		return $this->copyCtl->copyFromProgram($program, $table, $columns, $options);
151
	}
152
153
	public function copyFromInput($table, $columns = null, $options = [])
154
	{
155
		return $this->copyCtl->copyFromInput($table, $columns, $options);
156
	}
157
158
	public function copyToFile($file, $tableOrQuery, $columns = null, $options = [])
159
	{
160
		return $this->copyCtl->copyToFile($file, $tableOrQuery, $columns, $options);
161
	}
162
163
	public function copyToProgram($program, $tableOrQuery, $columns = null, $options = [])
164
	{
165
		return $this->copyCtl->copyToProgram($program, $tableOrQuery, $columns, $options);
166
	}
167
168
	public function copyToArray($table, $options = [])
169
	{
170
		return $this->copyCtl->copyToArray($table, $options);
171
	}
172
173
	//endregion
174
175
	//region Transaction Control
176
177
	public function inTransaction()
178
	{
179
		return $this->txCtl->inTransaction();
180
	}
181
182
	public function startTransaction($transactionOptions = 0)
183
	{
184
		return $this->txCtl->startTransaction($transactionOptions);
185
	}
186
187
	public function setupTransaction($transactionOptions)
188
	{
189
		$this->txCtl->setupTransaction($transactionOptions);
190
	}
191
192
	public function setupSubsequentTransactions($transactionOptions)
193
	{
194
		$this->txCtl->setupSubsequentTransactions($transactionOptions);
195
	}
196
197
	public function commit()
198
	{
199
		return $this->txCtl->commit();
200
	}
201
202
	public function rollback()
203
	{
204
		return $this->txCtl->rollback();
205
	}
206
207
	public function savepoint($name)
208
	{
209
		$this->txCtl->savepoint($name);
210
	}
211
212
	public function rollbackToSavepoint($name)
213
	{
214
		$this->txCtl->rollbackToSavepoint($name);
215
	}
216
217
	public function releaseSavepoint($name)
218
	{
219
		$this->txCtl->releaseSavepoint($name);
220
	}
221
222
	public function setTransactionSnapshot($snapshotId)
223
	{
224
		return $this->txCtl->setTransactionSnapshot($snapshotId);
225
	}
226
227
	public function exportTransactionSnapshot()
228
	{
229
		return $this->txCtl->exportTransactionSnapshot();
230
	}
231
232
	public function prepareTransaction($name)
233
	{
234
		return $this->txCtl->prepareTransaction($name);
235
	}
236
237
	public function commitPreparedTransaction($name)
238
	{
239
		$this->txCtl->commitPreparedTransaction($name);
240
	}
241
242
	public function rollbackPreparedTransaction($name)
243
	{
244
		$this->txCtl->rollbackPreparedTransaction($name);
245
	}
246
247
	public function listPreparedTransactions()
248
	{
249
		return $this->txCtl->listPreparedTransactions();
250
	}
251
252
	//endregion
253
254
	//region IPC Control
255
256
	public function getBackendPID()
257
	{
258
		return $this->ipcCtl->getBackendPID();
259
	}
260
261
	public function notify($channel, $payload = null)
262
	{
263
		$this->ipcCtl->notify($channel, $payload);
264
	}
265
266
	public function listen($channel)
267
	{
268
		$this->ipcCtl->listen($channel);
269
	}
270
271
	public function unlisten($channel)
272
	{
273
		$this->ipcCtl->unlisten($channel);
274
	}
275
276
	public function unlistenAll()
277
	{
278
		$this->ipcCtl->unlistenAll();
279
	}
280
281
	public function pollNotification()
282
	{
283
		return $this->ipcCtl->pollNotification();
284
	}
285
286
	//endregion
287
}
288