| Total Complexity | 71 |
| Total Lines | 547 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SlugHelper 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 SlugHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class SlugHelper |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $tableName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $fieldName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $workspaceId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $configuration = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $workspaceEnabled; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Defines whether the slug field should start with "/". |
||
| 66 | * For pages (due to rootline functionality), this is a must have, otherwise the root level page |
||
| 67 | * would have an empty value. |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $prependSlashInSlug; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Slug constructor. |
||
| 75 | * |
||
| 76 | * @param string $tableName TCA table |
||
| 77 | * @param string $fieldName TCA field |
||
| 78 | * @param array $configuration TCA configuration of the field |
||
| 79 | * @param int $workspaceId the workspace ID to be working on. |
||
| 80 | */ |
||
| 81 | public function __construct(string $tableName, string $fieldName, array $configuration, int $workspaceId = 0) |
||
| 82 | { |
||
| 83 | $this->tableName = $tableName; |
||
| 84 | $this->fieldName = $fieldName; |
||
| 85 | $this->configuration = $configuration; |
||
| 86 | $this->workspaceId = $workspaceId; |
||
| 87 | |||
| 88 | if ($this->tableName === 'pages' && $this->fieldName === 'slug') { |
||
| 89 | $this->prependSlashInSlug = true; |
||
| 90 | } else { |
||
| 91 | $this->prependSlashInSlug = $this->configuration['prependSlash'] ?? false; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->workspaceEnabled = BackendUtility::isTableWorkspaceEnabled($tableName); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Cleans a slug value so it is used directly in the path segment of a URL. |
||
| 99 | * |
||
| 100 | * @param string $slug |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function sanitize(string $slug): string |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Extracts payload of slug and removes wrapping delimiters, |
||
| 140 | * e.g. `/hello/world/` will become `hello/world`. |
||
| 141 | * |
||
| 142 | * @param string $slug |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function extract(string $slug): string |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Used when no slug exists for a record |
||
| 154 | * |
||
| 155 | * @param array $recordData |
||
| 156 | * @param int $pid The uid of the page to generate the slug for |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function generate(array $recordData, int $pid): string |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Checks if there are other records with the same slug that are located on the same PID. |
||
| 236 | * |
||
| 237 | * @param string $slug |
||
| 238 | * @param RecordState $state |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function isUniqueInPid(string $slug, RecordState $state): bool |
||
| 242 | { |
||
| 243 | $pageId = (int)$state->resolveNodeIdentifier(); |
||
| 244 | $recordId = $state->getSubject()->getIdentifier(); |
||
| 245 | $languageId = $state->getContext()->getLanguageId(); |
||
| 246 | |||
| 247 | $queryBuilder = $this->createPreparedQueryBuilder(); |
||
| 248 | $this->applySlugConstraint($queryBuilder, $slug); |
||
| 249 | $this->applyPageIdConstraint($queryBuilder, $pageId); |
||
| 250 | $this->applyRecordConstraint($queryBuilder, $recordId); |
||
| 251 | $this->applyLanguageConstraint($queryBuilder, $languageId); |
||
| 252 | $this->applyWorkspaceConstraint($queryBuilder, $state); |
||
| 253 | $statement = $queryBuilder->execute(); |
||
| 254 | |||
| 255 | $records = $this->resolveVersionOverlays( |
||
| 256 | $statement->fetchAll() |
||
| 257 | ); |
||
| 258 | return count($records) === 0; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if there are other records with the same slug that are located on the same site. |
||
| 263 | * |
||
| 264 | * @param string $slug |
||
| 265 | * @param RecordState $state |
||
| 266 | * @return bool |
||
| 267 | * @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException |
||
| 268 | */ |
||
| 269 | public function isUniqueInSite(string $slug, RecordState $state): bool |
||
| 270 | { |
||
| 271 | $pageId = $state->resolveNodeAggregateIdentifier(); |
||
| 272 | $recordId = $state->getSubject()->getIdentifier(); |
||
| 273 | $languageId = $state->getContext()->getLanguageId(); |
||
| 274 | |||
| 275 | if (!MathUtility::canBeInterpretedAsInteger($pageId)) { |
||
| 276 | // If this is a new page, we use the parent page to resolve the site |
||
| 277 | $pageId = $state->getNode()->getIdentifier(); |
||
| 278 | } |
||
| 279 | $pageId = (int)$pageId; |
||
| 280 | |||
| 281 | $queryBuilder = $this->createPreparedQueryBuilder(); |
||
| 282 | $this->applySlugConstraint($queryBuilder, $slug); |
||
| 283 | $this->applyRecordConstraint($queryBuilder, $recordId); |
||
| 284 | $this->applyLanguageConstraint($queryBuilder, $languageId); |
||
| 285 | $this->applyWorkspaceConstraint($queryBuilder, $state); |
||
| 286 | $statement = $queryBuilder->execute(); |
||
| 287 | |||
| 288 | $records = $this->resolveVersionOverlays( |
||
| 289 | $statement->fetchAll() |
||
| 290 | ); |
||
| 291 | if (count($records) === 0) { |
||
| 292 | return true; |
||
| 293 | } |
||
| 294 | |||
| 295 | // The installation contains at least ONE other record with the same slug |
||
| 296 | // Now find out if it is the same root page ID |
||
| 297 | $this->flushRootLineCaches(); |
||
| 298 | $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
||
| 299 | try { |
||
| 300 | $siteOfCurrentRecord = $siteFinder->getSiteByPageId($pageId); |
||
| 301 | } catch (SiteNotFoundException $e) { |
||
| 302 | // Not within a site, so nothing to do |
||
| 303 | return true; |
||
| 304 | } |
||
| 305 | foreach ($records as $record) { |
||
| 306 | try { |
||
| 307 | $recordState = RecordStateFactory::forName($this->tableName)->fromArray($record); |
||
| 308 | $siteOfExistingRecord = $siteFinder->getSiteByPageId( |
||
| 309 | (int)$recordState->resolveNodeAggregateIdentifier() |
||
| 310 | ); |
||
| 311 | } catch (SiteNotFoundException $exception) { |
||
| 312 | // In case not site is found, the record is not |
||
| 313 | // organized in any site |
||
| 314 | continue; |
||
| 315 | } |
||
| 316 | if ($siteOfExistingRecord->getRootPageId() === $siteOfCurrentRecord->getRootPageId()) { |
||
| 317 | return false; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | // Otherwise, everything is still fine |
||
| 322 | return true; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Ensure root line caches are flushed to avoid any issue regarding moving of pages or dynamically creating |
||
| 327 | * sites while managing slugs at the same request |
||
| 328 | */ |
||
| 329 | protected function flushRootLineCaches(): void |
||
| 330 | { |
||
| 331 | RootlineUtility::purgeCaches(); |
||
| 332 | GeneralUtility::makeInstance(CacheManager::class)->getCache('rootline')->flush(); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Generate a slug with a suffix "/mytitle-1" if that is in use already. |
||
| 337 | * |
||
| 338 | * @param string $slug proposed slug |
||
| 339 | * @param RecordState $state |
||
| 340 | * @return string |
||
| 341 | * @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException |
||
| 342 | */ |
||
| 343 | public function buildSlugForUniqueInSite(string $slug, RecordState $state): string |
||
| 344 | { |
||
| 345 | $slug = $this->sanitize($slug); |
||
| 346 | $rawValue = $this->extract($slug); |
||
| 347 | $newValue = $slug; |
||
| 348 | $counter = 0; |
||
| 349 | while (!$this->isUniqueInSite( |
||
| 350 | $newValue, |
||
| 351 | $state |
||
| 352 | ) && $counter++ < 100 |
||
| 353 | ) { |
||
| 354 | $newValue = $this->sanitize($rawValue . '-' . $counter); |
||
| 355 | } |
||
| 356 | if ($counter === 100) { |
||
| 357 | $newValue = $this->sanitize($rawValue . '-' . GeneralUtility::shortMD5($rawValue)); |
||
| 358 | } |
||
| 359 | return $newValue; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Generate a slug with a suffix "/mytitle-1" if the suggested slug is in use already. |
||
| 364 | * |
||
| 365 | * @param string $slug proposed slug |
||
| 366 | * @param RecordState $state |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function buildSlugForUniqueInPid(string $slug, RecordState $state): string |
||
| 370 | { |
||
| 371 | $slug = $this->sanitize($slug); |
||
| 372 | $rawValue = $this->extract($slug); |
||
| 373 | $newValue = $slug; |
||
| 374 | $counter = 0; |
||
| 375 | while (!$this->isUniqueInPid( |
||
| 376 | $newValue, |
||
| 377 | $state |
||
| 378 | ) && $counter++ < 100 |
||
| 379 | ) { |
||
| 380 | $newValue = $this->sanitize($rawValue . '-' . $counter); |
||
| 381 | } |
||
| 382 | if ($counter === 100) { |
||
| 383 | $newValue = $this->sanitize($rawValue . '-' . GeneralUtility::shortMD5($rawValue)); |
||
| 384 | } |
||
| 385 | return $newValue; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return QueryBuilder |
||
| 390 | */ |
||
| 391 | protected function createPreparedQueryBuilder(): QueryBuilder |
||
| 392 | { |
||
| 393 | $fieldNames = ['uid', 'pid', $this->fieldName]; |
||
| 394 | if ($this->workspaceEnabled) { |
||
| 395 | $fieldNames[] = 't3ver_state'; |
||
| 396 | $fieldNames[] = 't3ver_oid'; |
||
| 397 | } |
||
| 398 | $languageFieldName = $GLOBALS['TCA'][$this->tableName]['ctrl']['languageField'] ?? null; |
||
| 399 | if (is_string($languageFieldName)) { |
||
| 400 | $fieldNames[] = $languageFieldName; |
||
| 401 | } |
||
| 402 | $languageParentFieldName = $GLOBALS['TCA'][$this->tableName]['ctrl']['transOrigPointerField'] ?? null; |
||
| 403 | if (is_string($languageParentFieldName)) { |
||
| 404 | $fieldNames[] = $languageParentFieldName; |
||
| 405 | } |
||
| 406 | |||
| 407 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
||
| 408 | $queryBuilder->getRestrictions() |
||
| 409 | ->removeAll() |
||
| 410 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 411 | $queryBuilder |
||
| 412 | ->select(...$fieldNames) |
||
| 413 | ->from($this->tableName); |
||
| 414 | return $queryBuilder; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param QueryBuilder $queryBuilder |
||
| 419 | * @param RecordState $state |
||
| 420 | */ |
||
| 421 | protected function applyWorkspaceConstraint(QueryBuilder $queryBuilder, RecordState $state) |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param QueryBuilder $queryBuilder |
||
| 441 | * @param int $languageId |
||
| 442 | */ |
||
| 443 | protected function applyLanguageConstraint(QueryBuilder $queryBuilder, int $languageId) |
||
| 444 | { |
||
| 445 | $languageFieldName = $GLOBALS['TCA'][$this->tableName]['ctrl']['languageField'] ?? null; |
||
| 446 | if (!is_string($languageFieldName)) { |
||
| 447 | return; |
||
| 448 | } |
||
| 449 | |||
| 450 | // Only check records of the given language |
||
| 451 | $queryBuilder->andWhere( |
||
| 452 | $queryBuilder->expr()->eq( |
||
| 453 | $languageFieldName, |
||
| 454 | $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT) |
||
| 455 | ) |
||
| 456 | ); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param QueryBuilder $queryBuilder |
||
| 461 | * @param string $slug |
||
| 462 | */ |
||
| 463 | protected function applySlugConstraint(QueryBuilder $queryBuilder, string $slug) |
||
| 464 | { |
||
| 465 | $queryBuilder->where( |
||
| 466 | $queryBuilder->expr()->eq( |
||
| 467 | $this->fieldName, |
||
| 468 | $queryBuilder->createNamedParameter($slug) |
||
| 469 | ) |
||
| 470 | ); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param QueryBuilder $queryBuilder |
||
| 475 | * @param int $pageId |
||
| 476 | */ |
||
| 477 | protected function applyPageIdConstraint(QueryBuilder $queryBuilder, int $pageId) |
||
| 478 | { |
||
| 479 | if ($pageId < 0) { |
||
| 480 | throw new \RuntimeException( |
||
| 481 | sprintf( |
||
| 482 | 'Page id must be positive "%d"', |
||
| 483 | $pageId |
||
| 484 | ), |
||
| 485 | 1534962573 |
||
| 486 | ); |
||
| 487 | } |
||
| 488 | |||
| 489 | $queryBuilder->andWhere( |
||
| 490 | $queryBuilder->expr()->eq( |
||
| 491 | 'pid', |
||
| 492 | $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT) |
||
| 493 | ) |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param QueryBuilder $queryBuilder |
||
| 499 | * @param string|int $recordId |
||
| 500 | */ |
||
| 501 | protected function applyRecordConstraint(QueryBuilder $queryBuilder, $recordId) |
||
| 515 | ); |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param array $records |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | protected function resolveVersionOverlays(array $records): array |
||
| 545 | ) |
||
| 546 | ); |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Fetch a parent page, but exclude spacers, recyclers and sys-folders and all doktypes > 200 |
||
| 551 | * @param int $pid |
||
| 552 | * @param int $languageId |
||
| 553 | * @return array|null |
||
| 554 | */ |
||
| 555 | protected function resolveParentPageRecord(int $pid, int $languageId): ?array |
||
| 584 | } |
||
| 585 | } |
||
| 586 |