Total Complexity | 95 |
Total Lines | 679 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractCommand 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 AbstractCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | abstract class AbstractCommand implements CommandInterface, ProfilerAwareInterface |
||
75 | { |
||
76 | use LoggerAwareTrait; |
||
77 | use ProfilerAwareTrait; |
||
78 | |||
79 | protected string|null $isolationLevel = null; |
||
80 | protected string|null $refreshTableName = null; |
||
81 | protected Closure|null $retryHandler = null; |
||
82 | protected int|null $queryCacheDuration = null; |
||
83 | private string $sql = ''; |
||
84 | protected Dependency|null $queryCacheDependency = null; |
||
85 | /** * @var ParamInterface[] */ |
||
86 | protected array $params = []; |
||
87 | |||
88 | public function __construct(protected QueryCache $queryCache) |
||
89 | { |
||
90 | } |
||
91 | |||
92 | public function addCheck(string $name, string $table, string $expression): static |
||
93 | { |
||
94 | $sql = $this->queryBuilder()->addCheck($name, $table, $expression); |
||
95 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
96 | } |
||
97 | |||
98 | public function addColumn(string $table, string $column, string $type): static |
||
99 | { |
||
100 | $sql = $this->queryBuilder()->addColumn($table, $column, $type); |
||
101 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
102 | } |
||
103 | |||
104 | public function addCommentOnColumn(string $table, string $column, string $comment): static |
||
105 | { |
||
106 | $sql = $this->queryBuilder()->addCommentOnColumn($table, $column, $comment); |
||
107 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
108 | } |
||
109 | |||
110 | public function addCommentOnTable(string $table, string $comment): static |
||
111 | { |
||
112 | $sql = $this->queryBuilder()->addCommentOnTable($table, $comment); |
||
113 | return $this->setSql($sql); |
||
114 | } |
||
115 | |||
116 | public function addDefaultValue(string $name, string $table, string $column, mixed $value): static |
||
120 | } |
||
121 | |||
122 | public function addForeignKey( |
||
123 | string $name, |
||
124 | string $table, |
||
125 | array|string $columns, |
||
126 | string $refTable, |
||
127 | array|string $refColumns, |
||
128 | string $delete = null, |
||
129 | string $update = null |
||
130 | ): static { |
||
131 | $sql = $this->queryBuilder()->addForeignKey( |
||
132 | $name, |
||
133 | $table, |
||
134 | $columns, |
||
135 | $refTable, |
||
136 | $refColumns, |
||
137 | $delete, |
||
138 | $update |
||
139 | ); |
||
140 | |||
141 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
142 | } |
||
143 | |||
144 | public function addPrimaryKey(string $name, string $table, array|string $columns): static |
||
145 | { |
||
146 | $sql = $this->queryBuilder()->addPrimaryKey($name, $table, $columns); |
||
147 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
148 | } |
||
149 | |||
150 | public function addUnique(string $name, string $table, array|string $columns): static |
||
154 | } |
||
155 | |||
156 | public function alterColumn(string $table, string $column, string $type): static |
||
157 | { |
||
158 | $sql = $this->queryBuilder()->alterColumn($table, $column, $type); |
||
159 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
160 | } |
||
161 | |||
162 | public function batchInsert(string $table, array $columns, iterable $rows): static |
||
163 | { |
||
164 | $table = $this->queryBuilder()->quoter()->quoteSql($table); |
||
165 | |||
166 | /** @psalm-var string[] $columns */ |
||
167 | foreach ($columns as &$column) { |
||
168 | $column = $this->queryBuilder()->quoter()->quoteSql($column); |
||
169 | } |
||
170 | |||
171 | unset($column); |
||
172 | |||
173 | $params = []; |
||
174 | $sql = $this->queryBuilder()->batchInsert($table, $columns, $rows, $params); |
||
175 | |||
176 | $this->setRawSql($sql); |
||
177 | $this->bindValues($params); |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | abstract public function bindValue(int|string $name, mixed $value, int $dataType = null): static; |
||
182 | |||
183 | abstract public function bindValues(array $values): static; |
||
184 | |||
185 | public function cache(int $duration = null, Dependency $dependency = null): static |
||
186 | { |
||
187 | $this->queryCacheDuration = $duration ?? $this->queryCache->getDuration(); |
||
188 | $this->queryCacheDependency = $dependency; |
||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | public function checkIntegrity(string $schema, string $table, bool $check = true): static |
||
193 | { |
||
194 | $sql = $this->queryBuilder()->checkIntegrity($schema, $table, $check); |
||
195 | return $this->setSql($sql); |
||
196 | } |
||
197 | |||
198 | public function createIndex( |
||
199 | string $name, |
||
200 | string $table, |
||
201 | array|string $columns, |
||
202 | string $indexType = null, |
||
203 | string $indexMethod = null |
||
204 | ): static { |
||
205 | $sql = $this->queryBuilder()->createIndex($name, $table, $columns, $indexType, $indexMethod); |
||
206 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
207 | } |
||
208 | |||
209 | public function createTable(string $table, array $columns, string $options = null): static |
||
213 | } |
||
214 | |||
215 | public function createView(string $viewName, QueryInterface|string $subquery): static |
||
216 | { |
||
217 | $sql = $this->queryBuilder()->createView($viewName, $subquery); |
||
218 | return $this->setSql($sql)->requireTableSchemaRefresh($viewName); |
||
219 | } |
||
220 | |||
221 | public function delete(string $table, array|string $condition = '', array $params = []): static |
||
222 | { |
||
223 | $sql = $this->queryBuilder()->delete($table, $condition, $params); |
||
224 | return $this->setSql($sql)->bindValues($params); |
||
225 | } |
||
226 | |||
227 | public function dropCheck(string $name, string $table): static |
||
228 | { |
||
229 | $sql = $this->queryBuilder()->dropCheck($name, $table); |
||
230 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
231 | } |
||
232 | |||
233 | public function dropColumn(string $table, string $column): static |
||
234 | { |
||
235 | $sql = $this->queryBuilder()->dropColumn($table, $column); |
||
236 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
237 | } |
||
238 | |||
239 | public function dropCommentFromColumn(string $table, string $column): static |
||
240 | { |
||
241 | $sql = $this->queryBuilder()->dropCommentFromColumn($table, $column); |
||
242 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
243 | } |
||
244 | |||
245 | public function dropCommentFromTable(string $table): static |
||
246 | { |
||
247 | $sql = $this->queryBuilder()->dropCommentFromTable($table); |
||
248 | return $this->setSql($sql); |
||
249 | } |
||
250 | |||
251 | public function dropDefaultValue(string $name, string $table): static |
||
252 | { |
||
253 | $sql = $this->queryBuilder()->dropDefaultValue($name, $table); |
||
254 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
255 | } |
||
256 | |||
257 | public function dropForeignKey(string $name, string $table): static |
||
258 | { |
||
259 | $sql = $this->queryBuilder()->dropForeignKey($name, $table); |
||
260 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
261 | } |
||
262 | |||
263 | public function dropIndex(string $name, string $table): static |
||
264 | { |
||
265 | $sql = $this->queryBuilder()->dropIndex($name, $table); |
||
266 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
267 | } |
||
268 | |||
269 | public function dropPrimaryKey(string $name, string $table): static |
||
270 | { |
||
271 | $sql = $this->queryBuilder()->dropPrimaryKey($name, $table); |
||
272 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
273 | } |
||
274 | |||
275 | public function dropTable(string $table): static |
||
276 | { |
||
277 | $sql = $this->queryBuilder()->dropTable($table); |
||
278 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
279 | } |
||
280 | |||
281 | public function dropUnique(string $name, string $table): static |
||
282 | { |
||
283 | $sql = $this->queryBuilder()->dropUnique($name, $table); |
||
284 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
285 | } |
||
286 | |||
287 | public function dropView(string $viewName): static |
||
288 | { |
||
289 | $sql = $this->queryBuilder()->dropView($viewName); |
||
290 | return $this->setSql($sql)->requireTableSchemaRefresh($viewName); |
||
291 | } |
||
292 | |||
293 | public function getParams(bool $asValues = true): array |
||
294 | { |
||
295 | if (!$asValues) { |
||
296 | return $this->params; |
||
297 | } |
||
298 | |||
299 | $buildParams = []; |
||
300 | |||
301 | foreach ($this->params as $name => $value) { |
||
302 | /** @psalm-var mixed */ |
||
303 | $buildParams[$name] = $value->getValue(); |
||
304 | } |
||
305 | |||
306 | return $buildParams; |
||
307 | } |
||
308 | |||
309 | public function getRawSql(): string |
||
354 | } |
||
355 | |||
356 | public function getSql(): string |
||
357 | { |
||
358 | return $this->sql; |
||
359 | } |
||
360 | |||
361 | public function insert(string $table, QueryInterface|array $columns): static |
||
362 | { |
||
363 | $params = []; |
||
364 | $sql = $this->queryBuilder()->insert($table, $columns, $params); |
||
365 | return $this->setSql($sql)->bindValues($params); |
||
366 | } |
||
367 | |||
368 | public function insertWithReturningPks(string $table, array $columns): bool|array |
||
369 | { |
||
370 | $params = []; |
||
371 | |||
372 | $sql = $this->queryBuilder()->insertWithReturningPks($table, $columns, $params); |
||
373 | |||
374 | $this->setSql($sql)->bindValues($params); |
||
375 | |||
376 | /** @psalm-var array|bool $result */ |
||
377 | $result = $this->queryInternal(self::QUERY_MODE_ROW | self::QUERY_MODE_EXECUTE); |
||
378 | |||
379 | return is_array($result) ? $result : false; |
||
380 | } |
||
381 | |||
382 | public function noCache(): static |
||
383 | { |
||
384 | $this->queryCacheDuration = -1; |
||
385 | return $this; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @psalm-suppress MixedReturnStatement |
||
390 | * @psalm-suppress MixedInferredReturnType |
||
391 | */ |
||
392 | public function execute(): int |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @psalm-suppress MixedReturnStatement |
||
405 | * @psalm-suppress MixedInferredReturnType |
||
406 | */ |
||
407 | public function query(): DataReaderInterface |
||
408 | { |
||
409 | return $this->queryInternal(self::QUERY_MODE_CURSOR); |
||
410 | } |
||
411 | |||
412 | public function queryAll(): array |
||
413 | { |
||
414 | /** @psalm-var array<array-key, array>|null $results */ |
||
415 | $results = $this->queryInternal(self::QUERY_MODE_ALL); |
||
416 | |||
417 | return $results ?? []; |
||
418 | } |
||
419 | |||
420 | public function queryColumn(): array |
||
421 | { |
||
422 | /** @psalm-var mixed $results */ |
||
423 | $results = $this->queryInternal(self::QUERY_MODE_COLUMN); |
||
424 | |||
425 | return is_array($results) ? $results : []; |
||
426 | } |
||
427 | |||
428 | public function queryOne(): array|null |
||
429 | { |
||
430 | /** @psalm-var mixed $results */ |
||
431 | $results = $this->queryInternal(self::QUERY_MODE_ROW); |
||
432 | |||
433 | return is_array($results) ? $results : null; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @psalm-suppress MixedReturnStatement |
||
438 | * @psalm-suppress MixedInferredReturnType |
||
439 | */ |
||
440 | public function queryScalar(): bool|string|null|int|float |
||
441 | { |
||
442 | $firstRow = $this->queryInternal(self::QUERY_MODE_ROW); |
||
443 | |||
444 | if (!is_array($firstRow)) { |
||
445 | return false; |
||
446 | } |
||
447 | |||
448 | /** @psalm-var mixed $result */ |
||
449 | $result = current($firstRow); |
||
450 | |||
451 | if (is_resource($result) && get_resource_type($result) === 'stream') { |
||
452 | return stream_get_contents($result); |
||
453 | } |
||
454 | |||
455 | return $result; |
||
456 | } |
||
457 | |||
458 | public function renameColumn(string $table, string $oldName, string $newName): static |
||
459 | { |
||
460 | $sql = $this->queryBuilder()->renameColumn($table, $oldName, $newName); |
||
461 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
462 | } |
||
463 | |||
464 | public function renameTable(string $table, string $newName): static |
||
465 | { |
||
466 | $sql = $this->queryBuilder()->renameTable($table, $newName); |
||
467 | return $this->setSql($sql)->requireTableSchemaRefresh($table); |
||
468 | } |
||
469 | |||
470 | public function resetSequence(string $table, int|string $value = null): static |
||
474 | } |
||
475 | |||
476 | public function setRawSql(string $sql): static |
||
485 | } |
||
486 | |||
487 | public function setSql(string $sql): static |
||
488 | { |
||
489 | $this->cancel(); |
||
490 | $this->reset(); |
||
491 | $this->sql = $this->queryBuilder()->quoter()->quoteSql($sql); |
||
492 | |||
493 | return $this; |
||
494 | } |
||
495 | |||
496 | public function setRetryHandler(Closure|null $handler): static |
||
497 | { |
||
498 | $this->retryHandler = $handler; |
||
499 | return $this; |
||
500 | } |
||
501 | |||
502 | public function truncateTable(string $table): static |
||
506 | } |
||
507 | |||
508 | public function update(string $table, array $columns, array|string $condition = '', array $params = []): static |
||
512 | } |
||
513 | |||
514 | public function upsert( |
||
515 | string $table, |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * Returns the cache key for the query. |
||
526 | * |
||
527 | * @param string $rawSql the raw SQL with parameter values inserted into the corresponding placeholders. |
||
528 | * |
||
529 | * @throws JsonException |
||
530 | * |
||
531 | * @return array The cache key. |
||
532 | */ |
||
533 | abstract protected function getCacheKey(int $queryMode, string $rawSql): array; |
||
534 | |||
535 | /** |
||
536 | * Executes a prepared statement. |
||
537 | * |
||
538 | * @param string|null $rawSql the rawSql if it has been created. |
||
539 | * |
||
540 | * @throws Exception |
||
541 | * @throws Throwable |
||
542 | */ |
||
543 | abstract protected function internalExecute(string|null $rawSql): void; |
||
544 | |||
545 | protected function is(int $value, int $flag): bool |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Returns the query result. |
||
552 | * |
||
553 | * @param int $queryMode One from modes QUERY_MODE_*. |
||
554 | * |
||
555 | * @throws Exception |
||
556 | * @throws Throwable |
||
557 | */ |
||
558 | abstract protected function internalGetQueryResult(int $queryMode): mixed; |
||
559 | |||
560 | /** |
||
561 | * Refreshes table schema, which was marked by {@see requireTableSchemaRefresh()}. |
||
562 | */ |
||
563 | abstract protected function refreshTableSchema(): void; |
||
564 | |||
565 | /** |
||
566 | * The method is called after the query is executed. |
||
567 | * |
||
568 | * @param int $queryMode One from modes QUERY_MODE_*. |
||
569 | * |
||
570 | * @throws Exception |
||
571 | * @throws Throwable |
||
572 | */ |
||
573 | protected function queryInternal(int $queryMode): mixed |
||
574 | { |
||
575 | if ($this->is($queryMode, self::QUERY_MODE_EXECUTE) || $this->is($queryMode, self::QUERY_MODE_CURSOR)) { |
||
576 | return $this->queryWithoutCache($this->getRawSql(), $queryMode); |
||
577 | } |
||
578 | |||
579 | return $this->queryWithCache($queryMode); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Performs the actual DB query of a SQL statement. |
||
584 | * |
||
585 | * @param int $queryMode One from modes QUERY_MODE_*. |
||
586 | * |
||
587 | * @throws Exception |
||
588 | * @throws Throwable If the query causes any problem. |
||
589 | * |
||
590 | * @return mixed The method execution result. |
||
591 | */ |
||
592 | protected function queryWithCache(int $queryMode): mixed |
||
593 | { |
||
594 | $rawSql = $this->getRawSql(); |
||
595 | |||
596 | $cacheKey = $this->getCacheKey($queryMode, $rawSql); |
||
597 | /** @psalm-var array{CacheInterface, DateInterval|int|null, Dependency|null} $info */ |
||
598 | $info = $this->queryCache->info($this->queryCacheDuration, $this->queryCacheDependency); |
||
599 | /** @psalm-var mixed $cacheResult */ |
||
600 | $cacheResult = $this->getFromCacheInfo($info, $cacheKey); |
||
601 | |||
602 | if ($cacheResult) { |
||
603 | $this->logger?->log(LogLevel::DEBUG, 'Get query result from cache', [self::class . '::query']); |
||
604 | return $cacheResult; |
||
605 | } |
||
606 | |||
607 | /** @psalm-var mixed $result */ |
||
608 | $result = $this->queryWithoutCache($rawSql, $queryMode); |
||
609 | $this->setToCacheInfo($info, $cacheKey, $result); |
||
610 | |||
611 | return $result; |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * Performs the actual DB query of a SQL statement without caching. |
||
616 | * |
||
617 | * @param int $queryMode One from modes QUERY_MODE_*. |
||
618 | * |
||
619 | * @throws Exception |
||
620 | * @throws Throwable If the query causes any problem. |
||
621 | * |
||
622 | * @return mixed The method execution result. |
||
623 | */ |
||
624 | protected function queryWithoutCache(string $rawSql, int $queryMode): mixed |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * Logs the current database query if query logging is enabled and returns the profiling token if profiling is |
||
656 | * enabled. |
||
657 | */ |
||
658 | protected function logQuery(string $rawSql, string $category): void |
||
659 | { |
||
660 | $this->logger?->log(LogLevel::INFO, $rawSql, [$category]); |
||
661 | } |
||
662 | |||
663 | /** |
||
664 | * Marks a specified table schema to be refreshed after command execution. |
||
665 | * |
||
666 | * @param string $name Name of the table, which schema should be refreshed. |
||
667 | * |
||
668 | * @return static The command object itself. |
||
669 | */ |
||
670 | protected function requireTableSchemaRefresh(string $name): static |
||
671 | { |
||
672 | $this->refreshTableName = $name; |
||
673 | return $this; |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * Marks the command to be executed in transaction. |
||
678 | * |
||
679 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
680 | * |
||
681 | * {@see TransactionInterface::begin()} for details. |
||
682 | * |
||
683 | * @return static The command object itself. |
||
684 | */ |
||
685 | protected function requireTransaction(string $isolationLevel = null): static |
||
686 | { |
||
687 | $this->isolationLevel = $isolationLevel; |
||
688 | return $this; |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * Resets the command object, so it can be reused to build another SQL statement. |
||
693 | */ |
||
694 | protected function reset(): void |
||
695 | { |
||
696 | $this->sql = ''; |
||
697 | $this->params = []; |
||
698 | $this->refreshTableName = null; |
||
699 | $this->isolationLevel = null; |
||
700 | $this->retryHandler = null; |
||
701 | } |
||
702 | |||
703 | /** |
||
704 | * @psalm-param array{CacheInterface, DateInterval|int|null, Dependency|null} $info |
||
705 | */ |
||
706 | private function getFromCacheInfo(array|null $info, array $cacheKey): mixed |
||
707 | { |
||
708 | if (!is_array($info)) { |
||
|
|||
709 | return null; |
||
710 | } |
||
711 | |||
712 | $cache = $info[0]; |
||
713 | |||
714 | /** @psalm-var mixed $result */ |
||
715 | $result = $cache->getOrSet( |
||
716 | $cacheKey, |
||
717 | static fn () => null, |
||
718 | ); |
||
719 | |||
720 | if (is_array($result) && isset($result[0])) { |
||
721 | $this->logger?->log(LogLevel::DEBUG, 'Query result served from cache', [self::class . '::query']); |
||
722 | |||
723 | return $result[0]; |
||
724 | } |
||
725 | |||
726 | return null; |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * @psalm-param array{CacheInterface, DateInterval|int|null, Dependency|null} $info |
||
731 | */ |
||
732 | private function setToCacheInfo(array|null $info, array $cacheKey, mixed $result): void |
||
748 | } |
||
749 | |||
750 | private function isReadMode(int $queryMode): bool |
||
751 | { |
||
752 | return !$this->is($queryMode, self::QUERY_MODE_EXECUTE); |
||
753 | } |
||
754 | } |
||
755 |