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 SolrConnection |
||
| 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 \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 83 | */ |
||
| 84 | protected $logger = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var PagesRepository |
||
| 88 | */ |
||
| 89 | protected $pagesRepository; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var Builder |
||
| 93 | */ |
||
| 94 | protected $documentBuilder; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Constructor |
||
| 98 | * |
||
| 99 | * @param array $options array of indexer options |
||
| 100 | * @param PagesRepository|null $pagesRepository |
||
| 101 | * @param Builder|null $documentBuilder |
||
| 102 | */ |
||
| 103 | public function __construct(array $options = [], PagesRepository $pagesRepository = null, Builder $documentBuilder = null) |
||
| 111 | 25 | ||
| 112 | 25 | /** |
|
| 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 | public function index(Item $item) |
||
| 145 | 19 | ||
| 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 | protected function indexItem(Item $item, $language = 0) |
||
| 183 | 19 | ||
| 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 | protected function getFullItemRecord(Item $item, $language = 0) |
||
| 244 | 19 | ||
| 245 | /** |
||
| 246 | * Returns the overlayed item record. |
||
| 247 | * |
||
| 248 | * @param Item $item |
||
| 249 | * @param int $language |
||
| 250 | * @param string|null $systemLanguageContentOverlay |
||
| 251 | * @return array|mixed|null |
||
| 252 | */ |
||
| 253 | protected function getItemRecordOverlayed(Item $item, $language, $systemLanguageContentOverlay) |
||
| 269 | 19 | ||
| 270 | /** |
||
| 271 | * Gets the configuration how to process an item's fields for indexing. |
||
| 272 | * |
||
| 273 | * @param Item $item An index queue item |
||
| 274 | * @param int $language Language ID |
||
| 275 | * @throws \RuntimeException |
||
| 276 | * @return array Configuration array from TypoScript |
||
| 277 | */ |
||
| 278 | protected function getItemTypeConfiguration(Item $item, $language = 0) |
||
| 292 | 19 | ||
| 293 | /** |
||
| 294 | * The method retrieves the field configuration of the items record page id (pid). |
||
| 295 | * |
||
| 296 | * @param Item $item |
||
| 297 | * @param integer $language |
||
| 298 | * @param string $indexConfigurationName |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * The method returns the field configuration of the items root page id (uid of the related root page). |
||
| 313 | * |
||
| 314 | * @param Item $item |
||
| 315 | * @param integer $language |
||
| 316 | * @param string $indexConfigurationName |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName) |
||
| 328 | 1 | ||
| 329 | /** |
||
| 330 | * Converts an item array (record) to a Solr document by mapping the |
||
| 331 | * record's fields onto Solr document fields as configured in TypoScript. |
||
| 332 | * |
||
| 333 | * @param Item $item An index queue item |
||
| 334 | * @param int $language Language Id |
||
| 335 | * @return Apache_Solr_Document The Solr document converted from the record |
||
| 336 | */ |
||
| 337 | protected function itemToDocument(Item $item, $language = 0) |
||
| 350 | 19 | ||
| 351 | /** |
||
| 352 | * Creates a Solr document with the basic / core fields set already. |
||
| 353 | * |
||
| 354 | * @param Item $item The item to index |
||
| 355 | * @param array $itemRecord The record to use to build the base document |
||
| 356 | * @return Apache_Solr_Document A basic Solr document |
||
| 357 | */ |
||
| 358 | protected function getBaseDocument(Item $item, array $itemRecord) |
||
| 365 | 19 | ||
| 366 | /** |
||
| 367 | * Generates an Access Rootline for an item. |
||
| 368 | * |
||
| 369 | 19 | * @param Item $item Index Queue item to index. |
|
| 370 | 19 | * @return string The Access Rootline for the item |
|
| 371 | 19 | */ |
|
| 372 | protected function getAccessRootline(Item $item) |
||
| 390 | 19 | ||
| 391 | 19 | /** |
|
| 392 | 19 | * Sends the documents to the field processing service which takes care of |
|
| 393 | * manipulating fields as defined in the field's configuration. |
||
| 394 | * |
||
| 395 | * @param Item $item An index queue item |
||
| 396 | 19 | * @param array $documents An array of Apache_Solr_Document objects to manipulate. |
|
| 397 | 19 | * @return array Array of manipulated Apache_Solr_Document objects. |
|
| 398 | 19 | */ |
|
| 399 | protected function processDocuments(Item $item, array $documents) |
||
| 413 | 19 | ||
| 414 | /** |
||
| 415 | 19 | * Allows third party extensions to provide additional documents which |
|
| 416 | 19 | * should be indexed for the current item. |
|
| 417 | * |
||
| 418 | * @param Item $item The item currently being indexed. |
||
| 419 | * @param int $language The language uid currently being indexed. |
||
| 420 | 19 | * @param Apache_Solr_Document $itemDocument The document representing the item for the given language. |
|
| 421 | 1 | * @return array An array of additional Apache_Solr_Document objects to index. |
|
| 422 | */ |
||
| 423 | 1 | protected function getAdditionalDocuments(Item $item, $language, Apache_Solr_Document $itemDocument) |
|
| 450 | 19 | ||
| 451 | 19 | /** |
|
| 452 | * Provides a hook to manipulate documents right before they get added to |
||
| 453 | * the Solr index. |
||
| 454 | * |
||
| 455 | 19 | * @param Item $item The item currently being indexed. |
|
| 456 | * @param int $language The language uid of the documents |
||
| 457 | * @param array $documents An array of documents to be indexed |
||
| 458 | * @return array An array of modified documents |
||
| 459 | */ |
||
| 460 | protected function preAddModifyDocuments(Item $item, $language, array $documents) |
||
| 482 | 1 | ||
| 483 | // Initialization |
||
| 484 | 1 | ||
| 485 | 1 | /** |
|
| 486 | 1 | * Gets the Solr connections applicable for an item. |
|
| 487 | * |
||
| 488 | * The connections include the default connection and connections to be used |
||
| 489 | 1 | * for translations of an item. |
|
| 490 | 1 | * |
|
| 491 | 2 | * @param Item $item An index queue item |
|
| 492 | * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections, the array's keys are the sys_language_uid of the language of the connection |
||
| 493 | */ |
||
| 494 | protected function getSolrConnectionsByItem(Item $item) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Retrieves only translation overlays where a solr site is configured. |
||
| 531 | * |
||
| 532 | 19 | * @param int $pageId |
|
| 533 | * @param Site $site |
||
| 534 | * @param int $defaultLanguageUid |
||
| 535 | * @param $siteLanguages |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | protected function getTranslationOverlaysWithConfiguredSite($pageId, Site $site, $defaultLanguageUid, $siteLanguages) |
||
| 550 | 21 | ||
| 551 | 21 | /** |
|
| 552 | 3 | * @param Item $item An index queue item |
|
| 553 | * @param array $rootPage |
||
| 554 | * @param array $siteLanguages |
||
| 555 | * |
||
| 556 | 21 | * @return int |
|
| 557 | * @throws \Apache_Solr_Exception |
||
| 558 | 21 | */ |
|
| 559 | 21 | private function getDefaultLanguageUid(Item $item, array $rootPage, array $siteLanguages) |
|
| 572 | |||
| 573 | /** |
||
| 574 | 21 | * Finds the alternative page language overlay records for a page based on |
|
| 575 | 7 | * the sys_language_mode. |
|
| 576 | * |
||
| 577 | * Possible Language Modes: |
||
| 578 | 21 | * 1) content_fallback --> all languages |
|
| 579 | * 2) strict --> available languages with page overlay |
||
| 580 | * 3) ignore --> available languages with page overlay |
||
| 581 | * 4) unknown mode or blank --> all languages |
||
| 582 | * |
||
| 583 | * @param int $pageId Page ID. |
||
| 584 | * @param string $languageMode |
||
| 585 | * @return array An array of translation overlays (or fake overlays) found for the given page. |
||
| 586 | */ |
||
| 587 | protected function getTranslationOverlaysForPage($pageId, $languageMode) |
||
| 615 | 1 | ||
| 616 | 1 | /** |
|
| 617 | 20 | * Returns an array of system languages. |
|
| 618 | * |
||
| 619 | * @return array |
||
| 620 | */ |
||
| 621 | protected function getSystemLanguages() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Checks for which languages connections have been configured and returns |
||
| 628 | * these connections. |
||
| 629 | * |
||
| 630 | * @param array $translationOverlays An array of translation overlays to check for configured connections. |
||
| 631 | * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections. |
||
| 632 | */ |
||
| 633 | protected function getConnectionsForIndexableLanguages(array $translationOverlays) |
||
| 652 | |||
| 653 | 15 | // Utility methods |
|
| 654 | 15 | ||
| 655 | 15 | // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer |
|
| 656 | 15 | // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface |
|
| 657 | |||
| 658 | 1 | /** |
|
| 659 | 1 | * Enables logging dependent on the configuration of the item's site |
|
| 660 | 1 | * |
|
| 661 | * @param Item $item An item being indexed |
||
| 662 | * @return void |
||
| 663 | */ |
||
| 664 | protected function setLogging(Item $item) |
||
| 671 | |||
| 672 | /** |
||
| 673 | 15 | * Logs the item and what document was created from it |
|
| 674 | * |
||
| 675 | 15 | * @param Item $item The item that is being indexed. |
|
| 676 | * @param array $itemDocuments An array of Solr documents created from the item's data |
||
| 677 | * @param Apache_Solr_Response $response The Solr response for the particular index document |
||
| 678 | */ |
||
| 679 | protected function log(Item $item, array $itemDocuments, Apache_Solr_Response $response) |
||
| 708 | } |
||
| 709 |