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