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 | 22 | * |
|
99 | * @param array $options array of indexer options |
||
100 | 22 | * @param PagesRepository|null $pagesRepository |
|
101 | 22 | * @param Builder|null $documentBuilder |
|
102 | 22 | */ |
|
103 | 22 | public function __construct(array $options = [], PagesRepository $pagesRepository = null, Builder $documentBuilder = null) |
|
111 | |||
112 | 16 | /** |
|
113 | * Indexes an item from the indexing queue. |
||
114 | 16 | * |
|
115 | * @param Item $item An index queue item |
||
116 | 16 | * @return bool returns true when indexed, false when not |
|
117 | 16 | */ |
|
118 | public function index(Item $item) |
||
145 | |||
146 | /** |
||
147 | 16 | * Creates a single Solr Document for an item in a specific language. |
|
148 | * |
||
149 | 16 | * @param Item $item An index queue item to index. |
|
150 | 16 | * @param int $language The language to use. |
|
151 | * @return bool TRUE if item was indexed successfully, FALSE on failure |
||
152 | 16 | */ |
|
153 | 16 | 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 | 16 | * types. |
|
190 | * |
||
191 | 16 | * @param Item $item The item to be indexed |
|
192 | * @param int $language Language Id (sys_language.uid) |
||
193 | 16 | * @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 | 16 | */ |
|
195 | protected function getFullItemRecord(Item $item, $language = 0) |
||
244 | |||
245 | /** |
||
246 | * Returns the overlayed item record. |
||
247 | 16 | * |
|
248 | * @param Item $item |
||
249 | 16 | * @param int $language |
|
250 | * @param string|null $systemLanguageContentOverlay |
||
251 | 16 | * @return array|mixed|null |
|
252 | 5 | */ |
|
253 | 5 | protected function getItemRecordOverlayed(Item $item, $language, $systemLanguageContentOverlay) |
|
269 | |||
270 | /** |
||
271 | * Gets the configuration how to process an item's fields for indexing. |
||
272 | 16 | * |
|
273 | * @param Item $item An index queue item |
||
274 | 16 | * @param int $language Language ID |
|
275 | 16 | * @throws \RuntimeException |
|
276 | 16 | * @return array Configuration array from TypoScript |
|
277 | 1 | */ |
|
278 | 1 | protected function getItemTypeConfiguration(Item $item, $language = 0) |
|
292 | |||
293 | /** |
||
294 | * The method retrieves the field configuration of the items record page id (pid). |
||
295 | 16 | * |
|
296 | * @param Item $item |
||
297 | * @param integer $language |
||
298 | 16 | * @param string $indexConfigurationName |
|
299 | 15 | * @return array |
|
300 | 1 | */ |
|
301 | 1 | 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 | 1 | * |
|
314 | * @param Item $item |
||
315 | 1 | * @param integer $language |
|
316 | 1 | * @param string $indexConfigurationName |
|
317 | * @return array |
||
318 | */ |
||
319 | protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName) |
||
328 | |||
329 | /** |
||
330 | * Converts an item array (record) to a Solr document by mapping the |
||
331 | 16 | * record's fields onto Solr document fields as configured in TypoScript. |
|
332 | * |
||
333 | 16 | * @param Item $item An index queue item |
|
334 | * @param int $language Language Id |
||
335 | 16 | * @return Apache_Solr_Document The Solr document converted from the record |
|
336 | 16 | */ |
|
337 | 16 | protected function itemToDocument(Item $item, $language = 0) |
|
350 | |||
351 | /** |
||
352 | 16 | * Creates a Solr document with the basic / core fields set already. |
|
353 | * |
||
354 | 16 | * @param Item $item The item to index |
|
355 | 16 | * @param array $itemRecord The record to use to build the base document |
|
356 | * @return Apache_Solr_Document A basic Solr document |
||
357 | 16 | */ |
|
358 | protected function getBaseDocument(Item $item, array $itemRecord) |
||
365 | |||
366 | 16 | /** |
|
367 | 16 | * Generates an Access Rootline for an item. |
|
368 | * |
||
369 | * @param Item $item Index Queue item to index. |
||
370 | 16 | * @return string The Access Rootline for the item |
|
371 | 16 | */ |
|
372 | protected function getAccessRootline(Item $item) |
||
390 | 16 | ||
391 | /** |
||
392 | * 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 | 16 | * @param array $documents An array of Apache_Solr_Document objects to manipulate. |
|
397 | * @return array Array of manipulated Apache_Solr_Document objects. |
||
398 | */ |
||
399 | protected function processDocuments(Item $item, array $documents) |
||
413 | 1 | ||
414 | /** |
||
415 | 1 | * Allows third party extensions to provide additional documents which |
|
416 | * should be indexed for the current item. |
||
417 | 1 | * |
|
418 | * @param Item $item The item currently being indexed. |
||
419 | * @param int $language The language uid currently being indexed. |
||
420 | * @param Apache_Solr_Document $itemDocument The document representing the item for the given language. |
||
421 | 16 | * @return array An array of additional Apache_Solr_Document objects to index. |
|
422 | */ |
||
423 | protected function getAdditionalDocuments(Item $item, $language, Apache_Solr_Document $itemDocument) |
||
450 | |||
451 | /** |
||
452 | * Provides a hook to manipulate documents right before they get added to |
||
453 | * the Solr index. |
||
454 | * |
||
455 | * @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 | 20 | */ |
|
460 | protected function preAddModifyDocuments(Item $item, $language, array $documents) |
||
482 | 1 | ||
483 | 2 | // Initialization |
|
484 | |||
485 | /** |
||
486 | * Gets the Solr connections applicable for an item. |
||
487 | * |
||
488 | 17 | * The connections include the default connection and connections to be used |
|
489 | * for translations of an item. |
||
490 | * |
||
491 | * @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 | * @param int $pageId |
||
533 | * @param Site $site |
||
534 | * @param int $defaultLanguageUid |
||
535 | * @param $siteLanguages |
||
536 | * @return array |
||
537 | */ |
||
538 | 18 | protected function getTranslationOverlaysWithConfiguredSite($pageId, Site $site, $defaultLanguageUid, $siteLanguages) |
|
550 | 18 | ||
551 | 18 | /** |
|
552 | 18 | * @param Item $item An index queue item |
|
553 | 18 | * @param array $rootPage |
|
554 | * @param array $siteLanguages |
||
555 | * |
||
556 | 18 | * @return int |
|
557 | 18 | * @throws \Apache_Solr_Exception |
|
558 | */ |
||
559 | 18 | private function getDefaultLanguageUid(Item $item, array $rootPage, array $siteLanguages) |
|
572 | |||
573 | /** |
||
574 | * Finds the alternative page language overlay records for a page based on |
||
575 | * the sys_language_mode. |
||
576 | * |
||
577 | * Possible Language Modes: |
||
578 | * 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 | 18 | * |
|
583 | * @param int $pageId Page ID. |
||
584 | 18 | * @param string $languageMode |
|
585 | * @return array An array of translation overlays (or fake overlays) found for the given page. |
||
586 | 18 | */ |
|
587 | 6 | protected function getTranslationOverlaysForPage($pageId, $languageMode) |
|
615 | |||
616 | /** |
||
617 | * 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 | 18 | * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections. |
|
632 | */ |
||
633 | 18 | protected function getConnectionsForIndexableLanguages(array $translationOverlays) |
|
652 | 13 | ||
653 | 13 | // Utility methods |
|
654 | 13 | ||
655 | // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer |
||
656 | 1 | // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface |
|
657 | 1 | ||
658 | 1 | /** |
|
659 | * Enables logging dependent on the configuration of the item's site |
||
660 | * |
||
661 | * @param Item $item An item being indexed |
||
662 | * @return void |
||
663 | 18 | */ |
|
664 | protected function setLogging(Item $item) |
||
671 | 13 | ||
672 | /** |
||
673 | 13 | * Logs the item and what document was created from it |
|
674 | * |
||
675 | * @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 |