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 |
||
44 | class Typo3PageIndexer |
||
45 | { |
||
46 | |||
47 | /** |
||
48 | * ID of the current page's Solr document. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected static $pageSolrDocumentId = ''; |
||
53 | /** |
||
54 | * The Solr document generated for the current page. |
||
55 | * |
||
56 | * @var \Apache_Solr_Document |
||
57 | */ |
||
58 | protected static $pageSolrDocument = null; |
||
59 | /** |
||
60 | * The mount point parameter used in the Frontend controller. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $mountPointParameter; |
||
65 | /** |
||
66 | * Solr server connection. |
||
67 | * |
||
68 | * @var SolrService |
||
69 | */ |
||
70 | protected $solrConnection = null; |
||
71 | /** |
||
72 | * Frontend page object (TSFE). |
||
73 | * |
||
74 | * @var TypoScriptFrontendController |
||
75 | */ |
||
76 | protected $page = null; |
||
77 | /** |
||
78 | * Content extractor to extract content from TYPO3 pages |
||
79 | * |
||
80 | * @var Typo3PageContentExtractor |
||
81 | */ |
||
82 | protected $contentExtractor = null; |
||
83 | /** |
||
84 | * URL to be indexed as the page's URL |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $pageUrl = ''; |
||
89 | /** |
||
90 | * The page's access rootline |
||
91 | * |
||
92 | * @var Rootline |
||
93 | */ |
||
94 | protected $pageAccessRootline = null; |
||
95 | /** |
||
96 | * Documents that have been sent to Solr |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $documentsSentToSolr = []; |
||
101 | |||
102 | /** |
||
103 | * @var TypoScriptConfiguration |
||
104 | */ |
||
105 | protected $configuration; |
||
106 | |||
107 | /** |
||
108 | * @var Item |
||
109 | */ |
||
110 | protected $indexQueueItem; |
||
111 | |||
112 | /** |
||
113 | * @var IdBuilder |
||
114 | */ |
||
115 | protected $variantIdBuilder; |
||
116 | 41 | ||
117 | /** |
||
118 | 41 | * Constructor |
|
119 | 41 | * |
|
120 | 41 | * @param TypoScriptFrontendController $page The page to index |
|
121 | * @param IdBuilder $variantIdBuilder |
||
122 | */ |
||
123 | 41 | public function __construct(TypoScriptFrontendController $page, IdBuilder $variantIdBuilder = null) |
|
144 | |||
145 | /** |
||
146 | * @param Item $indexQueueItem |
||
147 | */ |
||
148 | public function setIndexQueueItem($indexQueueItem) |
||
152 | |||
153 | 41 | ||
154 | /** |
||
155 | * Initializes the Solr server connection. |
||
156 | 41 | * |
|
157 | * @throws \Exception when no Solr connection can be established. |
||
158 | */ |
||
159 | protected function initializeSolrConnection() |
||
173 | |||
174 | 41 | /** |
|
175 | * Logs messages to devlog and TS log (admin panel) |
||
176 | 41 | * |
|
177 | 41 | * @param string $message Message to set |
|
178 | * @param int $errorNum Error number |
||
179 | * @param array $data Additional data to log |
||
180 | 41 | * @return void |
|
181 | */ |
||
182 | protected function log($message, $errorNum = 0, array $data = []) |
||
199 | |||
200 | /** |
||
201 | * Gets the current page's Solr document ID. |
||
202 | * |
||
203 | * @return string|NULL The page's Solr document ID or NULL in case no document was generated yet. |
||
204 | */ |
||
205 | public static function getPageSolrDocumentId() |
||
209 | 5 | ||
210 | /** |
||
211 | * Gets the Solr document generated for the current page. |
||
212 | * |
||
213 | * @return \Apache_Solr_Document|NULL The page's Solr document or NULL if it has not been generated yet. |
||
214 | */ |
||
215 | public static function getPageSolrDocument() |
||
219 | 5 | ||
220 | /** |
||
221 | 5 | * Allows to provide a Solr server connection other than the one |
|
222 | * initialized by the constructor. |
||
223 | * |
||
224 | * @param SolrService $solrConnection Solr connection |
||
225 | * @throws \Exception if the Solr server cannot be reached |
||
226 | */ |
||
227 | public function setSolrConnection(SolrService $solrConnection) |
||
238 | |||
239 | 41 | /** |
|
240 | 41 | * Indexes a page. |
|
241 | * |
||
242 | 41 | * @return bool TRUE after successfully indexing the page, FALSE on error |
|
243 | * @throws \UnexpectedValueException if a page document post processor fails to implement interface ApacheSolrForTypo3\Solr\PageDocumentPostProcessor |
||
244 | */ |
||
245 | public function indexPage() |
||
273 | 41 | ||
274 | 40 | /** |
|
275 | * Applies the configured post processors (indexPagePostProcessPageDocument) |
||
276 | * |
||
277 | 1 | * @param \Apache_Solr_Document $pageDocument |
|
278 | 1 | */ |
|
279 | 1 | protected function applyIndexPagePostProcessors($pageDocument) |
|
294 | 41 | ||
295 | /** |
||
296 | 41 | * Builds the Solr document for the current page. |
|
297 | 41 | * |
|
298 | * @return \Apache_Solr_Document A document representing the page |
||
299 | 41 | */ |
|
300 | 41 | protected function getPageDocument() |
|
354 | 41 | ||
355 | 8 | /** |
|
356 | * Adds the access field to the document if needed. |
||
357 | 41 | * |
|
358 | * @param \Apache_Solr_Document $document |
||
359 | */ |
||
360 | protected function addAccessField(\Apache_Solr_Document $document) |
||
367 | |||
368 | 41 | /** |
|
369 | * @param $document |
||
370 | * @param $pageRecord |
||
371 | */ |
||
372 | protected function addEndtimeField(\Apache_Solr_Document $document, $pageRecord) |
||
378 | 41 | ||
379 | 41 | /** |
|
380 | * Adds keywords, multi valued. |
||
381 | * |
||
382 | 41 | * @param \Apache_Solr_Document $document |
|
383 | * @param array $pageRecord |
||
384 | */ |
||
385 | protected function addKeywordsField(\Apache_Solr_Document $document, $pageRecord) |
||
392 | 41 | ||
393 | /** |
||
394 | * Add content from several tags like headers, anchors, ... |
||
395 | 41 | * |
|
396 | * @param \Apache_Solr_Document $document |
||
397 | */ |
||
398 | protected function addTagContentFields(\Apache_Solr_Document $document) |
||
405 | 41 | ||
406 | 41 | /** |
|
407 | 36 | * Builds the content for the rootline field. |
|
408 | * |
||
409 | 41 | * @return string |
|
410 | */ |
||
411 | protected function getRootLineFieldValue() |
||
420 | 41 | ||
421 | 41 | /** |
|
422 | * Gets a comma separated list of frontend user groups to use for the |
||
423 | 41 | * document ID. |
|
424 | 35 | * |
|
425 | * @return string A comma separated list of frontend user groups. |
||
426 | */ |
||
427 | 41 | protected function getDocumentIdGroups() |
|
440 | 41 | ||
441 | // Logging |
||
442 | 41 | // TODO replace by a central logger |
|
443 | |||
444 | /** |
||
445 | * Gets the mount point parameter that is used in the Frontend controller. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getMountPointParameter() |
||
453 | |||
454 | 5 | // Misc |
|
455 | 5 | ||
456 | /** |
||
457 | * Sets the mount point parameter that is used in the Frontend controller. |
||
458 | * |
||
459 | * @param string $mountPointParameter |
||
460 | */ |
||
461 | public function setMountPointParameter($mountPointParameter) |
||
465 | |||
466 | 41 | /** |
|
467 | 36 | * Allows third party extensions to replace or modify the page document |
|
468 | * created by this indexer. |
||
469 | * |
||
470 | 5 | * @param \Apache_Solr_Document $pageDocument The page document created by this indexer. |
|
471 | 5 | * @return \Apache_Solr_Document An Apache Solr document representing the currently indexed page |
|
472 | 5 | */ |
|
473 | protected function substitutePageDocument(\Apache_Solr_Document $pageDocument) |
||
502 | |||
503 | /** |
||
504 | * Retrieves the indexConfigurationName from the related queueItem, or falls back to pages when no queue item set. |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | protected function getIndexConfigurationNameForCurrentPage() |
||
512 | 41 | ||
513 | /** |
||
514 | 41 | * Allows third party extensions to provide additional documents which |
|
515 | * should be indexed for the current page. |
||
516 | 41 | * |
|
517 | 40 | * @param \Apache_Solr_Document $pageDocument The main document representing this page. |
|
518 | * @param \Apache_Solr_Document[] $existingDocuments An array of documents already created for this page. |
||
519 | * @return array An array of additional \Apache_Solr_Document objects to index |
||
520 | 1 | */ |
|
521 | 1 | protected function getAdditionalDocuments(\Apache_Solr_Document $pageDocument, array $existingDocuments) |
|
545 | 41 | ||
546 | 41 | /** |
|
547 | 41 | * Sends the given documents to the field processing service which takes |
|
548 | 41 | * care of manipulating fields as defined in the field's configuration. |
|
549 | * |
||
550 | 41 | * @param array $documents An array of documents to manipulate |
|
551 | */ |
||
552 | protected function processDocuments(array $documents) |
||
560 | 41 | ||
561 | /** |
||
562 | 41 | * Adds the collected documents to the Solr index. |
|
563 | * |
||
564 | * @param array $documents An array of \Apache_Solr_Document objects. |
||
565 | * @return bool TRUE if documents were added successfully, FALSE otherwise |
||
566 | */ |
||
567 | 41 | protected function addDocumentsToSolrIndex(array $documents) |
|
600 | |||
601 | /** |
||
602 | * Gets the current page's URL. |
||
603 | * |
||
604 | * @return string URL of the current page. |
||
605 | */ |
||
606 | public function getPageUrl() |
||
610 | 5 | ||
611 | /** |
||
612 | * Sets the URL to use for the page document. |
||
613 | * |
||
614 | * @param string $url The page's URL. |
||
615 | */ |
||
616 | public function setPageUrl($url) |
||
620 | |||
621 | /** |
||
622 | * Gets the page's access rootline. |
||
623 | * |
||
624 | * @return Rootline The page's access rootline |
||
625 | */ |
||
626 | public function getPageAccessRootline() |
||
630 | 40 | ||
631 | /** |
||
632 | * Sets the page's access rootline. |
||
633 | * |
||
634 | * @param Rootline $accessRootline The page's access rootline |
||
635 | */ |
||
636 | public function setPageAccessRootline(Rootline $accessRootline) |
||
640 | |||
641 | /** |
||
642 | * Gets the documents that have been sent to Solr |
||
643 | * |
||
644 | * @return array An array of \Apache_Solr_Document objects |
||
645 | */ |
||
646 | public function getDocumentsSentToSolr() |
||
650 | } |
||
651 |