| Total Complexity | 62 |
| Total Lines | 568 |
| 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 |
||
| 63 | class Connection |
||
| 64 | { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The PDO instance |
||
| 68 | * @var PDO |
||
| 69 | */ |
||
| 70 | protected PDO $pdo; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The PDO data source name |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected string $dsn = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The list of execution query logs |
||
| 80 | * @var array<int, array<string, mixed>> |
||
| 81 | */ |
||
| 82 | protected array $logs = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The driver to use |
||
| 86 | * @var Driver |
||
| 87 | */ |
||
| 88 | protected Driver $driver; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The Schema instance to use |
||
| 92 | * @var Schema |
||
| 93 | */ |
||
| 94 | protected Schema $schema; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The connection configuration |
||
| 98 | * @var ConfigurationInterface |
||
| 99 | */ |
||
| 100 | protected ConfigurationInterface $config; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The connection parameters |
||
| 104 | * @var array<int|string, mixed> |
||
| 105 | */ |
||
| 106 | protected array $params = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var Logger |
||
| 110 | */ |
||
| 111 | protected Logger $logger; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Connection constructor. |
||
| 115 | * @param ConfigurationInterface $config |
||
| 116 | * @param Logger $logger |
||
| 117 | * @throws ConnectionException |
||
| 118 | */ |
||
| 119 | public function __construct( |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Connect to the database |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function connect(): void |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * |
||
| 200 | * @param Logger $logger |
||
| 201 | * @return self |
||
| 202 | */ |
||
| 203 | public function setLogger(Logger $logger): self |
||
| 204 | { |
||
| 205 | $this->logger = $logger; |
||
| 206 | |||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Return the query execution logs |
||
| 212 | * @return array<int, array<string, mixed>> |
||
| 213 | */ |
||
| 214 | public function getLogs(): array |
||
| 215 | { |
||
| 216 | return $this->logs; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return the current connection parameters |
||
| 221 | * @return array<int|string, mixed> |
||
| 222 | */ |
||
| 223 | public function getParams(): array |
||
| 224 | { |
||
| 225 | return $this->params; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return the current connection configuration |
||
| 230 | * @return ConfigurationInterface |
||
| 231 | */ |
||
| 232 | public function getConfig(): ConfigurationInterface |
||
| 233 | { |
||
| 234 | return $this->config; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return the current driver instance |
||
| 239 | * @return Driver |
||
| 240 | */ |
||
| 241 | public function getDriver(): Driver |
||
| 242 | { |
||
| 243 | return $this->driver; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return the current Schema instance |
||
| 248 | * @return Schema |
||
| 249 | */ |
||
| 250 | public function getSchema(): Schema |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Set connection to be persistent |
||
| 257 | * @param bool $value |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | public function persistent(bool $value = true): self |
||
| 261 | { |
||
| 262 | $this->config->setOption(PDO::ATTR_PERSISTENT, $value); |
||
| 263 | |||
| 264 | return $this; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getDsn(): string |
||
| 271 | { |
||
| 272 | return $this->dsn; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return the instance of the PDO |
||
| 277 | * @return PDO |
||
| 278 | */ |
||
| 279 | public function getPDO(): PDO |
||
| 280 | { |
||
| 281 | return $this->pdo; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Execute the SQL query and return the result |
||
| 286 | * @param string $sql |
||
| 287 | * @param array<int, mixed> $params the query parameters |
||
| 288 | * @return ResultSet |
||
| 289 | * @throws QueryException |
||
| 290 | */ |
||
| 291 | public function query(string $sql, array $params = []): ResultSet |
||
| 292 | { |
||
| 293 | $prepared = $this->prepare($sql, $params); |
||
| 294 | $this->execute($prepared); |
||
| 295 | |||
| 296 | return new ResultSet($prepared['statement']); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Direct execute the SQL query |
||
| 301 | * @param string $sql |
||
| 302 | * @param array<int, mixed> $params the query parameters |
||
| 303 | * @return bool |
||
| 304 | * @throws QueryException |
||
| 305 | */ |
||
| 306 | public function exec(string $sql, array $params = []): bool |
||
| 307 | { |
||
| 308 | return $this->execute($this->prepare($sql, $params)); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Execute the SQL query and return the number |
||
| 313 | * of affected rows |
||
| 314 | * @param string $sql |
||
| 315 | * @param array<int, mixed> $params the query parameters |
||
| 316 | * @return int |
||
| 317 | * @throws QueryException |
||
| 318 | */ |
||
| 319 | public function count(string $sql, array $params = []): int |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Execute the SQL query and return the first column result |
||
| 332 | * @param string $sql |
||
| 333 | * @param array<int, mixed> $params the query parameters |
||
| 334 | * @return mixed |
||
| 335 | * @throws QueryException |
||
| 336 | */ |
||
| 337 | public function column(string $sql, array $params = []) |
||
| 338 | { |
||
| 339 | $prepared = $this->prepare($sql, $params); |
||
| 340 | $this->execute($prepared); |
||
| 341 | |||
| 342 | $result = $prepared['statement']->fetchColumn(); |
||
| 343 | $prepared['statement']->closeCursor(); |
||
| 344 | |||
| 345 | return $result; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param callable $callback |
||
| 350 | * @param mixed|null $that |
||
| 351 | * |
||
| 352 | * @return mixed |
||
| 353 | * |
||
| 354 | * @throws ConnectionException |
||
| 355 | */ |
||
| 356 | public function transaction( |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Change the query parameters placeholder with the value |
||
| 390 | * @param string $query |
||
| 391 | * @param array<int, mixed> $params |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function replaceParameters(string $query, array $params): string |
||
| 416 | ); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Prepare the query |
||
| 421 | * @param string $query |
||
| 422 | * @param array<mixed> $params |
||
| 423 | * @return array<string, mixed> |
||
| 424 | * @throws QueryException |
||
| 425 | */ |
||
| 426 | protected function prepare(string $query, array $params): array |
||
| 427 | { |
||
| 428 | try { |
||
| 429 | $statement = $this->pdo->prepare($query); |
||
| 430 | } catch (PDOException $exception) { |
||
| 431 | $this->logger->error('Error when prepare query [{query}]. Error message: {error}', [ |
||
| 432 | 'exception' => $exception, |
||
| 433 | 'error' => $exception->getMessage(), |
||
| 434 | 'query' => $query |
||
| 435 | ]); |
||
| 436 | throw new QueryException( |
||
| 437 | $exception->getMessage() . ' [' . $query . ']', |
||
| 438 | (int) $exception->getCode(), |
||
| 439 | $exception->getPrevious() |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | return [ |
||
| 444 | 'statement' => $statement, |
||
| 445 | 'query' => $query, |
||
| 446 | 'params' => $params |
||
| 447 | ]; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Execute the prepared query |
||
| 452 | * @param array<string, mixed> $prepared |
||
| 453 | * @return bool the status of the execution |
||
| 454 | * @throws QueryException |
||
| 455 | */ |
||
| 456 | protected function execute(array $prepared): bool |
||
| 457 | { |
||
| 458 | $sql = $this->replaceParameters($prepared['query'], $prepared['params']); |
||
| 459 | $sqlLog = [ |
||
| 460 | 'query' => $prepared['query'], |
||
| 461 | 'parameters' => implode(', ', $prepared['params']) |
||
| 462 | ]; |
||
| 463 | |||
| 464 | try { |
||
| 465 | if ($prepared['params']) { |
||
| 466 | $this->bindValues($prepared['statement'], $prepared['params']); |
||
| 467 | } |
||
| 468 | $start = microtime(true); |
||
| 469 | $result = $prepared['statement']->execute(); |
||
| 470 | $sqlLog['time'] = number_format(microtime(true) - $start, 6); |
||
| 471 | |||
| 472 | $this->logs[] = $sqlLog; |
||
| 473 | |||
| 474 | $this->logger->info( |
||
| 475 | 'Execute Query: [{query}], parameters: [{parameters}], time: [{time}]', |
||
| 476 | $sqlLog |
||
| 477 | ); |
||
| 478 | } catch (PDOException $exception) { |
||
| 479 | $this->logger->error('Error when execute query [{sql}]. Error message: {error}', [ |
||
| 480 | 'exception' => $exception, |
||
| 481 | 'error' => $exception->getMessage(), |
||
| 482 | 'sql' => $sql |
||
| 483 | ]); |
||
| 484 | throw new QueryException( |
||
| 485 | $exception->getMessage() . ' [' . $sql . ']', |
||
| 486 | (int) $exception->getCode(), |
||
| 487 | $exception->getPrevious() |
||
| 488 | ); |
||
| 489 | } |
||
| 490 | |||
| 491 | return $result; |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Bind the parameters values |
||
| 496 | * @param PDOStatement $statement |
||
| 497 | * @param array<int, mixed> $values |
||
| 498 | */ |
||
| 499 | protected function bindValues(PDOStatement $statement, array $values): void |
||
| 500 | { |
||
| 501 | foreach ($values as $key => $value) { |
||
| 502 | $param = PDO::PARAM_STR; |
||
| 503 | if (is_null($value)) { |
||
| 504 | $param = PDO::PARAM_NULL; |
||
| 505 | } elseif (is_int($value) || is_float($value)) { |
||
| 506 | $param = PDO::PARAM_INT; |
||
| 507 | } elseif (is_bool($value)) { |
||
| 508 | $param = PDO::PARAM_BOOL; |
||
| 509 | } |
||
| 510 | |||
| 511 | $statement->bindValue($key + 1, $value, $param); |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Set the PDO connection parameters to use |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | protected function setConnectionParams(): void |
||
| 631 | } |
||
| 632 | } |
||
| 633 |