Complex classes like Indexer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Indexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class Indexer extends AbstractIndexer |
||
| 51 | { |
||
| 52 | |||
| 53 | # TODO change to singular $document instead of plural $documents |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A Solr service instance to interact with the Solr server |
||
| 57 | * |
||
| 58 | * @var SolrService |
||
| 59 | */ |
||
| 60 | protected $solr; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ConnectionManager |
||
| 64 | */ |
||
| 65 | protected $connectionManager; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Holds options for a specific indexer |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $options = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * To log or not to log... #Shakespeare |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $loggingEnabled = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var IdBuilder |
||
| 83 | */ |
||
| 84 | protected $variantIdBuilder; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Cache of the sys_language_overlay information |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected static $sysLanguageOverlay = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 95 | */ |
||
| 96 | protected $logger = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Constructor |
||
| 100 | * |
||
| 101 | * @param array $options array of indexer options |
||
| 102 | * @param IdBuilder $idBuilder |
||
| 103 | */ |
||
| 104 | 17 | public function __construct(array $options = [], IdBuilder $idBuilder = null) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Indexes an item from the indexing queue. |
||
| 114 | * |
||
| 115 | * @param Item $item An index queue item |
||
| 116 | * @return bool returns true when indexed, false when not |
||
| 117 | */ |
||
| 118 | 12 | public function index(Item $item) |
|
| 119 | { |
||
| 120 | 12 | $indexed = true; |
|
| 121 | |||
| 122 | 12 | $this->type = $item->getType(); |
|
| 123 | 12 | $this->setLogging($item); |
|
| 124 | |||
| 125 | 12 | $solrConnections = $this->getSolrConnectionsByItem($item); |
|
| 126 | |||
| 127 | 12 | foreach ($solrConnections as $systemLanguageUid => $solrConnection) { |
|
| 128 | 12 | $this->solr = $solrConnection; |
|
| 129 | |||
| 130 | 12 | if (!$this->indexItem($item, $systemLanguageUid)) { |
|
| 131 | /* |
||
| 132 | * A single language voting for "not indexed" should make the whole |
||
| 133 | * item count as being not indexed, even if all other languages are |
||
| 134 | * indexed. |
||
| 135 | * If there is no translation for a single language, this item counts |
||
| 136 | * as TRUE since it's not an error which that should make the item |
||
| 137 | * being reindexed during another index run. |
||
| 138 | */ |
||
| 139 | $indexed = false; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 12 | return $indexed; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Creates a single Solr Document for an item in a specific language. |
||
| 148 | * |
||
| 149 | * @param Item $item An index queue item to index. |
||
| 150 | * @param int $language The language to use. |
||
| 151 | * @return bool TRUE if item was indexed successfully, FALSE on failure |
||
| 152 | */ |
||
| 153 | 12 | protected function indexItem(Item $item, $language = 0) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Gets the full item record. |
||
| 186 | * |
||
| 187 | * This general record indexer simply gets the record from the item. Other |
||
| 188 | * more specialized indexers may provide more data for their specific item |
||
| 189 | * types. |
||
| 190 | * |
||
| 191 | * @param Item $item The item to be indexed |
||
| 192 | * @param int $language Language Id (sys_language.uid) |
||
| 193 | * @return array|NULL The full record with fields of data to be used for indexing or NULL to prevent an item from being indexed |
||
| 194 | */ |
||
| 195 | 12 | protected function getFullItemRecord(Item $item, $language = 0) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the configuration how to process an item's fields for indexing. |
||
| 267 | * |
||
| 268 | * @param Item $item An index queue item |
||
| 269 | * @param int $language Language ID |
||
| 270 | * @throws \RuntimeException |
||
| 271 | * @return array Configuration array from TypoScript |
||
| 272 | */ |
||
| 273 | 12 | protected function getItemTypeConfiguration(Item $item, $language = 0) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * The method retrieves the field configuration of the items record page id (pid). |
||
| 290 | * |
||
| 291 | * @param Item $item |
||
| 292 | * @param integer $language |
||
| 293 | * @param string $indexConfigurationName |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | 12 | protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * The method returns the field configuration of the items root page id (uid of the related root page). |
||
| 308 | * |
||
| 309 | * @param Item $item |
||
| 310 | * @param integer $language |
||
| 311 | * @param string $indexConfigurationName |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | 1 | protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Converts an item array (record) to a Solr document by mapping the |
||
| 326 | * record's fields onto Solr document fields as configured in TypoScript. |
||
| 327 | * |
||
| 328 | * @param Item $item An index queue item |
||
| 329 | * @param int $language Language Id |
||
| 330 | * @return Apache_Solr_Document The Solr document converted from the record |
||
| 331 | */ |
||
| 332 | 12 | protected function itemToDocument(Item $item, $language = 0) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Creates a Solr document with the basic / core fields set already. |
||
| 348 | * |
||
| 349 | * @param Item $item The item to index |
||
| 350 | * @param array $itemRecord The record to use to build the base document |
||
| 351 | * @return Apache_Solr_Document A basic Solr document |
||
| 352 | */ |
||
| 353 | 12 | protected function getBaseDocument(Item $item, array $itemRecord) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Generates an Access Rootline for an item. |
||
| 402 | * |
||
| 403 | * @param Item $item Index Queue item to index. |
||
| 404 | * @return string The Access Rootline for the item |
||
| 405 | */ |
||
| 406 | 12 | protected function getAccessRootline(Item $item) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Sends the documents to the field processing service which takes care of |
||
| 427 | * manipulating fields as defined in the field's configuration. |
||
| 428 | * |
||
| 429 | * @param Item $item An index queue item |
||
| 430 | * @param array $documents An array of Apache_Solr_Document objects to manipulate. |
||
| 431 | * @return array Array of manipulated Apache_Solr_Document objects. |
||
| 432 | */ |
||
| 433 | 12 | protected function processDocuments(Item $item, array $documents) |
|
| 434 | { |
||
| 435 | // needs to respect the TS settings for the page the item is on, conditions may apply |
||
| 436 | 12 | $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid()); |
|
| 437 | 12 | $fieldProcessingInstructions = $solrConfiguration->getIndexFieldProcessingInstructionsConfiguration(); |
|
| 438 | |||
| 439 | // same as in the FE indexer |
||
| 440 | 12 | if (is_array($fieldProcessingInstructions)) { |
|
| 441 | 12 | $service = GeneralUtility::makeInstance(Service::class); |
|
| 442 | 12 | $service->processDocuments( |
|
| 443 | $documents, |
||
| 444 | 12 | $fieldProcessingInstructions |
|
| 445 | ); |
||
| 446 | } |
||
| 447 | |||
| 448 | 12 | return $documents; |
|
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Allows third party extensions to provide additional documents which |
||
| 453 | * should be indexed for the current item. |
||
| 454 | * |
||
| 455 | * @param Item $item The item currently being indexed. |
||
| 456 | * @param int $language The language uid currently being indexed. |
||
| 457 | * @param Apache_Solr_Document $itemDocument The document representing the item for the given language. |
||
| 458 | * @return array An array of additional Apache_Solr_Document objects to index. |
||
| 459 | */ |
||
| 460 | 16 | protected function getAdditionalDocuments( |
|
| 461 | Item $item, |
||
| 462 | $language, |
||
| 463 | Apache_Solr_Document $itemDocument |
||
| 464 | ) { |
||
| 465 | 16 | $documents = []; |
|
| 466 | |||
| 467 | 16 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'])) { |
|
| 468 | 4 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'] as $classReference) { |
|
| 469 | 4 | if (!class_exists($classReference)) { |
|
| 470 | 2 | throw new \InvalidArgumentException('Class does not exits' . $classReference, 1490363487); |
|
| 471 | } |
||
| 472 | 2 | $additionalIndexer = GeneralUtility::makeInstance($classReference); |
|
| 473 | 2 | if ($additionalIndexer instanceof AdditionalIndexQueueItemIndexer) { |
|
| 474 | 1 | $additionalDocuments = $additionalIndexer->getAdditionalItemDocuments($item, |
|
| 475 | 1 | $language, $itemDocument); |
|
| 476 | |||
| 477 | 1 | if (is_array($additionalDocuments)) { |
|
| 478 | 1 | $documents = array_merge($documents, |
|
| 479 | 1 | $additionalDocuments); |
|
| 480 | } |
||
| 481 | } else { |
||
| 482 | 1 | throw new \UnexpectedValueException( |
|
| 483 | 1 | get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class, |
|
| 484 | 1 | 1326284551 |
|
| 485 | ); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 489 | 13 | return $documents; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Provides a hook to manipulate documents right before they get added to |
||
| 494 | * the Solr index. |
||
| 495 | * |
||
| 496 | * @param Item $item The item currently being indexed. |
||
| 497 | * @param int $language The language uid of the documents |
||
| 498 | * @param array $documents An array of documents to be indexed |
||
| 499 | * @return array An array of modified documents |
||
| 500 | */ |
||
| 501 | 12 | protected function preAddModifyDocuments( |
|
| 527 | |||
| 528 | // Initialization |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Gets the Solr connections applicable for an item. |
||
| 532 | * |
||
| 533 | * The connections include the default connection and connections to be used |
||
| 534 | * for translations of an item. |
||
| 535 | * |
||
| 536 | * @param Item $item An index queue item |
||
| 537 | * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections, the array's keys are the sys_language_uid of the language of the connection |
||
| 538 | */ |
||
| 539 | 13 | protected function getSolrConnectionsByItem(Item $item) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Finds the alternative page language overlay records for a page based on |
||
| 579 | * the sys_language_mode. |
||
| 580 | * |
||
| 581 | * Possible Language Modes: |
||
| 582 | * 1) content_fallback --> all languages |
||
| 583 | * 2) strict --> available languages with page overlay |
||
| 584 | * 3) ignore --> available languages with page overlay |
||
| 585 | * 4) unknown mode or blank --> all languages |
||
| 586 | * |
||
| 587 | * @param int $pageId Page ID. |
||
| 588 | * @param string $languageMode |
||
| 589 | * @return array An array of translation overlays (or fake overlays) found for the given page. |
||
| 590 | */ |
||
| 591 | 13 | protected function getTranslationOverlaysForPage($pageId, $languageMode) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Returns an array of system languages. |
||
| 629 | * |
||
| 630 | * @return array |
||
| 631 | */ |
||
| 632 | 12 | protected function getSystemLanguages() |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Checks for which languages connections have been configured and returns |
||
| 639 | * these connections. |
||
| 640 | * |
||
| 641 | * @param array $translationOverlays An array of translation overlays to check for configured connections. |
||
| 642 | * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections. |
||
| 643 | */ |
||
| 644 | 13 | protected function getConnectionsForIndexableLanguages( |
|
| 665 | |||
| 666 | // Utility methods |
||
| 667 | |||
| 668 | // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer |
||
| 669 | // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Enables logging dependent on the configuration of the item's site |
||
| 673 | * |
||
| 674 | * @param Item $item An item being indexed |
||
| 675 | * @return void |
||
| 676 | */ |
||
| 677 | 13 | protected function setLogging(Item $item) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Logs the item and what document was created from it |
||
| 687 | * |
||
| 688 | * @param Item $item The item that is being indexed. |
||
| 689 | * @param array $itemDocuments An array of Solr documents created from the item's data |
||
| 690 | * @param Apache_Solr_Response $response The Solr response for the particular index document |
||
| 691 | */ |
||
| 692 | 12 | protected function log( |
|
| 733 | } |
||
| 734 |