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 | 48 | public function __construct(TypoScriptFrontendController $page) |
|
| 125 | { |
||
| 126 | 48 | $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
| 127 | |||
| 128 | 48 | $this->page = $page; |
|
| 129 | 48 | $this->pageUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'); |
|
| 130 | 48 | $this->configuration = Util::getSolrConfiguration(); |
|
| 131 | |||
| 132 | try { |
||
| 133 | 48 | $this->initializeSolrConnection(); |
|
| 134 | 3 | } catch (\Exception $e) { |
|
| 135 | 3 | $this->logger->log( |
|
| 136 | 3 | SolrLogManager::ERROR, |
|
| 137 | 3 | $e->getMessage() . ' Error code: ' . $e->getCode() |
|
| 138 | ); |
||
| 139 | |||
| 140 | // TODO extract to a class "ExceptionLogger" |
||
| 141 | 3 | if ($this->configuration->getLoggingExceptions()) { |
|
| 142 | 3 | $this->logger->log( |
|
| 143 | 3 | SolrLogManager::ERROR, |
|
| 144 | 3 | 'Exception while trying to index a page', |
|
| 145 | [ |
||
| 146 | 3 | $e->__toString() |
|
| 147 | ] |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | 48 | $this->pageAccessRootline = GeneralUtility::makeInstance(Rootline::class, ''); |
|
| 153 | 48 | } |
|
| 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 | 48 | protected function initializeSolrConnection() |
|
| 169 | { |
||
| 170 | 48 | $solr = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->page->id, $this->page->sys_language_uid); |
|
| 171 | |||
| 172 | // do not continue if no server is available |
||
| 173 | 46 | if (!$solr->ping()) { |
|
| 174 | 1 | throw new \Exception( |
|
| 175 | 1 | 'No Solr instance available while trying to index a page.', |
|
| 176 | 1 | 1234790825 |
|
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | 45 | $this->solrConnection = $solr; |
|
| 181 | 45 | } |
|
| 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 | 48 | public function indexPage() |
|
| 229 | { |
||
| 230 | 48 | $pageIndexed = false; |
|
| 231 | 48 | $documents = []; // this will become useful as soon as when starting to index individual records instead of whole pages |
|
| 232 | |||
| 233 | 48 | if (is_null($this->solrConnection)) { |
|
| 234 | // intended early return as it doesn't make sense to continue |
||
| 235 | // and waste processing time if the solr server isn't available |
||
| 236 | // anyways |
||
| 237 | // FIXME use an exception |
||
| 238 | 1 | return $pageIndexed; |
|
| 239 | } |
||
| 240 | |||
| 241 | 47 | $pageDocument = $this->getPageDocument(); |
|
| 242 | 47 | $pageDocument = $this->substitutePageDocument($pageDocument); |
|
| 243 | |||
| 244 | 47 | $this->applyIndexPagePostProcessors($pageDocument); |
|
| 245 | |||
| 246 | 47 | self::$pageSolrDocument = $pageDocument; |
|
| 247 | 47 | $documents[] = $pageDocument; |
|
| 248 | 47 | $documents = $this->getAdditionalDocuments($pageDocument, $documents); |
|
| 249 | 47 | $this->processDocuments($documents); |
|
| 250 | |||
| 251 | 47 | $pageIndexed = $this->addDocumentsToSolrIndex($documents); |
|
| 252 | 47 | $this->documentsSentToSolr = $documents; |
|
| 253 | |||
| 254 | 47 | return $pageIndexed; |
|
| 255 | } |
||
| 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 | 21 | 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 |