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 | 16 | 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 | 11 | public function index(Item $item) |
|
119 | { |
||
120 | 11 | $indexed = true; |
|
121 | |||
122 | 11 | $this->type = $item->getType(); |
|
123 | 11 | $this->setLogging($item); |
|
124 | |||
125 | 11 | $solrConnections = $this->getSolrConnectionsByItem($item); |
|
126 | |||
127 | 11 | foreach ($solrConnections as $systemLanguageUid => $solrConnection) { |
|
128 | 11 | $this->solr = $solrConnection; |
|
129 | |||
130 | 11 | 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 | 11 | 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 | 11 | 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 | 11 | 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 | 11 | 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 | 11 | */ |
|
296 | protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName) |
||
305 | |||
306 | 11 | /** |
|
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 | protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName) |
||
323 | |||
324 | /** |
||
325 | 11 | * Converts an item array (record) to a Solr document by mapping the |
|
326 | 11 | * record's fields onto Solr document fields as configured in TypoScript. |
|
327 | 11 | * |
|
328 | 11 | * @param Item $item An index queue item |
|
329 | * @param int $language Language Id |
||
330 | 11 | * @return Apache_Solr_Document The Solr document converted from the record |
|
331 | 11 | */ |
|
332 | protected function itemToDocument(Item $item, $language = 0) |
||
345 | |||
346 | 11 | /** |
|
347 | 11 | * Creates a Solr document with the basic / core fields set already. |
|
348 | 11 | * |
|
349 | * @param Item $item The item to index |
||
350 | 11 | * @param array $itemRecord The record to use to build the base document |
|
351 | 11 | * @return Apache_Solr_Document A basic Solr document |
|
352 | 11 | */ |
|
353 | protected function getBaseDocument(Item $item, array $itemRecord) |
||
399 | |||
400 | 11 | /** |
|
401 | * Generates an Access Rootline for an item. |
||
402 | * |
||
403 | 11 | * @param Item $item Index Queue item to index. |
|
404 | 11 | * @return string The Access Rootline for the item |
|
405 | */ |
||
406 | protected function getAccessRootline(Item $item) |
||
424 | |||
425 | /** |
||
426 | * Sends the documents to the field processing service which takes care of |
||
427 | 15 | * 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 | 15 | */ |
|
433 | protected function processDocuments(Item $item, array $documents) |
||
450 | 1 | ||
451 | 1 | /** |
|
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 | 12 | * @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 | protected function getAdditionalDocuments( |
||
491 | |||
492 | 11 | /** |
|
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 | protected function preAddModifyDocuments( |
||
527 | 1 | ||
528 | // Initialization |
||
529 | |||
530 | /** |
||
531 | * Gets the Solr connections applicable for an item. |
||
532 | * |
||
533 | 12 | * The connections include the default connection and connections to be used |
|
534 | 12 | * for translations of an item. |
|
535 | * |
||
536 | 12 | * @param Item $item An index queue item |
|
537 | 12 | * @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 | 1 | */ |
|
539 | protected function getSolrConnectionsByItem(Item $item) |
||
576 | |||
577 | /** |
||
578 | 11 | * Finds the alternative page language overlay records for a page based on |
|
579 | * the sys_language_mode. |
||
580 | 11 | * |
|
581 | 11 | * Possible Language Modes: |
|
582 | 11 | * 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 | 12 | protected function getTranslationOverlaysForPage($pageId, $languageMode) |
|
626 | |||
627 | /** |
||
628 | * Returns an array of system languages. |
||
629 | * |
||
630 | 12 | * @return array |
|
631 | */ |
||
632 | 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 | 12 | protected function getConnectionsForIndexableLanguages( |
|
665 | 11 | ||
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 | 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 | protected function log( |
||
733 | } |
||
734 |