| Total Complexity | 83 |
| Total Lines | 670 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 67 | class Connection |
||
| 68 | { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The PDO instance |
||
| 72 | * @var PDO|null |
||
| 73 | */ |
||
| 74 | protected ?PDO $pdo; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The database driver name to use |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected string $driverName = ''; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The PDO dsn |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected string $dsn = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The PDO connection options |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected array $options = [ |
||
| 93 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 94 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, |
||
| 95 | PDO::ATTR_STRINGIFY_FETCHES => false, |
||
| 96 | PDO::ATTR_EMULATE_PREPARES => false, |
||
| 97 | ]; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The list of SQL command to execute after connection |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected array $commands = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The driver to use |
||
| 107 | * @var Driver|null |
||
| 108 | */ |
||
| 109 | protected ?Driver $driver = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The Schema instance to use |
||
| 113 | * @var Schema|null |
||
| 114 | */ |
||
| 115 | protected ?Schema $schema = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The driver options |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected array $driverOptions = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var Logger|null |
||
| 125 | */ |
||
| 126 | protected ?Logger $logger = null; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Connection constructor. |
||
| 130 | * @param array $config |
||
| 131 | * @param Logger $logger |
||
| 132 | * @throws ConnectionException |
||
| 133 | */ |
||
| 134 | public function __construct(array $config = [], ?Logger $logger = null) |
||
| 135 | { |
||
| 136 | $this->logger = $logger ? $logger : new Logger(new NullLogger()); |
||
| 137 | |||
| 138 | $defaultConfig = [ |
||
| 139 | 'driver' => 'mysql', |
||
| 140 | 'charset' => 'UTF8', //only for some drivers, |
||
| 141 | 'appname' => '', //only for MSSQL, DBLIB, |
||
| 142 | 'hostname' => 'localhost', |
||
| 143 | 'username' => '', |
||
| 144 | 'password' => '', |
||
| 145 | 'port' => null, |
||
| 146 | 'database' => '', |
||
| 147 | 'collation' => 'utf8_general_ci', //only for MySQL |
||
| 148 | 'socket' => '', //only for MySQL |
||
| 149 | 'options' => [], |
||
| 150 | 'commands' => [], |
||
| 151 | ]; |
||
| 152 | |||
| 153 | $dbConfig = array_merge($defaultConfig, $config); |
||
| 154 | |||
| 155 | if (is_string($dbConfig['driver'])) { |
||
| 156 | $this->driverName = strtolower($dbConfig['driver']); |
||
| 157 | } |
||
| 158 | |||
| 159 | $options = $this->options; |
||
| 160 | if (is_array($dbConfig['options'])) { |
||
| 161 | $options = array_merge($options, $dbConfig['options']); |
||
| 162 | } |
||
| 163 | |||
| 164 | $commands = $this->commands; |
||
| 165 | if (is_array($dbConfig['commands'])) { |
||
| 166 | $commands = array_merge($commands, $dbConfig['commands']); |
||
| 167 | } |
||
| 168 | |||
| 169 | $port = null; |
||
| 170 | $attr = []; |
||
| 171 | |||
| 172 | if (is_int($dbConfig['port'])) { |
||
| 173 | $port = $dbConfig['port']; |
||
| 174 | } |
||
| 175 | |||
| 176 | $driverName = $this->driverName; |
||
| 177 | switch ($driverName) { |
||
| 178 | case 'mysql': |
||
| 179 | case 'pgsql': |
||
| 180 | $attr = [ |
||
| 181 | 'driver' => $driverName, |
||
| 182 | 'dbname' => $dbConfig['database'], |
||
| 183 | 'host' => $dbConfig['hostname'], |
||
| 184 | ]; |
||
| 185 | |||
| 186 | if ($port > 0) { |
||
| 187 | $attr['port'] = $port; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($driverName === 'mysql'){ |
||
| 191 | //Make MySQL using standard quoted identifier |
||
| 192 | $commands[] = 'SET SQL_MODE=ANSI_QUOTES'; |
||
| 193 | |||
| 194 | if (!empty($dbConfig['socket'])) { |
||
| 195 | $attr['unix_socket'] = $dbConfig['socket']; |
||
| 196 | |||
| 197 | unset($attr['host']); |
||
| 198 | unset($attr['port']); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | break; |
||
| 202 | case 'sqlsrv': |
||
| 203 | //Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting |
||
| 204 | $commands[] = 'SET QUOTED_IDENTIFIER ON'; |
||
| 205 | |||
| 206 | //Make ANSI_NULLS is ON for NULL value |
||
| 207 | $commands[] = 'SET ANSI_NULLS ON'; |
||
| 208 | |||
| 209 | $attr = [ |
||
| 210 | 'driver' => 'sqlsrv', |
||
| 211 | 'Server' => $dbConfig['hostname'] |
||
| 212 | . ($port > 0 ? ':' . $port : ''), |
||
| 213 | 'Database' => $dbConfig['database'] |
||
| 214 | ]; |
||
| 215 | |||
| 216 | if (!empty($dbConfig['appname'])) { |
||
| 217 | $attr['APP'] = $dbConfig['appname']; |
||
| 218 | } |
||
| 219 | |||
| 220 | $attributes = [ |
||
| 221 | 'ApplicationIntent', |
||
| 222 | 'AttachDBFileName', |
||
| 223 | 'Authentication', |
||
| 224 | 'ColumnEncryption', |
||
| 225 | 'ConnectionPooling', |
||
| 226 | 'Encrypt', |
||
| 227 | 'Failover_Partner', |
||
| 228 | 'KeyStoreAuthentication', |
||
| 229 | 'KeyStorePrincipalId', |
||
| 230 | 'KeyStoreSecret', |
||
| 231 | 'LoginTimeout', |
||
| 232 | 'MultipleActiveResultSets', |
||
| 233 | 'MultiSubnetFailover', |
||
| 234 | 'Scrollable', |
||
| 235 | 'TraceFile', |
||
| 236 | 'TraceOn', |
||
| 237 | 'TransactionIsolation', |
||
| 238 | 'TransparentNetworkIPResolution', |
||
| 239 | 'TrustServerCertificate', |
||
| 240 | 'WSID', |
||
| 241 | ]; |
||
| 242 | |||
| 243 | foreach ($attributes as $attribute) { |
||
| 244 | $keyname = strtolower(preg_replace( |
||
| 245 | ['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], |
||
| 246 | '$1_$2', |
||
| 247 | $attribute |
||
| 248 | )); |
||
| 249 | |||
| 250 | if (isset($dbConfig[$keyname])) { |
||
| 251 | $attr[$attribute] = $dbConfig[$keyname]; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | break; |
||
| 255 | case 'oci': |
||
| 256 | case 'oracle': |
||
| 257 | $database = $dbConfig['database']; |
||
| 258 | $attr = [ |
||
| 259 | 'driver' => 'oci', |
||
| 260 | 'dbname' => '//' . $dbConfig['hostname'] |
||
| 261 | . ($port > 0 ? ':' . $port : ':1521') . '/' . $database |
||
| 262 | ]; |
||
| 263 | |||
| 264 | $attr['charset'] = $dbConfig['charset']; |
||
| 265 | break; |
||
| 266 | case 'sqlite': |
||
| 267 | $attr = [ |
||
| 268 | 'driver' => 'sqlite', |
||
| 269 | $dbConfig['database'] |
||
| 270 | ]; |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | |||
| 274 | |||
| 275 | if (empty($attr)) { |
||
| 276 | throw new InvalidArgumentException('Invalid database options supplied'); |
||
| 277 | } |
||
| 278 | |||
| 279 | $driver = $attr['driver']; |
||
| 280 | if (!in_array($driver, PDO::getAvailableDrivers())) { |
||
| 281 | throw new InvalidArgumentException(sprintf( |
||
| 282 | 'Invalid database driver [%s], must be one of [%s]', |
||
| 283 | $driver, |
||
| 284 | implode(', ', PDO::getAvailableDrivers()) |
||
| 285 | )); |
||
| 286 | } |
||
| 287 | |||
| 288 | unset($attr['driver']); |
||
| 289 | |||
| 290 | $params = []; |
||
| 291 | foreach ($attr as $key => $value) { |
||
| 292 | $params[] = is_int($key) ? $value : $key . '=' . $value; |
||
| 293 | } |
||
| 294 | |||
| 295 | $dsn = $driver . ':' . implode(';', $params); |
||
| 296 | if (in_array($driver, ['mysql', 'pgsql', 'sqlsrv'])) { |
||
| 297 | $commands[] = 'SET NAMES "' . $dbConfig['charset'] . '"' . ( |
||
| 298 | $this->driverName === 'mysql' |
||
| 299 | ? ' COLLATE "' . $dbConfig['collation'] . '"' |
||
| 300 | : '' |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | |||
| 304 | $this->dsn = $dsn; |
||
| 305 | $this->commands = $commands; |
||
| 306 | $this->options = $options; |
||
| 307 | |||
| 308 | try { |
||
| 309 | $this->pdo = new PDO( |
||
| 310 | $dsn, |
||
| 311 | isset($config['username']) ? $config['username'] : '', |
||
| 312 | isset($config['password']) ? $config['password'] : '', |
||
| 313 | $options |
||
| 314 | ); |
||
| 315 | |||
| 316 | foreach ($commands as $command) { |
||
| 317 | $this->pdo->exec($command); |
||
| 318 | } |
||
| 319 | } catch (PDOException $exception) { |
||
| 320 | $this->logger->emergency('Can not connect to database. Error message: {error}', [ |
||
| 321 | 'exception' => $exception, |
||
| 322 | 'error' => $exception->getMessage() |
||
| 323 | ]); |
||
| 324 | throw new ConnectionException($exception->getMessage()); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param Logger $logger |
||
| 330 | * @return self |
||
| 331 | */ |
||
| 332 | public function setLogger(Logger $logger): self |
||
| 333 | { |
||
| 334 | $this->logger = $logger; |
||
| 335 | |||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return the current driver instance |
||
| 341 | * @return Driver |
||
| 342 | */ |
||
| 343 | public function getDriver(): Driver |
||
| 344 | { |
||
| 345 | if ($this->driver === null) { |
||
| 346 | $this->setDefaultDriver(); |
||
| 347 | } |
||
| 348 | return $this->driver; |
||
|
|
|||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param Driver $driver |
||
| 353 | * @return self |
||
| 354 | */ |
||
| 355 | public function setDriver(Driver $driver): self |
||
| 356 | { |
||
| 357 | $this->driver = $driver; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return the current Schema instance |
||
| 364 | * @return Schema |
||
| 365 | */ |
||
| 366 | public function getSchema(): Schema |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param Schema $schema |
||
| 376 | * @return self |
||
| 377 | */ |
||
| 378 | public function setSchema(Schema $schema): self |
||
| 379 | { |
||
| 380 | $this->schema = $schema; |
||
| 381 | |||
| 382 | return $this; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set PDO Connection options |
||
| 387 | * @param array $options |
||
| 388 | * @return self |
||
| 389 | */ |
||
| 390 | public function options(array $options): self |
||
| 391 | { |
||
| 392 | foreach ($options as $name => $value) { |
||
| 393 | $this->option($name, $value); |
||
| 394 | } |
||
| 395 | |||
| 396 | return $this; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Set the PDO connection option |
||
| 401 | * @param string $name |
||
| 402 | * @param mixed $value |
||
| 403 | * @return self |
||
| 404 | */ |
||
| 405 | public function option(string $name, $value): self |
||
| 406 | { |
||
| 407 | $this->options[$name] = $value; |
||
| 408 | |||
| 409 | return $this; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set connection to be persistent |
||
| 414 | * @param bool $value |
||
| 415 | * @return self |
||
| 416 | */ |
||
| 417 | public function persistent(bool $value = true): self |
||
| 418 | { |
||
| 419 | $this->pdo->setAttribute(PDO::ATTR_PERSISTENT, $value); |
||
| 420 | |||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Set the date format to use for the current driver |
||
| 426 | * @param string $format |
||
| 427 | * @return self |
||
| 428 | */ |
||
| 429 | public function setDateFormat(string $format): self |
||
| 430 | { |
||
| 431 | $this->driverOptions['dateFormat'] = $format; |
||
| 432 | |||
| 433 | return $this; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Set the quote identifier to use for the current driver |
||
| 438 | * @param string $identifier |
||
| 439 | * @return self |
||
| 440 | */ |
||
| 441 | public function setQuoteIdentifier(string $identifier): self |
||
| 442 | { |
||
| 443 | $this->driverOptions['identifier'] = $identifier; |
||
| 444 | |||
| 445 | return $this; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function getDsn(): string |
||
| 452 | { |
||
| 453 | return $this->dsn; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Return the name of the connection driver |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getDriverName(): string |
||
| 461 | { |
||
| 462 | return $this->driverName; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Return the instance of the PDO |
||
| 467 | * @return PDO |
||
| 468 | */ |
||
| 469 | public function getPDO(): PDO |
||
| 470 | { |
||
| 471 | return $this->pdo; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * CLose the connection |
||
| 476 | */ |
||
| 477 | public function disconnect(): void |
||
| 478 | { |
||
| 479 | $this->pdo = null; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Execute the SQL query and return the result |
||
| 484 | * @param string $sql |
||
| 485 | * @param array $params the query parameters |
||
| 486 | * @return ResultSet |
||
| 487 | * @throws QueryException |
||
| 488 | */ |
||
| 489 | public function query(string $sql, array $params = []): ResultSet |
||
| 490 | { |
||
| 491 | $prepared = $this->prepare($sql, $params); |
||
| 492 | $this->execute($prepared); |
||
| 493 | |||
| 494 | return new ResultSet($prepared['statement']); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Direct execute the SQL query |
||
| 499 | * @param string $sql |
||
| 500 | * @param array $params the query parameters |
||
| 501 | * @return mixed |
||
| 502 | * @throws QueryException |
||
| 503 | */ |
||
| 504 | public function exec(string $sql, array $params = []) |
||
| 505 | { |
||
| 506 | return $this->execute($this->prepare($sql, $params)); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Execute the SQL query and return the number |
||
| 511 | * of affected rows |
||
| 512 | * @param string $sql |
||
| 513 | * @param array $params the query parameters |
||
| 514 | * @return int |
||
| 515 | * @throws QueryException |
||
| 516 | */ |
||
| 517 | public function count(string $sql, array $params = []): int |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Execute the SQL query and return the first column result |
||
| 530 | * @param string $sql |
||
| 531 | * @param array $params the query parameters |
||
| 532 | * @return mixed |
||
| 533 | * @throws QueryException |
||
| 534 | */ |
||
| 535 | public function column(string $sql, array $params = []) |
||
| 536 | { |
||
| 537 | $prepared = $this->prepare($sql, $params); |
||
| 538 | $this->execute($prepared); |
||
| 539 | |||
| 540 | $result = $prepared['statement']->fetchColumn(); |
||
| 541 | $prepared['statement']->closeCursor(); |
||
| 542 | |||
| 543 | return $result; |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param callable $callback |
||
| 548 | * @param mixed|null $that |
||
| 549 | * |
||
| 550 | * @return mixed |
||
| 551 | * |
||
| 552 | * @throws ConnectionException |
||
| 553 | */ |
||
| 554 | public function transaction( |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Change the query parameters placeholder with the value |
||
| 584 | * @param string $query |
||
| 585 | * @param array $params |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | protected function replaceParameters(string $query, array $params): string |
||
| 609 | ); |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Prepare the query |
||
| 614 | * @param string $query |
||
| 615 | * @param array $params |
||
| 616 | * @return array |
||
| 617 | * @throws QueryException |
||
| 618 | */ |
||
| 619 | protected function prepare(string $query, array $params): array |
||
| 620 | { |
||
| 621 | try { |
||
| 622 | $statement = $this->pdo->prepare($query); |
||
| 623 | } catch (PDOException $exception) { |
||
| 624 | $sql = $this->replaceParameters($query, $params); |
||
| 625 | $this->logger->error('Error when prepare query [{sql}]. Error message: {error}', [ |
||
| 626 | 'exception' => $exception, |
||
| 627 | 'error' => $exception->getMessage(), |
||
| 628 | 'sql' => $sql |
||
| 629 | ]); |
||
| 630 | throw new QueryException( |
||
| 631 | $exception->getMessage() . ' [' . $sql . ']', |
||
| 632 | (int) $exception->getCode(), |
||
| 633 | $exception->getPrevious() |
||
| 634 | ); |
||
| 635 | } |
||
| 636 | |||
| 637 | return [ |
||
| 638 | 'statement' => $statement, |
||
| 639 | 'query' => $query, |
||
| 640 | 'params' => $params |
||
| 641 | ]; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Execute the prepared query |
||
| 646 | * @param array $prepared |
||
| 647 | * @return bool the status of the execution |
||
| 648 | * @throws QueryException |
||
| 649 | */ |
||
| 650 | protected function execute(array $prepared): bool |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Bind the parameters values |
||
| 688 | * @param PDOStatement $statement |
||
| 689 | * @param array $values |
||
| 690 | */ |
||
| 691 | protected function bindValues(PDOStatement $statement, array $values): void |
||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Set the default driver instance using current driver name |
||
| 709 | * @return void |
||
| 710 | */ |
||
| 711 | protected function setDefaultDriver(): void |
||
| 737 | } |
||
| 738 | } |
||
| 739 |