Total Complexity | 54 |
Total Lines | 337 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
23 | class Connection implements IConnection |
||
24 | { |
||
25 | use NotSerializable; // TODO: implement connection serialization instead of giving up |
||
26 | |||
27 | private $name; |
||
28 | private $connCtl; |
||
29 | private $typeCtl; |
||
30 | private $stmtExec; |
||
31 | private $txCtl; |
||
32 | private $ipcCtl; |
||
33 | private $cursorCtl; |
||
34 | private $cacheCtl; |
||
35 | |||
36 | private $config; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @param string $name name for the connection |
||
41 | * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array |
||
42 | * of parameters for {@link ConnectionParameters::fromArray()}, |
||
43 | * or a URL for {@link ConnectionParameters::fromUrl()}, |
||
44 | * or a PostgreSQL connection string (see {@link pg_connect()}) |
||
45 | */ |
||
46 | public function __construct(string $name, $params) |
||
47 | { |
||
48 | $this->name = $name; |
||
49 | // TODO: Extract all usages of ConnectionControl::requireConnection() - consider introducing an interface |
||
50 | // specifying the method, named like PGSQLDriver or ConnectionManager or ConnectionPool. |
||
51 | $this->connCtl = new ConnectionControl($this, $params); |
||
52 | $this->typeCtl = new TypeControl($this, $this->connCtl); |
||
53 | $this->stmtExec = new StatementExecution($this->connCtl, $this->typeCtl); |
||
54 | $this->txCtl = new TransactionControl($this->connCtl, $this->stmtExec, $this); |
||
55 | $this->ipcCtl = new IPCControl($this->connCtl, $this->stmtExec); |
||
56 | $this->config = new ConnConfig($this->connCtl, $this->stmtExec, $this->txCtl); |
||
57 | $this->cursorCtl = new CursorControl($this->stmtExec); |
||
58 | // TODO: Consider moving cacheCtl initialization out of Connection itself (let the core factory set it up), or |
||
59 | // do not hold cache control here but rather besides the connection register at Ivory. |
||
60 | $this->cacheCtl = Ivory::getCoreFactory()->createCacheControl($this); |
||
61 | } |
||
62 | |||
63 | final public function getName(): string |
||
64 | { |
||
65 | return $this->name; |
||
66 | } |
||
67 | |||
68 | final public function getConfig(): IConnConfig |
||
69 | { |
||
70 | return $this->config; |
||
71 | } |
||
72 | |||
73 | |||
74 | //region Connection Control |
||
75 | |||
76 | public function getParameters(): ConnectionParameters |
||
77 | { |
||
78 | return $this->connCtl->getParameters(); |
||
79 | } |
||
80 | |||
81 | public function isConnected(): ?bool |
||
82 | { |
||
83 | return $this->connCtl->isConnected(); |
||
84 | } |
||
85 | |||
86 | public function isConnectedWait(): ?bool |
||
87 | { |
||
88 | return $this->connCtl->isConnectedWait(); |
||
89 | } |
||
90 | |||
91 | public function connect(?\Closure $initProcedure = null): bool |
||
92 | { |
||
93 | return $this->connCtl->connect($initProcedure); |
||
94 | } |
||
95 | |||
96 | public function connectWait(): bool |
||
97 | { |
||
98 | return $this->connCtl->connectWait(); |
||
99 | } |
||
100 | |||
101 | public function disconnect(): bool |
||
102 | { |
||
103 | return $this->connCtl->disconnect(); |
||
104 | } |
||
105 | |||
106 | public function registerConnectStartHook(\Closure $closure): void |
||
107 | { |
||
108 | $this->connCtl->registerConnectStartHook($closure); |
||
109 | } |
||
110 | |||
111 | public function registerPreDisconnectHook(\Closure $closure): void |
||
112 | { |
||
113 | $this->connCtl->registerPreDisconnectHook($closure); |
||
114 | } |
||
115 | |||
116 | public function registerPostDisconnectHook(\Closure $closure): void |
||
117 | { |
||
118 | $this->connCtl->registerPostDisconnectHook($closure); |
||
119 | } |
||
120 | |||
121 | //endregion |
||
122 | |||
123 | //region Type Control |
||
124 | |||
125 | public function getTypeRegister(): TypeRegister |
||
126 | { |
||
127 | return $this->typeCtl->getTypeRegister(); |
||
128 | } |
||
129 | |||
130 | public function getTypeDictionary(): ITypeDictionary |
||
131 | { |
||
132 | return $this->typeCtl->getTypeDictionary(); |
||
133 | } |
||
134 | |||
135 | public function flushTypeDictionary(): void |
||
136 | { |
||
137 | $this->typeCtl->flushTypeDictionary(); |
||
138 | } |
||
139 | |||
140 | public function setTypeControlOption(int $option): void |
||
141 | { |
||
142 | $this->typeCtl->setTypeControlOption($option); |
||
143 | } |
||
144 | |||
145 | public function unsetTypeControlOption(int $option): void |
||
146 | { |
||
147 | $this->typeCtl->unsetTypeControlOption($option); |
||
148 | } |
||
149 | |||
150 | //endregion |
||
151 | |||
152 | //region Statement Execution |
||
153 | |||
154 | public function query($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IQueryResult |
||
155 | { |
||
156 | return $this->stmtExec->query($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams); |
||
157 | } |
||
158 | |||
159 | public function queryAsync($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IAsyncQueryResult |
||
160 | { |
||
161 | return $this->stmtExec->queryAsync($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams); |
||
162 | } |
||
163 | |||
164 | public function querySingleTuple($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): ?ITuple |
||
165 | { |
||
166 | return $this->stmtExec->querySingleTuple($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams); |
||
167 | } |
||
168 | |||
169 | public function querySingleColumn($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IColumn |
||
170 | { |
||
171 | return $this->stmtExec->querySingleColumn($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams); |
||
172 | } |
||
173 | |||
174 | public function querySingleValue($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams) |
||
175 | { |
||
176 | return $this->stmtExec->querySingleValue($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams); |
||
177 | } |
||
178 | |||
179 | public function command($sqlFragmentPatternOrCommand, ...$fragmentsAndParams): ICommandResult |
||
180 | { |
||
181 | return $this->stmtExec->command($sqlFragmentPatternOrCommand, ...$fragmentsAndParams); |
||
182 | } |
||
183 | |||
184 | public function commandAsync($sqlFragmentPatternOrCommand, ...$fragmentsAndParams): IAsyncCommandResult |
||
185 | { |
||
186 | return $this->stmtExec->commandAsync($sqlFragmentPatternOrCommand, ...$fragmentsAndParams); |
||
187 | } |
||
188 | |||
189 | public function rawQuery(string $sqlQuery): IQueryResult |
||
190 | { |
||
191 | return $this->stmtExec->rawQuery($sqlQuery); |
||
192 | } |
||
193 | |||
194 | public function rawCommand(string $sqlCommand): ICommandResult |
||
197 | } |
||
198 | |||
199 | public function executeStatement($sqlStatement): IResult |
||
200 | { |
||
201 | return $this->stmtExec->executeStatement($sqlStatement); |
||
202 | } |
||
203 | |||
204 | public function executeStatementAsync($sqlStatement): IAsyncResult |
||
205 | { |
||
206 | return $this->stmtExec->executeStatementAsync($sqlStatement); |
||
207 | } |
||
208 | |||
209 | public function runScript(string $sqlScript): array |
||
210 | { |
||
211 | return $this->stmtExec->runScript($sqlScript); |
||
212 | } |
||
213 | |||
214 | public function getStatementExceptionFactory(): StatementExceptionFactory |
||
215 | { |
||
216 | return $this->stmtExec->getStatementExceptionFactory(); |
||
217 | } |
||
218 | |||
219 | //endregion |
||
220 | |||
221 | //region Transaction Control |
||
222 | |||
223 | public function inTransaction(): bool |
||
224 | { |
||
225 | return $this->txCtl->inTransaction(); |
||
226 | } |
||
227 | |||
228 | public function startTransaction($transactionOptions = 0): ITxHandle |
||
229 | { |
||
230 | return $this->txCtl->startTransaction($transactionOptions); |
||
231 | } |
||
232 | |||
233 | public function setupSubsequentTransactions($transactionOptions): void |
||
234 | { |
||
235 | $this->txCtl->setupSubsequentTransactions($transactionOptions); |
||
236 | } |
||
237 | |||
238 | public function getDefaultTxConfig(): TxConfig |
||
239 | { |
||
240 | return $this->txCtl->getDefaultTxConfig(); |
||
241 | } |
||
242 | |||
243 | public function commitPreparedTransaction(string $name): void |
||
244 | { |
||
245 | $this->txCtl->commitPreparedTransaction($name); |
||
246 | } |
||
247 | |||
248 | public function rollbackPreparedTransaction(string $name): void |
||
249 | { |
||
250 | $this->txCtl->rollbackPreparedTransaction($name); |
||
251 | } |
||
252 | |||
253 | public function listPreparedTransactions(): IQueryResult |
||
254 | { |
||
255 | return $this->txCtl->listPreparedTransactions(); |
||
256 | } |
||
257 | |||
258 | //endregion |
||
259 | |||
260 | //region Transaction observing |
||
261 | |||
262 | public function addTransactionControlObserver(ITransactionControlObserver $observer): void |
||
263 | { |
||
264 | $this->txCtl->addObserver($observer); |
||
265 | } |
||
266 | |||
267 | public function removeTransactionControlObserver(ITransactionControlObserver $observer): void |
||
268 | { |
||
269 | $this->txCtl->removeObserver($observer); |
||
270 | } |
||
271 | |||
272 | public function removeAllTransactionControlObservers(): void |
||
273 | { |
||
274 | $this->txCtl->removeAllObservers(); |
||
275 | } |
||
276 | |||
277 | //endregion |
||
278 | |||
279 | //region IPC Control |
||
280 | |||
281 | public function getBackendPID(): int |
||
282 | { |
||
283 | return $this->ipcCtl->getBackendPID(); |
||
284 | } |
||
285 | |||
286 | public function notify(string $channel, ?string $payload = null): void |
||
287 | { |
||
288 | $this->ipcCtl->notify($channel, $payload); |
||
289 | } |
||
290 | |||
291 | public function listen(string $channel): void |
||
292 | { |
||
293 | $this->ipcCtl->listen($channel); |
||
294 | } |
||
295 | |||
296 | public function unlisten(string $channel): void |
||
297 | { |
||
298 | $this->ipcCtl->unlisten($channel); |
||
299 | } |
||
300 | |||
301 | public function unlistenAll(): void |
||
302 | { |
||
303 | $this->ipcCtl->unlistenAll(); |
||
304 | } |
||
305 | |||
306 | public function pollNotification(): ?Notification |
||
307 | { |
||
308 | return $this->ipcCtl->pollNotification(); |
||
309 | } |
||
310 | |||
311 | public function waitForNotification(int $millisecondTimeout): ?Notification |
||
312 | { |
||
313 | return $this->ipcCtl->waitForNotification($millisecondTimeout); |
||
314 | } |
||
315 | |||
316 | //endregion |
||
317 | |||
318 | //region Cursor Control |
||
319 | |||
320 | public function declareCursor(string $name, IRelationDefinition $relationDefinition, int $options = 0): ICursor |
||
321 | { |
||
322 | return $this->cursorCtl->declareCursor($name, $relationDefinition, $options); |
||
323 | } |
||
324 | |||
325 | public function getAllCursors(): array |
||
326 | { |
||
327 | return $this->cursorCtl->getAllCursors(); |
||
328 | } |
||
329 | |||
330 | public function closeAllCursors(): void |
||
331 | { |
||
332 | $this->cursorCtl->closeAllCursors(); |
||
333 | } |
||
334 | |||
335 | //endregion |
||
336 | |||
337 | //region Cache Control |
||
338 | |||
339 | public function isCacheEnabled(): bool |
||
340 | { |
||
341 | return $this->cacheCtl->isCacheEnabled(); |
||
342 | } |
||
343 | |||
344 | public function cachePermanently(string $cacheKey, $object): bool |
||
345 | { |
||
346 | /** @noinspection PhpUnhandledExceptionInspection PhpStorm bug WI-38168 */ |
||
347 | return $this->cacheCtl->cachePermanently($cacheKey, $object); |
||
348 | } |
||
349 | |||
350 | public function getCached(string $cacheKey) |
||
354 | } |
||
355 | |||
356 | public function flushCache(string $cacheKey): bool |
||
357 | { |
||
358 | /** @noinspection PhpUnhandledExceptionInspection PhpStorm bug WI-38168 */ |
||
359 | return $this->cacheCtl->flushCache($cacheKey); |
||
360 | } |
||
361 | |||
362 | //endregion |
||
363 | } |
||
364 |