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