Complex classes like Typo3PageIndexer 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 Typo3PageIndexer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Typo3PageIndexer |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * ID of the current page's Solr document. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected static $pageSolrDocumentId = ''; |
||
52 | /** |
||
53 | * The Solr document generated for the current page. |
||
54 | * |
||
55 | * @var \Apache_Solr_Document |
||
56 | */ |
||
57 | protected static $pageSolrDocument = null; |
||
58 | /** |
||
59 | * The mount point parameter used in the Frontend controller. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $mountPointParameter; |
||
64 | /** |
||
65 | * Solr server connection. |
||
66 | * |
||
67 | * @var SolrService |
||
68 | */ |
||
69 | protected $solrConnection = null; |
||
70 | /** |
||
71 | * Frontend page object (TSFE). |
||
72 | * |
||
73 | * @var TypoScriptFrontendController |
||
74 | */ |
||
75 | protected $page = null; |
||
76 | /** |
||
77 | * Content extractor to extract content from TYPO3 pages |
||
78 | * |
||
79 | * @var Typo3PageContentExtractor |
||
80 | */ |
||
81 | protected $contentExtractor = null; |
||
82 | /** |
||
83 | * URL to be indexed as the page's URL |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $pageUrl = ''; |
||
88 | /** |
||
89 | * The page's access rootline |
||
90 | * |
||
91 | * @var Rootline |
||
92 | */ |
||
93 | protected $pageAccessRootline = null; |
||
94 | /** |
||
95 | * Documents that have been sent to Solr |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $documentsSentToSolr = []; |
||
100 | |||
101 | /** |
||
102 | * @var TypoScriptConfiguration |
||
103 | */ |
||
104 | protected $configuration; |
||
105 | |||
106 | /** |
||
107 | * @var Item |
||
108 | */ |
||
109 | protected $indexQueueItem; |
||
110 | |||
111 | /** |
||
112 | * Constructor |
||
113 | * |
||
114 | * @param TypoScriptFrontendController $page The page to index |
||
115 | */ |
||
116 | 35 | public function __construct(TypoScriptFrontendController $page) |
|
136 | |||
137 | /** |
||
138 | * @param Item $indexQueueItem |
||
139 | */ |
||
140 | 5 | public function setIndexQueueItem($indexQueueItem) |
|
144 | |||
145 | |||
146 | /** |
||
147 | * Initializes the Solr server connection. |
||
148 | * |
||
149 | * @throws \Exception when no Solr connection can be established. |
||
150 | */ |
||
151 | 35 | protected function initializeSolrConnection() |
|
165 | |||
166 | /** |
||
167 | * Logs messages to devlog and TS log (admin panel) |
||
168 | * |
||
169 | * @param string $message Message to set |
||
170 | * @param int $errorNum Error number |
||
171 | * @param array $data Additional data to log |
||
172 | * @return void |
||
173 | */ |
||
174 | 35 | protected function log($message, $errorNum = 0, array $data = []) |
|
191 | |||
192 | /** |
||
193 | * Gets the current page's Solr document ID. |
||
194 | * |
||
195 | * @return string|NULL The page's Solr document ID or NULL in case no document was generated yet. |
||
196 | */ |
||
197 | public static function getPageSolrDocumentId() |
||
201 | |||
202 | /** |
||
203 | * Gets the Solr document generated for the current page. |
||
204 | * |
||
205 | * @return \Apache_Solr_Document|NULL The page's Solr document or NULL if it has not been generated yet. |
||
206 | */ |
||
207 | 5 | public static function getPageSolrDocument() |
|
211 | |||
212 | /** |
||
213 | * Allows to provide a Solr server connection other than the one |
||
214 | * initialized by the constructor. |
||
215 | * |
||
216 | * @param SolrService $solrConnection Solr connection |
||
217 | * @throws \Exception if the Solr server cannot be reached |
||
218 | */ |
||
219 | 5 | public function setSolrConnection(SolrService $solrConnection) |
|
230 | |||
231 | /** |
||
232 | * Indexes a page. |
||
233 | * |
||
234 | * @return bool TRUE after successfully indexing the page, FALSE on error |
||
235 | * @throws \UnexpectedValueException if a page document post processor fails to implement interface ApacheSolrForTypo3\Solr\PageDocumentPostProcessor |
||
236 | */ |
||
237 | 35 | public function indexPage() |
|
265 | |||
266 | /** |
||
267 | * Applies the configured post processors (indexPagePostProcessPageDocument) |
||
268 | * |
||
269 | * @param \Apache_Solr_Document $pageDocument |
||
270 | */ |
||
271 | 35 | protected function applyIndexPagePostProcessors($pageDocument) |
|
286 | |||
287 | /** |
||
288 | * Builds the Solr document for the current page. |
||
289 | * |
||
290 | * @return \Apache_Solr_Document A document representing the page |
||
291 | */ |
||
292 | 35 | protected function getPageDocument() |
|
345 | |||
346 | /** |
||
347 | * Adds the access field to the document if needed. |
||
348 | * |
||
349 | * @param \Apache_Solr_Document $document |
||
350 | */ |
||
351 | 35 | protected function addAccessField(\Apache_Solr_Document $document) |
|
358 | |||
359 | /** |
||
360 | * @param $document |
||
361 | * @param $pageRecord |
||
362 | */ |
||
363 | 35 | protected function addEndtimeField(\Apache_Solr_Document $document, $pageRecord) |
|
369 | |||
370 | /** |
||
371 | * Adds keywords, multi valued. |
||
372 | * |
||
373 | * @param \Apache_Solr_Document $document |
||
374 | * @param array $pageRecord |
||
375 | */ |
||
376 | 35 | protected function addKeywordsField(\Apache_Solr_Document $document, $pageRecord) |
|
383 | |||
384 | /** |
||
385 | * Add content from several tags like headers, anchors, ... |
||
386 | * |
||
387 | * @param \Apache_Solr_Document $document |
||
388 | */ |
||
389 | 35 | protected function addTagContentFields(\Apache_Solr_Document $document) |
|
396 | |||
397 | /** |
||
398 | * Builds the content for the rootline field. |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | 35 | protected function getRootLineFieldValue() |
|
411 | |||
412 | /** |
||
413 | * Gets a comma separated list of frontend user groups to use for the |
||
414 | * document ID. |
||
415 | * |
||
416 | * @return string A comma separated list of frontend user groups. |
||
417 | */ |
||
418 | 35 | protected function getDocumentIdGroups() |
|
431 | |||
432 | // Logging |
||
433 | // TODO replace by a central logger |
||
434 | |||
435 | /** |
||
436 | * Gets the mount point parameter that is used in the Frontend controller. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | 35 | public function getMountPointParameter() |
|
444 | |||
445 | // Misc |
||
446 | |||
447 | /** |
||
448 | * Sets the mount point parameter that is used in the Frontend controller. |
||
449 | * |
||
450 | * @param string $mountPointParameter |
||
451 | */ |
||
452 | 5 | public function setMountPointParameter($mountPointParameter) |
|
456 | |||
457 | /** |
||
458 | * Allows third party extensions to replace or modify the page document |
||
459 | * created by this indexer. |
||
460 | * |
||
461 | * @param \Apache_Solr_Document $pageDocument The page document created by this indexer. |
||
462 | * @return \Apache_Solr_Document An Apache Solr document representing the currently indexed page |
||
463 | */ |
||
464 | 35 | protected function substitutePageDocument(\Apache_Solr_Document $pageDocument) |
|
493 | |||
494 | /** |
||
495 | * Retrieves the indexConfigurationName from the related queueItem, or falls back to pages when no queue item set. |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | 5 | protected function getIndexConfigurationNameForCurrentPage() |
|
503 | |||
504 | /** |
||
505 | * Allows third party extensions to provide additional documents which |
||
506 | * should be indexed for the current page. |
||
507 | * |
||
508 | * @param \Apache_Solr_Document $pageDocument The main document representing this page. |
||
509 | * @param \Apache_Solr_Document[] $existingDocuments An array of documents already created for this page. |
||
510 | * @return array An array of additional \Apache_Solr_Document objects to index |
||
511 | */ |
||
512 | 35 | protected function getAdditionalDocuments(\Apache_Solr_Document $pageDocument, array $existingDocuments) |
|
536 | |||
537 | /** |
||
538 | * Sends the given documents to the field processing service which takes |
||
539 | * care of manipulating fields as defined in the field's configuration. |
||
540 | * |
||
541 | * @param array $documents An array of documents to manipulate |
||
542 | */ |
||
543 | 35 | protected function processDocuments(array $documents) |
|
551 | |||
552 | /** |
||
553 | * Adds the collected documents to the Solr index. |
||
554 | * |
||
555 | * @param array $documents An array of \Apache_Solr_Document objects. |
||
556 | * @return bool TRUE if documents were added successfully, FALSE otherwise |
||
557 | */ |
||
558 | 35 | protected function addDocumentsToSolrIndex(array $documents) |
|
591 | |||
592 | /** |
||
593 | * Gets the current page's URL. |
||
594 | * |
||
595 | * @return string URL of the current page. |
||
596 | */ |
||
597 | public function getPageUrl() |
||
601 | |||
602 | /** |
||
603 | * Sets the URL to use for the page document. |
||
604 | * |
||
605 | * @param string $url The page's URL. |
||
606 | */ |
||
607 | 5 | public function setPageUrl($url) |
|
611 | |||
612 | /** |
||
613 | * Gets the page's access rootline. |
||
614 | * |
||
615 | * @return Rootline The page's access rootline |
||
616 | */ |
||
617 | public function getPageAccessRootline() |
||
621 | |||
622 | /** |
||
623 | * Sets the page's access rootline. |
||
624 | * |
||
625 | * @param Rootline $accessRootline The page's access rootline |
||
626 | */ |
||
627 | 34 | public function setPageAccessRootline(Rootline $accessRootline) |
|
631 | |||
632 | /** |
||
633 | * Gets the documents that have been sent to Solr |
||
634 | * |
||
635 | * @return array An array of \Apache_Solr_Document objects |
||
636 | */ |
||
637 | 5 | public function getDocumentsSentToSolr() |
|
641 | } |
||
642 |