| Total Complexity | 55 |
| Total Lines | 339 |
| 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 |
||
| 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 |
||
| 226 | } |
||
| 227 | |||
| 228 | public function startTransaction($transactionOptions = 0): ITxHandle |
||
| 229 | { |
||
| 230 | return $this->txCtl->startTransaction($transactionOptions); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function startAutoTransaction($transactionOptions = 0): ITxHandle |
||
| 234 | { |
||
| 235 | return $this->txCtl->startAutoTransaction($transactionOptions); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function setupSubsequentTransactions($transactionOptions): void |
||
| 239 | { |
||
| 240 | $this->txCtl->setupSubsequentTransactions($transactionOptions); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getDefaultTxConfig(): TxConfig |
||
| 244 | { |
||
| 245 | return $this->txCtl->getDefaultTxConfig(); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function commitPreparedTransaction(string $name): void |
||
| 249 | { |
||
| 250 | $this->txCtl->commitPreparedTransaction($name); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function rollbackPreparedTransaction(string $name): void |
||
| 254 | { |
||
| 255 | $this->txCtl->rollbackPreparedTransaction($name); |
||
| 256 | } |
||
| 257 | |||
| 258 | public function listPreparedTransactions(): IQueryResult |
||
| 259 | { |
||
| 260 | return $this->txCtl->listPreparedTransactions(); |
||
| 261 | } |
||
| 262 | |||
| 263 | //endregion |
||
| 264 | |||
| 265 | //region Transaction observing |
||
| 266 | |||
| 267 | public function addTransactionControlObserver(ITransactionControlObserver $observer): void |
||
| 268 | { |
||
| 269 | $this->txCtl->addObserver($observer); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function removeTransactionControlObserver(ITransactionControlObserver $observer): void |
||
| 273 | { |
||
| 274 | $this->txCtl->removeObserver($observer); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function removeAllTransactionControlObservers(): void |
||
| 278 | { |
||
| 279 | $this->txCtl->removeAllObservers(); |
||
| 280 | } |
||
| 281 | |||
| 282 | //endregion |
||
| 283 | |||
| 284 | //region IPC Control |
||
| 285 | |||
| 286 | public function getBackendPID(): int |
||
| 287 | { |
||
| 288 | return $this->ipcCtl->getBackendPID(); |
||
| 289 | } |
||
| 290 | |||
| 291 | public function notify(string $channel, ?string $payload = null): void |
||
| 292 | { |
||
| 293 | $this->ipcCtl->notify($channel, $payload); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function listen(string $channel): void |
||
| 297 | { |
||
| 298 | $this->ipcCtl->listen($channel); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function unlisten(string $channel): void |
||
| 302 | { |
||
| 303 | $this->ipcCtl->unlisten($channel); |
||
| 304 | } |
||
| 305 | |||
| 306 | public function unlistenAll(): void |
||
| 307 | { |
||
| 308 | $this->ipcCtl->unlistenAll(); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function pollNotification(): ?Notification |
||
| 312 | { |
||
| 313 | return $this->ipcCtl->pollNotification(); |
||
| 314 | } |
||
| 315 | |||
| 316 | public function waitForNotification(int $millisecondTimeout): ?Notification |
||
| 317 | { |
||
| 318 | return $this->ipcCtl->waitForNotification($millisecondTimeout); |
||
| 319 | } |
||
| 320 | |||
| 321 | //endregion |
||
| 322 | |||
| 323 | //region Cursor Control |
||
| 324 | |||
| 325 | public function declareCursor(string $name, IRelationDefinition $relationDefinition, int $options = 0): ICursor |
||
| 326 | { |
||
| 327 | return $this->cursorCtl->declareCursor($name, $relationDefinition, $options); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function getAllCursors(): array |
||
| 331 | { |
||
| 332 | return $this->cursorCtl->getAllCursors(); |
||
| 333 | } |
||
| 334 | |||
| 335 | public function closeAllCursors(): void |
||
| 336 | { |
||
| 337 | $this->cursorCtl->closeAllCursors(); |
||
| 338 | } |
||
| 339 | |||
| 340 | //endregion |
||
| 341 | |||
| 342 | //region Cache Control |
||
| 343 | |||
| 344 | public function isCacheEnabled(): bool |
||
| 345 | { |
||
| 346 | return $this->cacheCtl->isCacheEnabled(); |
||
| 347 | } |
||
| 348 | |||
| 349 | public function cachePermanently(string $cacheKey, $object): bool |
||
| 350 | { |
||
| 351 | return $this->cacheCtl->cachePermanently($cacheKey, $object); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function getCached(string $cacheKey) |
||
| 357 | } |
||
| 358 | |||
| 359 | public function flushCache(string $cacheKey): bool |
||
| 360 | { |
||
| 361 | return $this->cacheCtl->flushCache($cacheKey); |
||
| 362 | } |
||
| 363 | |||
| 364 | //endregion |
||
| 365 | } |
||
| 366 |