| Total Complexity | 81 |
| Total Lines | 694 |
| 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 = null; |
||
| 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 | * The connection configurations |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected array $config = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The connection parameters |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected array $params = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var Logger|null |
||
| 137 | */ |
||
| 138 | protected ?Logger $logger = null; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Connection constructor. |
||
| 142 | * @param array $config |
||
| 143 | * @param Logger $logger |
||
| 144 | * @throws ConnectionException |
||
| 145 | */ |
||
| 146 | public function __construct(array $config = [], ?Logger $logger = null) |
||
| 147 | { |
||
| 148 | $this->logger = $logger ? $logger : new Logger(new NullLogger()); |
||
| 149 | |||
| 150 | $defaultConfig = [ |
||
| 151 | 'driver' => 'mysql', |
||
| 152 | 'charset' => 'UTF8', //only for some drivers, |
||
| 153 | 'appname' => '', //only for MSSQL, DBLIB, |
||
| 154 | 'hostname' => 'localhost', |
||
| 155 | 'username' => '', |
||
| 156 | 'password' => '', |
||
| 157 | 'port' => null, |
||
| 158 | 'database' => '', |
||
| 159 | 'auto_connect' => false, |
||
| 160 | 'collation' => 'utf8_general_ci', //only for MySQL |
||
| 161 | 'socket' => '', //only for MySQL |
||
| 162 | 'options' => [], |
||
| 163 | 'commands' => [], |
||
| 164 | ]; |
||
| 165 | |||
| 166 | $dbConfig = array_merge($defaultConfig, $config); |
||
| 167 | $this->config = $dbConfig; |
||
| 168 | |||
| 169 | $this->driverName = strtolower((string) $dbConfig['driver']); |
||
| 170 | |||
| 171 | $options = array_replace($this->options, (array) $dbConfig['options']); |
||
| 172 | |||
| 173 | $commands = array_merge($this->commands, $dbConfig['commands']); |
||
| 174 | |||
| 175 | $port = null; |
||
| 176 | $attr = []; |
||
| 177 | |||
| 178 | if (is_int($dbConfig['port'])) { |
||
| 179 | $port = $dbConfig['port']; |
||
| 180 | } |
||
| 181 | |||
| 182 | $driverName = $this->driverName; |
||
| 183 | switch ($driverName) { |
||
| 184 | case 'mysql': |
||
| 185 | case 'pgsql': |
||
| 186 | $attr = [ |
||
| 187 | 'driver' => $driverName, |
||
| 188 | 'dbname' => $dbConfig['database'], |
||
| 189 | 'host' => $dbConfig['hostname'], |
||
| 190 | ]; |
||
| 191 | |||
| 192 | if ($port > 0) { |
||
| 193 | $attr['port'] = $port; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($driverName === 'mysql'){ |
||
| 197 | //Make MySQL using standard quoted identifier |
||
| 198 | $commands[] = 'SET SQL_MODE=ANSI_QUOTES'; |
||
| 199 | |||
| 200 | if (!empty($dbConfig['socket'])) { |
||
| 201 | $attr['unix_socket'] = $dbConfig['socket']; |
||
| 202 | |||
| 203 | unset($attr['host']); |
||
| 204 | unset($attr['port']); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | break; |
||
| 208 | case 'sqlsrv': |
||
| 209 | //Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting |
||
| 210 | $commands[] = 'SET QUOTED_IDENTIFIER ON'; |
||
| 211 | |||
| 212 | //Make ANSI_NULLS is ON for NULL value |
||
| 213 | $commands[] = 'SET ANSI_NULLS ON'; |
||
| 214 | |||
| 215 | $attr = [ |
||
| 216 | 'driver' => 'sqlsrv', |
||
| 217 | 'Server' => $dbConfig['hostname'] |
||
| 218 | . ($port > 0 ? ':' . $port : ''), |
||
| 219 | 'Database' => $dbConfig['database'] |
||
| 220 | ]; |
||
| 221 | |||
| 222 | if (!empty($dbConfig['appname'])) { |
||
| 223 | $attr['APP'] = $dbConfig['appname']; |
||
| 224 | } |
||
| 225 | |||
| 226 | $attributes = [ |
||
| 227 | 'ApplicationIntent', |
||
| 228 | 'AttachDBFileName', |
||
| 229 | 'Authentication', |
||
| 230 | 'ColumnEncryption', |
||
| 231 | 'ConnectionPooling', |
||
| 232 | 'Encrypt', |
||
| 233 | 'Failover_Partner', |
||
| 234 | 'KeyStoreAuthentication', |
||
| 235 | 'KeyStorePrincipalId', |
||
| 236 | 'KeyStoreSecret', |
||
| 237 | 'LoginTimeout', |
||
| 238 | 'MultipleActiveResultSets', |
||
| 239 | 'MultiSubnetFailover', |
||
| 240 | 'Scrollable', |
||
| 241 | 'TraceFile', |
||
| 242 | 'TraceOn', |
||
| 243 | 'TransactionIsolation', |
||
| 244 | 'TransparentNetworkIPResolution', |
||
| 245 | 'TrustServerCertificate', |
||
| 246 | 'WSID', |
||
| 247 | ]; |
||
| 248 | |||
| 249 | foreach ($attributes as $attribute) { |
||
| 250 | $keyname = strtolower(preg_replace( |
||
| 251 | ['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], |
||
| 252 | '$1_$2', |
||
| 253 | $attribute |
||
| 254 | )); |
||
| 255 | |||
| 256 | if (isset($dbConfig[$keyname])) { |
||
| 257 | $attr[$attribute] = $dbConfig[$keyname]; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | case 'oci': |
||
| 262 | case 'oracle': |
||
| 263 | $database = $dbConfig['database']; |
||
| 264 | $attr = [ |
||
| 265 | 'driver' => 'oci', |
||
| 266 | 'dbname' => '//' . $dbConfig['hostname'] |
||
| 267 | . ($port > 0 ? ':' . $port : ':1521') . '/' . $database |
||
| 268 | ]; |
||
| 269 | |||
| 270 | $attr['charset'] = $dbConfig['charset']; |
||
| 271 | break; |
||
| 272 | case 'sqlite': |
||
| 273 | $attr = [ |
||
| 274 | 'driver' => 'sqlite', |
||
| 275 | $dbConfig['database'] |
||
| 276 | ]; |
||
| 277 | break; |
||
| 278 | } |
||
| 279 | |||
| 280 | |||
| 281 | if (empty($attr)) { |
||
| 282 | throw new InvalidArgumentException('Invalid database options supplied'); |
||
| 283 | } |
||
| 284 | |||
| 285 | $this->params = $attr; |
||
| 286 | |||
| 287 | $driver = $attr['driver']; |
||
| 288 | if (!in_array($driver, PDO::getAvailableDrivers())) { |
||
| 289 | throw new InvalidArgumentException(sprintf( |
||
| 290 | 'Invalid database driver [%s], must be one of [%s]', |
||
| 291 | $driver, |
||
| 292 | implode(', ', PDO::getAvailableDrivers()) |
||
| 293 | )); |
||
| 294 | } |
||
| 295 | |||
| 296 | unset($attr['driver']); |
||
| 297 | |||
| 298 | $params = []; |
||
| 299 | foreach ($attr as $key => $value) { |
||
| 300 | $params[] = is_int($key) ? $value : $key . '=' . $value; |
||
| 301 | } |
||
| 302 | |||
| 303 | $dsn = $driver . ':' . implode(';', $params); |
||
| 304 | if (in_array($driver, ['mysql', 'pgsql', 'sqlsrv'])) { |
||
| 305 | $commands[] = 'SET NAMES "' . $dbConfig['charset'] . '"' . ( |
||
| 306 | $this->driverName === 'mysql' |
||
| 307 | ? ' COLLATE "' . $dbConfig['collation'] . '"' |
||
| 308 | : '' |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | |||
| 312 | $this->dsn = $dsn; |
||
| 313 | $this->commands = $commands; |
||
| 314 | $this->options = $options; |
||
| 315 | |||
| 316 | if($dbConfig['auto_connect'] === true){ |
||
|
|
|||
| 317 | $this->connect(); |
||
| 318 | } |
||
| 319 | |||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Connect to the database |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function connect():bool{ |
||
| 327 | try { |
||
| 328 | $this->pdo = new PDO( |
||
| 329 | $this->dsn, |
||
| 330 | $this->config['username'], |
||
| 331 | $this->config['password'], |
||
| 332 | $this->options |
||
| 333 | ); |
||
| 334 | |||
| 335 | foreach ($this->commands as $command) { |
||
| 336 | $this->pdo->exec($command); |
||
| 337 | } |
||
| 338 | } catch (PDOException $exception) { |
||
| 339 | $this->logger->emergency('Can not connect to database. Error message: {error}', [ |
||
| 340 | 'exception' => $exception, |
||
| 341 | 'error' => $exception->getMessage() |
||
| 342 | ]); |
||
| 343 | throw new ConnectionException($exception->getMessage()); |
||
| 344 | } |
||
| 345 | |||
| 346 | return $this->pdo !== null; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param Logger $logger |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | public function setLogger(Logger $logger): self |
||
| 354 | { |
||
| 355 | $this->logger = $logger; |
||
| 356 | |||
| 357 | return $this; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return the current driver instance |
||
| 362 | * @return Driver |
||
| 363 | */ |
||
| 364 | public function getDriver(): Driver |
||
| 365 | { |
||
| 366 | if ($this->driver === null) { |
||
| 367 | $this->setDefaultDriver(); |
||
| 368 | } |
||
| 369 | return $this->driver; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param Driver $driver |
||
| 374 | * @return self |
||
| 375 | */ |
||
| 376 | public function setDriver(Driver $driver): self |
||
| 377 | { |
||
| 378 | $this->driver = $driver; |
||
| 379 | |||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Return the current Schema instance |
||
| 385 | * @return Schema |
||
| 386 | */ |
||
| 387 | public function getSchema(): Schema |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param Schema $schema |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | public function setSchema(Schema $schema): self |
||
| 400 | { |
||
| 401 | $this->schema = $schema; |
||
| 402 | |||
| 403 | return $this; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Set PDO Connection options |
||
| 408 | * @param array $options |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | public function options(array $options): self |
||
| 412 | { |
||
| 413 | foreach ($options as $name => $value) { |
||
| 414 | $this->option($name, $value); |
||
| 415 | } |
||
| 416 | |||
| 417 | return $this; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set the PDO connection option |
||
| 422 | * @param string $name |
||
| 423 | * @param mixed $value |
||
| 424 | * @return self |
||
| 425 | */ |
||
| 426 | public function option(string $name, $value): self |
||
| 427 | { |
||
| 428 | $this->options[$name] = $value; |
||
| 429 | |||
| 430 | return $this; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set connection to be persistent |
||
| 435 | * @param bool $value |
||
| 436 | * @return self |
||
| 437 | */ |
||
| 438 | public function persistent(bool $value = true): self |
||
| 439 | { |
||
| 440 | $this->options[PDO::ATTR_PERSISTENT] = $value; |
||
| 441 | |||
| 442 | return $this; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set the date format to use for the current driver |
||
| 447 | * @param string $format |
||
| 448 | * @return self |
||
| 449 | */ |
||
| 450 | public function setDateFormat(string $format): self |
||
| 451 | { |
||
| 452 | $this->driverOptions['dateFormat'] = $format; |
||
| 453 | |||
| 454 | return $this; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Set the quote identifier to use for the current driver |
||
| 459 | * @param string $identifier |
||
| 460 | * @return self |
||
| 461 | */ |
||
| 462 | public function setQuoteIdentifier(string $identifier): self |
||
| 463 | { |
||
| 464 | $this->driverOptions['identifier'] = $identifier; |
||
| 465 | |||
| 466 | return $this; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getDsn(): string |
||
| 473 | { |
||
| 474 | return $this->dsn; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Return the name of the connection driver |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getDriverName(): string |
||
| 482 | { |
||
| 483 | return $this->driverName; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Return the instance of the PDO |
||
| 488 | * @return PDO |
||
| 489 | */ |
||
| 490 | public function getPDO(): PDO |
||
| 491 | { |
||
| 492 | if($this->pdo === null){ |
||
| 493 | $this->connect(); |
||
| 494 | } |
||
| 495 | return $this->pdo; |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * CLose the connection |
||
| 500 | */ |
||
| 501 | public function disconnect(): void |
||
| 502 | { |
||
| 503 | $this->pdo = null; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Execute the SQL query and return the result |
||
| 508 | * @param string $sql |
||
| 509 | * @param array $params the query parameters |
||
| 510 | * @return ResultSet |
||
| 511 | * @throws QueryException |
||
| 512 | */ |
||
| 513 | public function query(string $sql, array $params = []): ResultSet |
||
| 514 | { |
||
| 515 | $prepared = $this->prepare($sql, $params); |
||
| 516 | $this->execute($prepared); |
||
| 517 | |||
| 518 | return new ResultSet($prepared['statement']); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Direct execute the SQL query |
||
| 523 | * @param string $sql |
||
| 524 | * @param array $params the query parameters |
||
| 525 | * @return mixed |
||
| 526 | * @throws QueryException |
||
| 527 | */ |
||
| 528 | public function exec(string $sql, array $params = []) |
||
| 529 | { |
||
| 530 | return $this->execute($this->prepare($sql, $params)); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Execute the SQL query and return the number |
||
| 535 | * of affected rows |
||
| 536 | * @param string $sql |
||
| 537 | * @param array $params the query parameters |
||
| 538 | * @return int |
||
| 539 | * @throws QueryException |
||
| 540 | */ |
||
| 541 | public function count(string $sql, array $params = []): int |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Execute the SQL query and return the first column result |
||
| 554 | * @param string $sql |
||
| 555 | * @param array $params the query parameters |
||
| 556 | * @return mixed |
||
| 557 | * @throws QueryException |
||
| 558 | */ |
||
| 559 | public function column(string $sql, array $params = []) |
||
| 560 | { |
||
| 561 | $prepared = $this->prepare($sql, $params); |
||
| 562 | $this->execute($prepared); |
||
| 563 | |||
| 564 | $result = $prepared['statement']->fetchColumn(); |
||
| 565 | $prepared['statement']->closeCursor(); |
||
| 566 | |||
| 567 | return $result; |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param callable $callback |
||
| 572 | * @param mixed|null $that |
||
| 573 | * |
||
| 574 | * @return mixed |
||
| 575 | * |
||
| 576 | * @throws ConnectionException |
||
| 577 | */ |
||
| 578 | public function transaction( |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Change the query parameters placeholder with the value |
||
| 608 | * @param string $query |
||
| 609 | * @param array $params |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | protected function replaceParameters(string $query, array $params): string |
||
| 633 | ); |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Prepare the query |
||
| 638 | * @param string $query |
||
| 639 | * @param array $params |
||
| 640 | * @return array |
||
| 641 | * @throws QueryException |
||
| 642 | */ |
||
| 643 | protected function prepare(string $query, array $params): array |
||
| 644 | { |
||
| 645 | try { |
||
| 646 | $statement = $this->pdo->prepare($query); |
||
| 647 | } catch (PDOException $exception) { |
||
| 648 | $sql = $this->replaceParameters($query, $params); |
||
| 649 | $this->logger->error('Error when prepare query [{sql}]. Error message: {error}', [ |
||
| 650 | 'exception' => $exception, |
||
| 651 | 'error' => $exception->getMessage(), |
||
| 652 | 'sql' => $sql |
||
| 653 | ]); |
||
| 654 | throw new QueryException( |
||
| 655 | $exception->getMessage() . ' [' . $sql . ']', |
||
| 656 | (int) $exception->getCode(), |
||
| 657 | $exception->getPrevious() |
||
| 658 | ); |
||
| 659 | } |
||
| 660 | |||
| 661 | return [ |
||
| 662 | 'statement' => $statement, |
||
| 663 | 'query' => $query, |
||
| 664 | 'params' => $params |
||
| 665 | ]; |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Execute the prepared query |
||
| 670 | * @param array $prepared |
||
| 671 | * @return bool the status of the execution |
||
| 672 | * @throws QueryException |
||
| 673 | */ |
||
| 674 | protected function execute(array $prepared): bool |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Bind the parameters values |
||
| 712 | * @param PDOStatement $statement |
||
| 713 | * @param array $values |
||
| 714 | */ |
||
| 715 | protected function bindValues(PDOStatement $statement, array $values): void |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Set the default driver instance using current driver name |
||
| 733 | * @return void |
||
| 734 | */ |
||
| 735 | protected function setDefaultDriver(): void |
||
| 761 | } |
||
| 762 | } |
||
| 763 |