| Total Complexity | 43 |
| Total Lines | 410 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Relation 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 Relation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Relation extends AbstractContentObject |
||
| 49 | { |
||
| 50 | public const CONTENT_OBJECT_NAME = 'SOLR_RELATION'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Content object configuration |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected array $configuration = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var TCAService |
||
| 61 | */ |
||
| 62 | protected TCAService $tcaService; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var FrontendOverlayService |
||
| 66 | */ |
||
| 67 | protected FrontendOverlayService $frontendOverlayService; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var TypoScriptFrontendController|null |
||
| 71 | */ |
||
| 72 | protected ?TypoScriptFrontendController $typoScriptFrontendController = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Relation constructor. |
||
| 76 | * @param TCAService|null $tcaService |
||
| 77 | * @param FrontendOverlayService|null $frontendOverlayService |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 80 | ContentObjectRenderer $cObj, |
||
| 81 | TCAService $tcaService = null, |
||
| 82 | FrontendOverlayService $frontendOverlayService = null |
||
| 83 | ) { |
||
| 84 | parent::__construct($cObj); |
||
|
|
|||
| 85 | $this->configuration['enableRecursiveValueResolution'] = 1; |
||
| 86 | $this->configuration['removeEmptyValues'] = 1; |
||
| 87 | $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class); |
||
| 88 | $this->typoScriptFrontendController = $this->getProtectedTsfeObjectFromContentObjectRenderer($cObj); |
||
| 89 | $this->frontendOverlayService = $frontendOverlayService ?? GeneralUtility::makeInstance(FrontendOverlayService::class, $this->tcaService, $this->typoScriptFrontendController); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Executes the SOLR_RELATION content object. |
||
| 94 | * |
||
| 95 | * Resolves relations between records. Currently, supported relations are |
||
| 96 | * TYPO3-style m:n relations. |
||
| 97 | * May resolve single value and multi value relations. |
||
| 98 | * |
||
| 99 | * @inheritDoc |
||
| 100 | * |
||
| 101 | * @param array $conf |
||
| 102 | * @return string |
||
| 103 | * @throws AspectNotFoundException |
||
| 104 | * @throws DBALException |
||
| 105 | * @noinspection PhpMissingReturnTypeInspection, because foreign source inheritance See {@link AbstractContentObject::render()} |
||
| 106 | */ |
||
| 107 | public function render($conf = []) |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Gets the related items of the current record's configured field. |
||
| 131 | * |
||
| 132 | * @param ContentObjectRenderer $parentContentObject parent content object |
||
| 133 | * |
||
| 134 | * @return array Array of related items, values already resolved from related records |
||
| 135 | * |
||
| 136 | * @throws AspectNotFoundException |
||
| 137 | * @throws DBALException |
||
| 138 | */ |
||
| 139 | protected function getRelatedItems(ContentObjectRenderer $parentContentObject): array |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the related items from a table using the n:m relation. |
||
| 163 | * |
||
| 164 | * @param string $localTableName Local table name |
||
| 165 | * @param int $localRecordUid Local record uid |
||
| 166 | * @param array $localFieldTca The local table's TCA |
||
| 167 | * @return array Array of related items, values already resolved from related records |
||
| 168 | * |
||
| 169 | * @throws AspectNotFoundException |
||
| 170 | * @throws DBALException |
||
| 171 | */ |
||
| 172 | protected function getRelatedItemsFromMMTable(string $localTableName, int $localRecordUid, array $localFieldTca): array |
||
| 173 | { |
||
| 174 | $relatedItems = []; |
||
| 175 | $foreignTableName = $localFieldTca['config']['foreign_table']; |
||
| 176 | $foreignTableTca = $this->tcaService->getTableConfiguration($foreignTableName); |
||
| 177 | $foreignTableLabelField = $this->resolveForeignTableLabelField($foreignTableTca); |
||
| 178 | $mmTableName = $localFieldTca['config']['MM']; |
||
| 179 | |||
| 180 | // Remove the first option of foreignLabelField for recursion |
||
| 181 | if (str_contains($this->configuration['foreignLabelField'] ?? '', '.')) { |
||
| 182 | $foreignTableLabelFieldArr = explode('.', $this->configuration['foreignLabelField']); |
||
| 183 | unset($foreignTableLabelFieldArr[0]); |
||
| 184 | $this->configuration['foreignLabelField'] = implode('.', $foreignTableLabelFieldArr); |
||
| 185 | } |
||
| 186 | |||
| 187 | $relationHandler = GeneralUtility::makeInstance(RelationHandler::class); |
||
| 188 | $relationHandler->start('', $foreignTableName, $mmTableName, $localRecordUid, $localTableName, $localFieldTca['config']); |
||
| 189 | $selectUids = $relationHandler->tableArray[$foreignTableName]; |
||
| 190 | if (!is_array($selectUids) || count($selectUids) <= 0) { |
||
| 191 | return $relatedItems; |
||
| 192 | } |
||
| 193 | |||
| 194 | $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
||
| 195 | $relatedRecords = $this->getRelatedRecords($foreignTableName, ...$selectUids); |
||
| 196 | foreach ($relatedRecords as $record) { |
||
| 197 | $contentObject->start($record, $foreignTableName); |
||
| 198 | |||
| 199 | if (isset($foreignTableTca['columns'][$foreignTableLabelField]['config']['foreign_table']) |
||
| 200 | && !empty($this->configuration['enableRecursiveValueResolution']) |
||
| 201 | ) { |
||
| 202 | $this->configuration['localField'] = $foreignTableLabelField; |
||
| 203 | if (str_contains($this->configuration['foreignLabelField'], '.')) { |
||
| 204 | $foreignTableLabelFieldArr = explode('.', $this->configuration['foreignLabelField']); |
||
| 205 | unset($foreignTableLabelFieldArr[0]); |
||
| 206 | $this->configuration['foreignLabelField'] = implode('.', $foreignTableLabelFieldArr); |
||
| 207 | } else { |
||
| 208 | unset($this->configuration['foreignLabelField']); |
||
| 209 | } |
||
| 210 | |||
| 211 | $relatedItems = array_merge($relatedItems, $this->getRelatedItems($contentObject)); |
||
| 212 | continue; |
||
| 213 | } |
||
| 214 | if ($this->getLanguageUid() > 0) { |
||
| 215 | $record = $this->frontendOverlayService->getOverlay($foreignTableName, $record); |
||
| 216 | } |
||
| 217 | |||
| 218 | $relatedItems[] = $contentObject->stdWrap($record[$foreignTableLabelField] ?? '', $this->configuration) ?? ''; |
||
| 219 | } |
||
| 220 | |||
| 221 | return $relatedItems; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Resolves the field to use as the related item's label depending on TCA |
||
| 226 | * and TypoScript configuration |
||
| 227 | * |
||
| 228 | * @param array $foreignTableTca The foreign table's TCA |
||
| 229 | * |
||
| 230 | * @return string|null The field to use for the related item's label |
||
| 231 | */ |
||
| 232 | protected function resolveForeignTableLabelField(array $foreignTableTca): ?string |
||
| 233 | { |
||
| 234 | $foreignTableLabelField = $foreignTableTca['ctrl']['label'] ?? null; |
||
| 235 | |||
| 236 | // when foreignLabelField is not enabled we can return directly |
||
| 237 | if (empty($this->configuration['foreignLabelField'])) { |
||
| 238 | return $foreignTableLabelField; |
||
| 239 | } |
||
| 240 | |||
| 241 | if (str_contains($this->configuration['foreignLabelField'] ?? '', '.')) { |
||
| 242 | list($foreignTableLabelField) = explode('.', $this->configuration['foreignLabelField'], 2); |
||
| 243 | } else { |
||
| 244 | $foreignTableLabelField = $this->configuration['foreignLabelField']; |
||
| 245 | } |
||
| 246 | |||
| 247 | return $foreignTableLabelField; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Gets the related items from a table using a 1:n relation. |
||
| 252 | * |
||
| 253 | * @param string $localTableName Local table name |
||
| 254 | * @param int $localRecordUid Local record uid |
||
| 255 | * @param array $localFieldTca The local table's TCA |
||
| 256 | * @param ContentObjectRenderer $parentContentObject parent content object |
||
| 257 | * @return array Array of related items, values already resolved from related records |
||
| 258 | * |
||
| 259 | * @throws AspectNotFoundException |
||
| 260 | * @throws DBALException |
||
| 261 | */ |
||
| 262 | protected function getRelatedItemsFromForeignTable( |
||
| 263 | string $localTableName, |
||
| 264 | int $localRecordUid, |
||
| 265 | array $localFieldTca, |
||
| 266 | ContentObjectRenderer $parentContentObject |
||
| 267 | ): array { |
||
| 268 | $relatedItems = []; |
||
| 269 | $foreignTableName = $localFieldTca['config']['foreign_table']; |
||
| 270 | $foreignTableTca = $this->tcaService->getTableConfiguration($foreignTableName); |
||
| 271 | $foreignTableLabelField = $this->resolveForeignTableLabelField($foreignTableTca); |
||
| 272 | $localField = $this->configuration['localField']; |
||
| 273 | |||
| 274 | /** @var $relationHandler RelationHandler */ |
||
| 275 | $relationHandler = GeneralUtility::makeInstance(RelationHandler::class); |
||
| 276 | if (!empty($localFieldTca['config']['MM'] ?? '')) { |
||
| 277 | $relationHandler->start( |
||
| 278 | '', |
||
| 279 | $foreignTableName, |
||
| 280 | $localFieldTca['config']['MM'], |
||
| 281 | $localRecordUid, |
||
| 282 | $localTableName, |
||
| 283 | $localFieldTca['config'] |
||
| 284 | ); |
||
| 285 | } else { |
||
| 286 | $itemList = $parentContentObject->data[$localField] ?? ''; |
||
| 287 | $relationHandler->start($itemList, $foreignTableName, '', $localRecordUid, $localTableName, $localFieldTca['config']); |
||
| 288 | } |
||
| 289 | |||
| 290 | $selectUids = $relationHandler->tableArray[$foreignTableName]; |
||
| 291 | if (!is_array($selectUids) || count($selectUids) <= 0) { |
||
| 292 | return $relatedItems; |
||
| 293 | } |
||
| 294 | |||
| 295 | $relatedRecords = $this->getRelatedRecords($foreignTableName, ...$selectUids); |
||
| 296 | foreach ($relatedRecords as $relatedRecord) { |
||
| 297 | $resolveRelatedValues = $this->resolveRelatedValue( |
||
| 298 | $relatedRecord, |
||
| 299 | $foreignTableTca, |
||
| 300 | $foreignTableLabelField, |
||
| 301 | $parentContentObject, |
||
| 302 | $foreignTableName |
||
| 303 | ); |
||
| 304 | |||
| 305 | foreach ($resolveRelatedValues as $resolveRelatedValue) { |
||
| 306 | if (!empty($resolveRelatedValue) || !$this->configuration['removeEmptyValues']) { |
||
| 307 | $relatedItems[] = $resolveRelatedValue; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | return $relatedItems; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Resolves the value of the related field. If the related field's value is |
||
| 317 | * a relation itself, this method takes care of resolving it recursively. |
||
| 318 | * |
||
| 319 | * @param array $relatedRecord Related record as array |
||
| 320 | * @param array $foreignTableTca TCA of the related table |
||
| 321 | * @param string $foreignTableLabelField Field name of the foreign label field |
||
| 322 | * @param ContentObjectRenderer $parentContentObject cObject |
||
| 323 | * @param string $foreignTableName Related record table name |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | * |
||
| 327 | * @throws AspectNotFoundException |
||
| 328 | * @throws DBALException |
||
| 329 | */ |
||
| 330 | protected function resolveRelatedValue( |
||
| 331 | array $relatedRecord, |
||
| 332 | array $foreignTableTca, |
||
| 333 | string $foreignTableLabelField, |
||
| 334 | ContentObjectRenderer $parentContentObject, |
||
| 335 | string $foreignTableName = '' |
||
| 336 | ): array { |
||
| 337 | if ($this->getLanguageUid() > 0 && !empty($foreignTableName)) { |
||
| 338 | $relatedRecord = $this->frontendOverlayService->getOverlay($foreignTableName, $relatedRecord); |
||
| 339 | } |
||
| 340 | |||
| 341 | $values = [$relatedRecord[$foreignTableLabelField]]; |
||
| 342 | |||
| 343 | if ( |
||
| 344 | !empty($foreignTableName) |
||
| 345 | && isset($foreignTableTca['columns'][$foreignTableLabelField]['config']['foreign_table']) |
||
| 346 | && $this->configuration['enableRecursiveValueResolution'] |
||
| 347 | ) { |
||
| 348 | // backup |
||
| 349 | $backupRecord = $parentContentObject->data; |
||
| 350 | $backupConfiguration = $this->configuration; |
||
| 351 | |||
| 352 | // adjust configuration for next level |
||
| 353 | $this->configuration['localField'] = $foreignTableLabelField; |
||
| 354 | $parentContentObject->data = $relatedRecord; |
||
| 355 | if (str_contains($this->configuration['foreignLabelField'], '.')) { |
||
| 356 | list(, $this->configuration['foreignLabelField']) = explode( |
||
| 357 | '.', |
||
| 358 | $this->configuration['foreignLabelField'], |
||
| 359 | 2 |
||
| 360 | ); |
||
| 361 | } else { |
||
| 362 | $this->configuration['foreignLabelField'] = ''; |
||
| 363 | } |
||
| 364 | |||
| 365 | // recursion |
||
| 366 | $relatedItemsFromForeignTable = $this->getRelatedItemsFromForeignTable( |
||
| 367 | $foreignTableName, |
||
| 368 | $relatedRecord['uid'], |
||
| 369 | $foreignTableTca['columns'][$foreignTableLabelField], |
||
| 370 | $parentContentObject |
||
| 371 | ); |
||
| 372 | $values = $relatedItemsFromForeignTable; |
||
| 373 | |||
| 374 | // restore |
||
| 375 | $this->configuration = $backupConfiguration; |
||
| 376 | $parentContentObject->data = $backupRecord; |
||
| 377 | } |
||
| 378 | foreach ($values as &$value) { |
||
| 379 | $value = $parentContentObject->stdWrap($value, $this->configuration) ?? ''; |
||
| 380 | } |
||
| 381 | |||
| 382 | return $values; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Return records via relation. |
||
| 387 | * |
||
| 388 | * @param string $foreignTable The table to fetch records from. |
||
| 389 | * @param int ...$uids The uids to fetch from table. |
||
| 390 | * @return array |
||
| 391 | * |
||
| 392 | * @throws \Doctrine\DBAL\DBALException |
||
| 393 | */ |
||
| 394 | protected function getRelatedRecords(string $foreignTable, int ...$uids): array |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Sorts the result set by key in array for IN values. |
||
| 411 | * Simulates MySqls ORDER BY FIELD(fieldname, COPY_OF_IN_FOR_WHERE) |
||
| 412 | * Example: SELECT * FROM a_table WHERE field_name IN (2, 3, 4) SORT BY FIELD(field_name, 2, 3, 4) |
||
| 413 | * |
||
| 414 | * |
||
| 415 | * @param Result $statement |
||
| 416 | * @param string $columnName |
||
| 417 | * @param array $arrayWithValuesForIN |
||
| 418 | * @return array |
||
| 419 | * @throws DBALException |
||
| 420 | */ |
||
| 421 | protected function sortByKeyInIN(Result $statement, string $columnName, ...$arrayWithValuesForIN): array |
||
| 422 | { |
||
| 423 | $records = []; |
||
| 424 | while ($record = $statement->fetchAssociative()) { |
||
| 425 | $indexNumber = array_search($record[$columnName], $arrayWithValuesForIN); |
||
| 426 | $records[$indexNumber] = $record; |
||
| 427 | } |
||
| 428 | ksort($records); |
||
| 429 | return $records; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Returns current language id fetched from object properties chain TSFE->context->language aspect->id. |
||
| 434 | * |
||
| 435 | * @return int |
||
| 436 | * @throws AspectNotFoundException |
||
| 437 | */ |
||
| 438 | protected function getLanguageUid(): int |
||
| 439 | { |
||
| 440 | return (int)$this->typoScriptFrontendController->/** @scrutinizer ignore-call */ getContext()->getPropertyFromAspect('language', 'id'); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns inaccessible {@link ContentObjectRenderer::typoScriptFrontendController} via reflection. |
||
| 445 | * |
||
| 446 | * The TypoScriptFrontendController object must be delegated to the whole object aggregation on indexing stack, |
||
| 447 | * to be able to use Contexts properties and proceed indexing request. |
||
| 448 | * |
||
| 449 | * @param ContentObjectRenderer $cObj |
||
| 450 | * @return TypoScriptFrontendController|null |
||
| 451 | */ |
||
| 452 | protected function getProtectedTsfeObjectFromContentObjectRenderer(ContentObjectRenderer $cObj): ?TypoScriptFrontendController |
||
| 458 | } |
||
| 459 | } |
||
| 460 |