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 |
||
47 | class Typo3PageIndexer |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * ID of the current page's Solr document. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected static $pageSolrDocumentId = ''; |
||
56 | /** |
||
57 | * The Solr document generated for the current page. |
||
58 | * |
||
59 | * @var \Apache_Solr_Document |
||
60 | */ |
||
61 | protected static $pageSolrDocument = null; |
||
62 | /** |
||
63 | * The mount point parameter used in the Frontend controller. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $mountPointParameter; |
||
68 | /** |
||
69 | * Solr server connection. |
||
70 | * |
||
71 | * @var SolrConnection |
||
72 | */ |
||
73 | protected $solrConnection = null; |
||
74 | /** |
||
75 | * Frontend page object (TSFE). |
||
76 | * |
||
77 | * @var TypoScriptFrontendController |
||
78 | */ |
||
79 | protected $page = null; |
||
80 | /** |
||
81 | * Content extractor to extract content from TYPO3 pages |
||
82 | * |
||
83 | * @var Typo3PageContentExtractor |
||
84 | */ |
||
85 | protected $contentExtractor = null; |
||
86 | /** |
||
87 | * URL to be indexed as the page's URL |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $pageUrl = ''; |
||
92 | /** |
||
93 | * The page's access rootline |
||
94 | * |
||
95 | * @var Rootline |
||
96 | */ |
||
97 | protected $pageAccessRootline = null; |
||
98 | /** |
||
99 | * Documents that have been sent to Solr |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $documentsSentToSolr = []; |
||
104 | |||
105 | /** |
||
106 | * @var TypoScriptConfiguration |
||
107 | */ |
||
108 | protected $configuration; |
||
109 | |||
110 | /** |
||
111 | * @var Item |
||
112 | */ |
||
113 | protected $indexQueueItem; |
||
114 | |||
115 | /** |
||
116 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
117 | */ |
||
118 | protected $logger = null; |
||
119 | |||
120 | /** |
||
121 | * Constructor |
||
122 | * |
||
123 | * @param TypoScriptFrontendController $page The page to index |
||
124 | */ |
||
125 | 62 | public function __construct(TypoScriptFrontendController $page) |
|
155 | |||
156 | /** |
||
157 | * @param Item $indexQueueItem |
||
158 | */ |
||
159 | 10 | public function setIndexQueueItem($indexQueueItem) |
|
163 | |||
164 | /** |
||
165 | * Initializes the Solr server connection. |
||
166 | * |
||
167 | * @throws \Exception when no Solr connection can be established. |
||
168 | */ |
||
169 | 62 | protected function initializeSolrConnection() |
|
183 | |||
184 | /** |
||
185 | * Gets the current page's Solr document ID. |
||
186 | * |
||
187 | * @return string|NULL The page's Solr document ID or NULL in case no document was generated yet. |
||
188 | */ |
||
189 | public static function getPageSolrDocumentId() |
||
193 | |||
194 | /** |
||
195 | * Gets the Solr document generated for the current page. |
||
196 | * |
||
197 | * @return \Apache_Solr_Document|NULL The page's Solr document or NULL if it has not been generated yet. |
||
198 | */ |
||
199 | 10 | public static function getPageSolrDocument() |
|
203 | |||
204 | /** |
||
205 | * Allows to provide a Solr server connection other than the one |
||
206 | * initialized by the constructor. |
||
207 | * |
||
208 | * @param SolrConnection $solrConnection Solr connection |
||
209 | * @throws \Exception if the Solr server cannot be reached |
||
210 | */ |
||
211 | 10 | public function setSolrConnection(SolrConnection $solrConnection) |
|
222 | |||
223 | /** |
||
224 | * Indexes a page. |
||
225 | * |
||
226 | * @return bool TRUE after successfully indexing the page, FALSE on error |
||
227 | * @throws \UnexpectedValueException if a page document post processor fails to implement interface ApacheSolrForTypo3\Solr\PageDocumentPostProcessor |
||
228 | */ |
||
229 | 62 | public function indexPage() |
|
257 | |||
258 | /** |
||
259 | * Applies the configured post processors (indexPagePostProcessPageDocument) |
||
260 | * |
||
261 | * @param \Apache_Solr_Document $pageDocument |
||
262 | */ |
||
263 | 62 | protected function applyIndexPagePostProcessors($pageDocument) |
|
278 | |||
279 | /** |
||
280 | * Builds the Solr document for the current page. |
||
281 | * |
||
282 | * @return \Apache_Solr_Document A document representing the page |
||
283 | */ |
||
284 | 62 | protected function getPageDocument() |
|
294 | |||
295 | |||
296 | // Logging |
||
297 | // TODO replace by a central logger |
||
298 | |||
299 | /** |
||
300 | * Gets the mount point parameter that is used in the Frontend controller. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getMountPointParameter() |
||
308 | |||
309 | // Misc |
||
310 | |||
311 | /** |
||
312 | * Sets the mount point parameter that is used in the Frontend controller. |
||
313 | * |
||
314 | * @param string $mountPointParameter |
||
315 | */ |
||
316 | 10 | public function setMountPointParameter($mountPointParameter) |
|
320 | |||
321 | /** |
||
322 | * Allows third party extensions to replace or modify the page document |
||
323 | * created by this indexer. |
||
324 | * |
||
325 | * @param \Apache_Solr_Document $pageDocument The page document created by this indexer. |
||
326 | * @return \Apache_Solr_Document An Apache Solr document representing the currently indexed page |
||
327 | */ |
||
328 | 62 | protected function substitutePageDocument(\Apache_Solr_Document $pageDocument) |
|
357 | |||
358 | /** |
||
359 | * Retrieves the indexConfigurationName from the related queueItem, or falls back to pages when no queue item set. |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 10 | protected function getIndexConfigurationNameForCurrentPage() |
|
367 | |||
368 | /** |
||
369 | * Allows third party extensions to provide additional documents which |
||
370 | * should be indexed for the current page. |
||
371 | * |
||
372 | * @param \Apache_Solr_Document $pageDocument The main document representing this page. |
||
373 | * @param \Apache_Solr_Document[] $existingDocuments An array of documents already created for this page. |
||
374 | * @return array An array of additional \Apache_Solr_Document objects to index |
||
375 | */ |
||
376 | 62 | protected function getAdditionalDocuments(\Apache_Solr_Document $pageDocument, array $existingDocuments) |
|
400 | |||
401 | /** |
||
402 | * Sends the given documents to the field processing service which takes |
||
403 | * care of manipulating fields as defined in the field's configuration. |
||
404 | * |
||
405 | * @param array $documents An array of documents to manipulate |
||
406 | */ |
||
407 | 62 | protected function processDocuments(array $documents) |
|
415 | |||
416 | /** |
||
417 | * Adds the collected documents to the Solr index. |
||
418 | * |
||
419 | * @param array $documents An array of \Apache_Solr_Document objects. |
||
420 | * @return bool TRUE if documents were added successfully, FALSE otherwise |
||
421 | */ |
||
422 | 62 | protected function addDocumentsToSolrIndex(array $documents) |
|
468 | |||
469 | /** |
||
470 | * Gets the current page's URL. |
||
471 | * |
||
472 | * @return string URL of the current page. |
||
473 | */ |
||
474 | public function getPageUrl() |
||
478 | |||
479 | /** |
||
480 | * Sets the URL to use for the page document. |
||
481 | * |
||
482 | * @param string $url The page's URL. |
||
483 | */ |
||
484 | 10 | public function setPageUrl($url) |
|
488 | |||
489 | /** |
||
490 | * Gets the page's access rootline. |
||
491 | * |
||
492 | * @return Rootline The page's access rootline |
||
493 | */ |
||
494 | public function getPageAccessRootline() |
||
498 | |||
499 | /** |
||
500 | * Sets the page's access rootline. |
||
501 | * |
||
502 | * @param Rootline $accessRootline The page's access rootline |
||
503 | */ |
||
504 | 23 | public function setPageAccessRootline(Rootline $accessRootline) |
|
508 | |||
509 | /** |
||
510 | * Gets the documents that have been sent to Solr |
||
511 | * |
||
512 | * @return array An array of \Apache_Solr_Document objects |
||
513 | */ |
||
514 | public function getDocumentsSentToSolr() |
||
518 | } |
||
519 |