| Total Complexity | 102 |
| Total Lines | 607 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Typo3DbBackend 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 Typo3DbBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Typo3DbBackend implements BackendInterface, SingletonInterface |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * @var ConnectionPool |
||
| 58 | */ |
||
| 59 | protected $connectionPool; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ConfigurationManagerInterface |
||
| 63 | */ |
||
| 64 | protected $configurationManager; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var CacheService |
||
| 68 | */ |
||
| 69 | protected $cacheService; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var EnvironmentService |
||
| 73 | */ |
||
| 74 | protected $environmentService; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ObjectManagerInterface |
||
| 78 | */ |
||
| 79 | protected $objectManager; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * As determining the table columns is a costly operation this is done only once per table during runtime and cached then |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | * @see clearPageCache() |
||
| 86 | */ |
||
| 87 | protected $hasPidColumn = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param ConfigurationManagerInterface $configurationManager |
||
| 91 | */ |
||
| 92 | public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void |
||
| 93 | { |
||
| 94 | $this->configurationManager = $configurationManager; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param CacheService $cacheService |
||
| 99 | */ |
||
| 100 | public function injectCacheService(CacheService $cacheService): void |
||
| 101 | { |
||
| 102 | $this->cacheService = $cacheService; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param EnvironmentService $environmentService |
||
| 107 | */ |
||
| 108 | public function injectEnvironmentService(EnvironmentService $environmentService): void |
||
| 109 | { |
||
| 110 | $this->environmentService = $environmentService; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param ObjectManagerInterface $objectManager |
||
| 115 | */ |
||
| 116 | public function injectObjectManager(ObjectManagerInterface $objectManager): void |
||
| 117 | { |
||
| 118 | $this->objectManager = $objectManager; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Constructor. |
||
| 123 | */ |
||
| 124 | public function __construct() |
||
| 125 | { |
||
| 126 | $this->connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Adds a row to the storage |
||
| 131 | * |
||
| 132 | * @param string $tableName The database table name |
||
| 133 | * @param array $fieldValues The row to be inserted |
||
| 134 | * @param bool $isRelation TRUE if we are currently inserting into a relation table, FALSE by default |
||
| 135 | * @return int The uid of the inserted row |
||
| 136 | * @throws SqlErrorException |
||
| 137 | */ |
||
| 138 | public function addRow(string $tableName, array $fieldValues, bool $isRelation = false): int |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Updates a row in the storage |
||
| 172 | * |
||
| 173 | * @param string $tableName The database table name |
||
| 174 | * @param array $fieldValues The row to be updated |
||
| 175 | * @param bool $isRelation TRUE if we are currently inserting into a relation table, FALSE by default |
||
| 176 | * @throws \InvalidArgumentException |
||
| 177 | * @throws SqlErrorException |
||
| 178 | */ |
||
| 179 | public function updateRow(string $tableName, array $fieldValues, bool $isRelation = false): void |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Updates a relation row in the storage. |
||
| 214 | * |
||
| 215 | * @param string $tableName The database relation table name |
||
| 216 | * @param array $fieldValues The row to be updated |
||
| 217 | * @throws SqlErrorException |
||
| 218 | * @throws \InvalidArgumentException |
||
| 219 | */ |
||
| 220 | public function updateRelationTableRow(string $tableName, array $fieldValues): void |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Deletes a row in the storage |
||
| 253 | * |
||
| 254 | * @param string $tableName The database table name |
||
| 255 | * @param array $where An array of where array('fieldname' => value). |
||
| 256 | * @param bool $isRelation TRUE if we are currently manipulating a relation table, FALSE by default |
||
| 257 | * @throws SqlErrorException |
||
| 258 | */ |
||
| 259 | public function removeRow(string $tableName, array $where, bool $isRelation = false): void |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns the object data matching the $query. |
||
| 274 | * |
||
| 275 | * @param QueryInterface $query |
||
| 276 | * @return array |
||
| 277 | * @throws SqlErrorException |
||
| 278 | */ |
||
| 279 | public function getObjectDataByQuery(QueryInterface $query): array |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the object data using a custom statement |
||
| 324 | * |
||
| 325 | * @param Qom\Statement $statement |
||
| 326 | * @return array |
||
| 327 | * @throws SqlErrorException when the raw SQL statement fails in the database |
||
| 328 | */ |
||
| 329 | protected function getObjectDataByRawQuery(Statement $statement): array |
||
| 330 | { |
||
| 331 | $realStatement = $statement->getStatement(); |
||
| 332 | $parameters = $statement->getBoundVariables(); |
||
| 333 | |||
| 334 | // The real statement is an instance of the Doctrine DBAL QueryBuilder, so fetching |
||
| 335 | // this directly is possible |
||
| 336 | if ($realStatement instanceof QueryBuilder) { |
||
| 337 | try { |
||
| 338 | $result = $realStatement->execute(); |
||
| 339 | } catch (DBALException $e) { |
||
| 340 | throw new SqlErrorException($e->getPrevious()->getMessage(), 1472064721, $e); |
||
| 341 | } |
||
| 342 | $rows = $result->fetchAll(); |
||
| 343 | } elseif ($realStatement instanceof \Doctrine\DBAL\Statement) { |
||
| 344 | try { |
||
| 345 | $realStatement->execute($parameters); |
||
| 346 | } catch (DBALException $e) { |
||
| 347 | throw new SqlErrorException($e->getPrevious()->getMessage(), 1481281404, $e); |
||
| 348 | } |
||
| 349 | $rows = $realStatement->fetchAll(); |
||
| 350 | } else { |
||
| 351 | // Do a real raw query. This is very stupid, as it does not allow to use DBAL's real power if |
||
| 352 | // several tables are on different databases, so this is used with caution and could be removed |
||
| 353 | // in the future |
||
| 354 | try { |
||
| 355 | $connection = $this->connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); |
||
| 356 | $statement = $connection->executeQuery($realStatement, $parameters); |
||
| 357 | } catch (DBALException $e) { |
||
| 358 | throw new SqlErrorException($e->getPrevious()->getMessage(), 1472064775, $e); |
||
| 359 | } |
||
| 360 | |||
| 361 | $rows = $statement->fetchAll(); |
||
| 362 | } |
||
| 363 | |||
| 364 | return $rows; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the number of tuples matching the query. |
||
| 369 | * |
||
| 370 | * @param QueryInterface $query |
||
| 371 | * @return int The number of matching tuples |
||
| 372 | * @throws BadConstraintException |
||
| 373 | * @throws SqlErrorException |
||
| 374 | */ |
||
| 375 | public function getObjectCountByQuery(QueryInterface $query): int |
||
| 376 | { |
||
| 377 | if ($query->getConstraint() instanceof Statement) { |
||
| 378 | throw new BadConstraintException('Could not execute count on queries with a constraint of type TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Statement', 1256661045); |
||
| 379 | } |
||
| 380 | |||
| 381 | $statement = $query->getStatement(); |
||
| 382 | if ($statement instanceof Statement |
||
| 383 | && !$statement->getStatement() instanceof QueryBuilder |
||
| 384 | ) { |
||
| 385 | $rows = $this->getObjectDataByQuery($query); |
||
| 386 | $count = count($rows); |
||
| 387 | } else { |
||
| 388 | /** @var Typo3DbQueryParser $queryParser */ |
||
| 389 | $queryParser = $this->objectManager->get(Typo3DbQueryParser::class); |
||
| 390 | $queryBuilder = $queryParser |
||
| 391 | ->convertQueryToDoctrineQueryBuilder($query) |
||
| 392 | ->resetQueryPart('orderBy'); |
||
| 393 | |||
| 394 | if ($queryParser->isDistinctQuerySuggested()) { |
||
| 395 | $source = $queryBuilder->getQueryPart('from')[0]; |
||
| 396 | // Tablename is already quoted for the DBMS, we need to treat table and field names separately |
||
| 397 | $tableName = $source['alias'] ?: $source['table']; |
||
| 398 | $fieldName = $queryBuilder->quoteIdentifier('uid'); |
||
| 399 | $queryBuilder->resetQueryPart('groupBy') |
||
| 400 | ->selectLiteral(sprintf('COUNT(DISTINCT %s.%s)', $tableName, $fieldName)); |
||
| 401 | } else { |
||
| 402 | $queryBuilder->count('*'); |
||
| 403 | } |
||
| 404 | |||
| 405 | try { |
||
| 406 | $count = $queryBuilder->execute()->fetchColumn(0); |
||
| 407 | } catch (DBALException $e) { |
||
| 408 | throw new SqlErrorException($e->getPrevious()->getMessage(), 1472074379, $e); |
||
| 409 | } |
||
| 410 | if ($query->getOffset()) { |
||
| 411 | $count -= $query->getOffset(); |
||
| 412 | } |
||
| 413 | if ($query->getLimit()) { |
||
| 414 | $count = min($count, $query->getLimit()); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | return (int)max(0, $count); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Checks if a Value Object equal to the given Object exists in the database |
||
| 422 | * |
||
| 423 | * @param AbstractValueObject $object The Value Object |
||
| 424 | * @return int|null The matching uid if an object was found, else FALSE |
||
| 425 | * @throws SqlErrorException |
||
| 426 | */ |
||
| 427 | public function getUidOfAlreadyPersistedValueObject(AbstractValueObject $object): ?int |
||
| 428 | { |
||
| 429 | /** @var DataMapper $dataMapper */ |
||
| 430 | $dataMapper = $this->objectManager->get(DataMapper::class); |
||
| 431 | $dataMap = $dataMapper->getDataMap(get_class($object)); |
||
| 432 | $tableName = $dataMap->getTableName(); |
||
| 433 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); |
||
| 434 | if ($this->environmentService->isEnvironmentInFrontendMode()) { |
||
| 435 | $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class)); |
||
| 436 | } |
||
| 437 | $whereClause = []; |
||
| 438 | // loop over all properties of the object to exactly set the values of each database field |
||
| 439 | $properties = $object->_getProperties(); |
||
| 440 | foreach ($properties as $propertyName => $propertyValue) { |
||
| 441 | $propertyName = (string)$propertyName; |
||
| 442 | |||
| 443 | // @todo We couple the Backend to the Entity implementation (uid, isClone); changes there breaks this method |
||
| 444 | if ($dataMap->isPersistableProperty($propertyName) && $propertyName !== 'uid' && $propertyName !== 'pid' && $propertyName !== 'isClone') { |
||
| 445 | $fieldName = $dataMap->getColumnMap($propertyName)->getColumnName(); |
||
| 446 | if ($propertyValue === null) { |
||
| 447 | $whereClause[] = $queryBuilder->expr()->isNull($fieldName); |
||
| 448 | } else { |
||
| 449 | $whereClause[] = $queryBuilder->expr()->eq($fieldName, $queryBuilder->createNamedParameter($dataMapper->getPlainValue($propertyValue))); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | } |
||
| 453 | $queryBuilder |
||
| 454 | ->select('uid') |
||
| 455 | ->from($tableName) |
||
| 456 | ->where(...$whereClause); |
||
| 457 | |||
| 458 | try { |
||
| 459 | $uid = (int)$queryBuilder |
||
| 460 | ->execute() |
||
| 461 | ->fetchColumn(0); |
||
| 462 | if ($uid > 0) { |
||
| 463 | return $uid; |
||
| 464 | } |
||
| 465 | return null; |
||
| 466 | } catch (DBALException $e) { |
||
| 467 | throw new SqlErrorException($e->getPrevious()->getMessage(), 1470231748, $e); |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Performs workspace and language overlay on the given row array. The language and workspace id is automatically |
||
| 473 | * detected (depending on FE or BE context). You can also explicitly set the language/workspace id. |
||
| 474 | * |
||
| 475 | * @param Qom\SourceInterface $source The source (selector or join) |
||
| 476 | * @param array $rows |
||
| 477 | * @param QueryInterface $query |
||
| 478 | * @param int|null $workspaceUid |
||
| 479 | * @return array |
||
| 480 | * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException |
||
| 481 | */ |
||
| 482 | protected function overlayLanguageAndWorkspace(SourceInterface $source, array $rows, QueryInterface $query, int $workspaceUid = null): array |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Clear the TYPO3 page cache for the given record. |
||
| 586 | * If the record lies on a page, then we clear the cache of this page. |
||
| 587 | * If the record has no PID column, we clear the cache of the current page as best-effort. |
||
| 588 | * |
||
| 589 | * Much of this functionality is taken from DataHandler::clear_cache() which unfortunately only works with logged-in BE user. |
||
| 590 | * |
||
| 591 | * @param string $tableName Table name of the record |
||
| 592 | * @param int $uid UID of the record |
||
| 593 | */ |
||
| 594 | protected function clearPageCache(string $tableName, int $uid): void |
||
| 595 | { |
||
| 596 | $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 597 | if (empty($frameworkConfiguration['persistence']['enableAutomaticCacheClearing'])) { |
||
| 598 | return; |
||
| 599 | } |
||
| 600 | $pageIdsToClear = []; |
||
| 601 | $storagePage = null; |
||
| 602 | |||
| 603 | // As determining the table columns is a costly operation this is done only once per table during runtime and cached then |
||
| 604 | if (!isset($this->hasPidColumn[$tableName])) { |
||
| 605 | $columns = $this->connectionPool |
||
| 606 | ->getConnectionForTable($tableName) |
||
| 607 | ->getSchemaManager() |
||
| 608 | ->listTableColumns($tableName); |
||
| 609 | $this->hasPidColumn[$tableName] = array_key_exists('pid', $columns); |
||
| 610 | } |
||
| 611 | |||
| 612 | $tsfe = $this->getTSFE(); |
||
| 613 | if ($this->hasPidColumn[$tableName]) { |
||
| 614 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName); |
||
| 615 | $queryBuilder->getRestrictions()->removeAll(); |
||
| 616 | $result = $queryBuilder |
||
| 617 | ->select('pid') |
||
| 618 | ->from($tableName) |
||
| 619 | ->where( |
||
| 620 | $queryBuilder->expr()->eq( |
||
| 621 | 'uid', |
||
| 622 | $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT) |
||
| 623 | ) |
||
| 624 | ) |
||
| 625 | ->execute(); |
||
| 626 | if ($row = $result->fetch()) { |
||
| 627 | $storagePage = $row['pid']; |
||
| 628 | $pageIdsToClear[] = $storagePage; |
||
| 629 | } |
||
| 630 | } elseif (isset($tsfe)) { |
||
| 631 | // No PID column - we can do a best-effort to clear the cache of the current page if in FE |
||
| 632 | $storagePage = $tsfe->id; |
||
| 633 | $pageIdsToClear[] = $storagePage; |
||
| 634 | } |
||
| 635 | if ($storagePage === null) { |
||
| 636 | return; |
||
| 637 | } |
||
| 638 | |||
| 639 | $pageTS = BackendUtility::getPagesTSconfig($storagePage); |
||
| 640 | if (isset($pageTS['TCEMAIN.']['clearCacheCmd'])) { |
||
| 641 | $clearCacheCommands = GeneralUtility::trimExplode(',', strtolower($pageTS['TCEMAIN.']['clearCacheCmd']), true); |
||
| 642 | $clearCacheCommands = array_unique($clearCacheCommands); |
||
| 643 | foreach ($clearCacheCommands as $clearCacheCommand) { |
||
| 644 | if (MathUtility::canBeInterpretedAsInteger($clearCacheCommand)) { |
||
| 645 | $pageIdsToClear[] = $clearCacheCommand; |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | |||
| 650 | foreach ($pageIdsToClear as $pageIdToClear) { |
||
| 651 | $this->cacheService->getPageIdStack()->push($pageIdToClear); |
||
| 652 | } |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return TypoScriptFrontendController|null |
||
| 657 | */ |
||
| 658 | protected function getTSFE(): ?TypoScriptFrontendController |
||
| 661 | } |
||
| 662 | } |
||
| 663 |