| Total Complexity | 61 |
| Total Lines | 549 |
| 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 |
||
| 62 | class Connection |
||
| 63 | { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The PDO instance |
||
| 67 | * @var PDO |
||
| 68 | */ |
||
| 69 | protected PDO $pdo; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The PDO data source name |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected string $dsn = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The list of execution query logs |
||
| 79 | * @var array<int, array<string, mixed>> |
||
| 80 | */ |
||
| 81 | protected array $logs = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The driver to use |
||
| 85 | * @var Driver |
||
| 86 | */ |
||
| 87 | protected Driver $driver; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The Schema instance to use |
||
| 91 | * @var Schema |
||
| 92 | */ |
||
| 93 | protected Schema $schema; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The connection configuration |
||
| 97 | * @var ConfigurationInterface |
||
| 98 | */ |
||
| 99 | protected ConfigurationInterface $config; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The connection parameters |
||
| 103 | * @var array<int|string, mixed> |
||
| 104 | */ |
||
| 105 | protected array $params = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Logger |
||
| 109 | */ |
||
| 110 | protected Logger $logger; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Connection constructor. |
||
| 114 | * @param ConfigurationInterface $config |
||
| 115 | * @param Logger $logger |
||
| 116 | * @throws ConnectionException |
||
| 117 | */ |
||
| 118 | public function __construct( |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Connect to the database |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function connect(): void |
||
| 140 | { |
||
| 141 | $this->setConnectionParams(); |
||
| 142 | |||
| 143 | if ($this->config->isPersistent()) { |
||
| 144 | $this->persistent(true); |
||
| 145 | } |
||
| 146 | |||
| 147 | $attr = $this->params; |
||
| 148 | |||
| 149 | if (empty($attr)) { |
||
| 150 | throw new InvalidArgumentException('Invalid database options supplied'); |
||
| 151 | } |
||
| 152 | |||
| 153 | $driver = $attr['driver']; |
||
| 154 | unset($attr['driver']); |
||
| 155 | |||
| 156 | $params = []; |
||
| 157 | foreach ($attr as $key => $value) { |
||
| 158 | $params[] = is_int($key) ? $value : $key . '=' . $value; |
||
| 159 | } |
||
| 160 | |||
| 161 | $dsn = $driver . ':' . implode(';', $params); |
||
| 162 | if (in_array($driver, ['mysql', 'pgsql', 'sqlsrv'])) { |
||
| 163 | $charset = $this->config->getCharset(); |
||
| 164 | $this->config->addCommand('SET NAMES "' . $charset . '"' . ( |
||
| 165 | $this->config->getDriverName() === 'mysql' |
||
| 166 | ? ' COLLATE "' . $this->config->getCollation() . '"' |
||
| 167 | : '' |
||
| 168 | )); |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->dsn = $dsn; |
||
| 172 | |||
| 173 | try { |
||
| 174 | $this->pdo = new PDO( |
||
| 175 | $this->dsn, |
||
| 176 | $this->config->getUsername(), |
||
| 177 | $this->config->getPassword(), |
||
| 178 | $this->config->getOptions() |
||
| 179 | ); |
||
| 180 | |||
| 181 | foreach ($this->config->getCommands() as $command) { |
||
| 182 | $this->pdo->exec($command); |
||
| 183 | } |
||
| 184 | } catch (PDOException $exception) { |
||
| 185 | $this->logger->emergency('Can not connect to database. Error message: {error}', [ |
||
| 186 | 'exception' => $exception, |
||
| 187 | 'error' => $exception->getMessage() |
||
| 188 | ]); |
||
| 189 | throw new ConnectionException($exception->getMessage()); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return the query execution logs |
||
| 195 | * @return array<int, array<string, mixed>> |
||
| 196 | */ |
||
| 197 | public function getLogs(): array |
||
| 198 | { |
||
| 199 | return $this->logs; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return the current connection parameters |
||
| 204 | * @return array<int|string, mixed> |
||
| 205 | */ |
||
| 206 | public function getParams(): array |
||
| 207 | { |
||
| 208 | return $this->params; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return the current connection configuration |
||
| 213 | * @return ConfigurationInterface |
||
| 214 | */ |
||
| 215 | public function getConfig(): ConfigurationInterface |
||
| 216 | { |
||
| 217 | return $this->config; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return the current driver instance |
||
| 222 | * @return Driver |
||
| 223 | */ |
||
| 224 | public function getDriver(): Driver |
||
| 225 | { |
||
| 226 | return $this->driver; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Return the current Schema instance |
||
| 231 | * @return Schema |
||
| 232 | */ |
||
| 233 | public function getSchema(): Schema |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set connection to be persistent |
||
| 240 | * @param bool $value |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | public function persistent(bool $value = true): self |
||
| 244 | { |
||
| 245 | $this->config->setOption(PDO::ATTR_PERSISTENT, $value); |
||
| 246 | |||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getDsn(): string |
||
| 254 | { |
||
| 255 | return $this->dsn; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return the instance of the PDO |
||
| 260 | * @return PDO |
||
| 261 | */ |
||
| 262 | public function getPDO(): PDO |
||
| 263 | { |
||
| 264 | return $this->pdo; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Execute the SQL query and return the result |
||
| 269 | * @param string $sql |
||
| 270 | * @param array<int, mixed> $params the query parameters |
||
| 271 | * @return ResultSet |
||
| 272 | * @throws QueryException |
||
| 273 | */ |
||
| 274 | public function query(string $sql, array $params = []): ResultSet |
||
| 275 | { |
||
| 276 | $prepared = $this->prepare($sql, $params); |
||
| 277 | $this->execute($prepared); |
||
| 278 | |||
| 279 | return new ResultSet($prepared['statement']); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Direct execute the SQL query |
||
| 284 | * @param string $sql |
||
| 285 | * @param array<int, mixed> $params the query parameters |
||
| 286 | * @return mixed |
||
| 287 | * @throws QueryException |
||
| 288 | */ |
||
| 289 | public function exec(string $sql, array $params = []) |
||
| 290 | { |
||
| 291 | return $this->execute($this->prepare($sql, $params)); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Execute the SQL query and return the number |
||
| 296 | * of affected rows |
||
| 297 | * @param string $sql |
||
| 298 | * @param array<int, mixed> $params the query parameters |
||
| 299 | * @return int |
||
| 300 | * @throws QueryException |
||
| 301 | */ |
||
| 302 | public function count(string $sql, array $params = []): int |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Execute the SQL query and return the first column result |
||
| 315 | * @param string $sql |
||
| 316 | * @param array<int, mixed> $params the query parameters |
||
| 317 | * @return mixed |
||
| 318 | * @throws QueryException |
||
| 319 | */ |
||
| 320 | public function column(string $sql, array $params = []) |
||
| 321 | { |
||
| 322 | $prepared = $this->prepare($sql, $params); |
||
| 323 | $this->execute($prepared); |
||
| 324 | |||
| 325 | $result = $prepared['statement']->fetchColumn(); |
||
| 326 | $prepared['statement']->closeCursor(); |
||
| 327 | |||
| 328 | return $result; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param callable $callback |
||
| 333 | * @param mixed|null $that |
||
| 334 | * |
||
| 335 | * @return mixed |
||
| 336 | * |
||
| 337 | * @throws ConnectionException |
||
| 338 | */ |
||
| 339 | public function transaction( |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Change the query parameters placeholder with the value |
||
| 369 | * @param string $query |
||
| 370 | * @param array<int, mixed> $params |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | protected function replaceParameters(string $query, array $params): string |
||
| 395 | ); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Prepare the query |
||
| 400 | * @param string $query |
||
| 401 | * @param array<mixed> $params |
||
| 402 | * @return array<string, mixed> |
||
| 403 | * @throws QueryException |
||
| 404 | */ |
||
| 405 | protected function prepare(string $query, array $params): array |
||
| 406 | { |
||
| 407 | try { |
||
| 408 | $statement = $this->pdo->prepare($query); |
||
| 409 | } catch (PDOException $exception) { |
||
| 410 | $sql = $this->replaceParameters($query, $params); |
||
| 411 | $this->logger->error('Error when prepare query [{sql}]. Error message: {error}', [ |
||
| 412 | 'exception' => $exception, |
||
| 413 | 'error' => $exception->getMessage(), |
||
| 414 | 'sql' => $sql |
||
| 415 | ]); |
||
| 416 | throw new QueryException( |
||
| 417 | $exception->getMessage() . ' [' . $sql . ']', |
||
| 418 | (int) $exception->getCode(), |
||
| 419 | $exception->getPrevious() |
||
| 420 | ); |
||
| 421 | } |
||
| 422 | |||
| 423 | return [ |
||
| 424 | 'statement' => $statement, |
||
| 425 | 'query' => $query, |
||
| 426 | 'params' => $params |
||
| 427 | ]; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Execute the prepared query |
||
| 432 | * @param array<string, mixed> $prepared |
||
| 433 | * @return bool the status of the execution |
||
| 434 | * @throws QueryException |
||
| 435 | */ |
||
| 436 | protected function execute(array $prepared): bool |
||
| 437 | { |
||
| 438 | $sql = $this->replaceParameters($prepared['query'], $prepared['params']); |
||
| 439 | $sqlLog = [ |
||
| 440 | 'query' => $prepared['query'], |
||
| 441 | 'parameters' => implode(', ', $prepared['params']) |
||
| 442 | ]; |
||
| 443 | |||
| 444 | try { |
||
| 445 | if ($prepared['params']) { |
||
| 446 | $this->bindValues($prepared['statement'], $prepared['params']); |
||
| 447 | } |
||
| 448 | $start = microtime(true); |
||
| 449 | $result = $prepared['statement']->execute(); |
||
| 450 | $sqlLog['time'] = number_format(microtime(true) - $start, 6); |
||
| 451 | |||
| 452 | $this->logs[] = $sqlLog; |
||
| 453 | |||
| 454 | $this->logger->info( |
||
| 455 | 'Execute Query: [{query}], parameters: [{parameters}], time: [{time}]', |
||
| 456 | $sqlLog |
||
| 457 | ); |
||
| 458 | } catch (PDOException $exception) { |
||
| 459 | $this->logger->error('Error when execute query [{sql}]. Error message: {error}', [ |
||
| 460 | 'exception' => $exception, |
||
| 461 | 'error' => $exception->getMessage(), |
||
| 462 | 'sql' => $sql |
||
| 463 | ]); |
||
| 464 | throw new QueryException( |
||
| 465 | $exception->getMessage() . ' [' . $sql . ']', |
||
| 466 | (int) $exception->getCode(), |
||
| 467 | $exception->getPrevious() |
||
| 468 | ); |
||
| 469 | } |
||
| 470 | |||
| 471 | return $result; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Bind the parameters values |
||
| 476 | * @param PDOStatement $statement |
||
| 477 | * @param array<int, mixed> $values |
||
| 478 | */ |
||
| 479 | protected function bindValues(PDOStatement $statement, array $values): void |
||
| 480 | { |
||
| 481 | foreach ($values as $key => $value) { |
||
| 482 | $param = PDO::PARAM_STR; |
||
| 483 | if (is_null($value)) { |
||
| 484 | $param = PDO::PARAM_NULL; |
||
| 485 | } elseif (is_int($value) || is_float($value)) { |
||
| 486 | $param = PDO::PARAM_INT; |
||
| 487 | } elseif (is_bool($value)) { |
||
| 488 | $param = PDO::PARAM_BOOL; |
||
| 489 | } |
||
| 490 | |||
| 491 | $statement->bindValue($key + 1, $value, $param); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set the PDO connection parameters to use |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | protected function setConnectionParams(): void |
||
| 611 | } |
||
| 612 | } |
||
| 613 |