Complex classes like IndexService 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 IndexService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class IndexService { |
||
| 47 | |||
| 48 | /** @var IndexesRequest */ |
||
| 49 | private $indexesRequest; |
||
| 50 | |||
| 51 | /** @var ConfigService */ |
||
| 52 | private $configService; |
||
| 53 | |||
| 54 | /** @var ProviderService */ |
||
| 55 | private $providerService; |
||
| 56 | |||
| 57 | /** @var PlatformService */ |
||
| 58 | private $platformService; |
||
| 59 | |||
| 60 | /** @var MiscService */ |
||
| 61 | private $miscService; |
||
| 62 | |||
| 63 | |||
| 64 | /** @var Runner */ |
||
| 65 | private $runner = null; |
||
| 66 | |||
| 67 | /** @var array */ |
||
| 68 | private $queuedDeleteIndex = []; |
||
| 69 | |||
| 70 | /** @var int */ |
||
| 71 | private $currentTotalDocuments = 0; |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * IndexService constructor. |
||
| 76 | * |
||
| 77 | * @param IndexesRequest $indexesRequest |
||
| 78 | * @param ConfigService $configService |
||
| 79 | * @param ProviderService $providerService |
||
| 80 | * @param PlatformService $platformService |
||
| 81 | * @param MiscService $miscService |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 84 | IndexesRequest $indexesRequest, ConfigService $configService, |
||
| 85 | ProviderService $providerService, |
||
| 86 | PlatformService $platformService, MiscService $miscService |
||
| 87 | ) { |
||
| 88 | $this->indexesRequest = $indexesRequest; |
||
| 89 | $this->configService = $configService; |
||
| 90 | $this->providerService = $providerService; |
||
| 91 | $this->platformService = $platformService; |
||
| 92 | $this->miscService = $miscService; |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @param Runner $runner |
||
| 98 | */ |
||
| 99 | public function setRunner(Runner $runner) { |
||
| 100 | $this->runner = $runner; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $action |
||
| 106 | * @param bool $force |
||
| 107 | * |
||
| 108 | * @throws InterruptException |
||
| 109 | * @throws TickDoesNotExistException |
||
| 110 | */ |
||
| 111 | private function updateRunnerAction($action, $force = false) { |
||
| 112 | if ($this->runner === null) { |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->runner->updateAction($action, $force); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $info |
||
| 121 | * @param string $value |
||
| 122 | */ |
||
| 123 | private function updateRunnerInfo($info, $value, $color = '') { |
||
|
|
|||
| 124 | if ($this->runner === null) { |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->runner->setInfo($info, $value, $color); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param array $data |
||
| 133 | */ |
||
| 134 | private function updateRunnerInfoArray($data) { |
||
| 135 | if ($this->runner === null) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->runner->setInfoArray($data); |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param IFullTextSearchPlatform $platform |
||
| 145 | * @param IFullTextSearchProvider $provider |
||
| 146 | * @param string $userId |
||
| 147 | * @param IndexOptions $options |
||
| 148 | * |
||
| 149 | * @throws InterruptException |
||
| 150 | * @throws TickDoesNotExistException |
||
| 151 | * @throws Exception |
||
| 152 | */ |
||
| 153 | public function indexProviderContentFromUser( |
||
| 154 | IFullTextSearchPlatform $platform, IFullTextSearchProvider $provider, $userId, $options |
||
| 155 | ) { |
||
| 156 | $this->updateRunnerAction('generateIndex' . $provider->getName()); |
||
| 157 | $this->updateRunnerInfoArray( |
||
| 158 | [ |
||
| 159 | 'userId' => $userId, |
||
| 160 | 'providerId' => $provider->getId(), |
||
| 161 | 'providerName' => $provider->getName() |
||
| 162 | ] |
||
| 163 | ); |
||
| 164 | |||
| 165 | $documents = $provider->generateIndexableDocuments($userId); |
||
| 166 | $this->currentTotalDocuments = sizeof($documents); |
||
| 167 | |||
| 168 | $this->updateRunnerInfoArray( |
||
| 169 | [ |
||
| 170 | 'documentTotal' => $this->currentTotalDocuments, |
||
| 171 | 'documentCurrent' => 0 |
||
| 172 | ] |
||
| 173 | ); |
||
| 174 | |||
| 175 | //$maxSize = sizeof($documents); |
||
| 176 | |||
| 177 | $toIndex = $this->updateDocumentsWithCurrIndex($provider, $documents, $options); |
||
| 178 | $this->indexDocuments($platform, $provider, $toIndex, $options); |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @param IFullTextSearchProvider $provider |
||
| 184 | * @param IndexDocument[] $documents |
||
| 185 | * @param IndexOptions $options |
||
| 186 | * |
||
| 187 | * @return IndexDocument[] |
||
| 188 | * @throws InterruptException |
||
| 189 | * @throws TickDoesNotExistException |
||
| 190 | */ |
||
| 191 | private function updateDocumentsWithCurrIndex( |
||
| 192 | IFullTextSearchProvider $provider, array $documents, IndexOptions $options |
||
| 193 | ) { |
||
| 194 | |||
| 195 | $currIndex = $this->getProviderIndexFromProvider($provider); |
||
| 196 | $result = []; |
||
| 197 | foreach ($documents as $document) { |
||
| 198 | $this->updateRunnerAction('compareWithCurrentIndex'); |
||
| 199 | |||
| 200 | $index = $currIndex->getIndex($document->getId()); |
||
| 201 | if ($index === null) { |
||
| 202 | $index = new Index($document->getProviderId(), $document->getId()); |
||
| 203 | $index->setStatus(Index::INDEX_FULL); |
||
| 204 | $index->setLastIndex(); |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($options->getOption('errors', '') !== 'ignore' && $index->getErrorCount() > 0) { |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($options->getOption('force', false) === true) { |
||
| 212 | $index->setStatus(Index::INDEX_FULL); |
||
| 213 | } |
||
| 214 | |||
| 215 | $index->resetErrors(); |
||
| 216 | $document->setIndex($index); |
||
| 217 | if ($options->getOption('force', false) === true |
||
| 218 | || !$this->isDocumentUpToDate($provider, $document)) { |
||
| 219 | $result[] = $document; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return $result; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * @param IFullTextSearchProvider $provider |
||
| 229 | * @param IndexDocument $document |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | private function isDocumentUpToDate(IFullTextSearchProvider $provider, IndexDocument $document |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * @param IFullTextSearchProvider $provider |
||
| 251 | * |
||
| 252 | * @return ProviderIndexes |
||
| 253 | */ |
||
| 254 | private function getProviderIndexFromProvider(IFullTextSearchProvider $provider) { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @param IFullTextSearchPlatform $platform |
||
| 263 | * @param IFullTextSearchProvider $provider |
||
| 264 | * @param IndexDocument[] $documents |
||
| 265 | * @param IndexOptions $options |
||
| 266 | * |
||
| 267 | * @throws InterruptException |
||
| 268 | * @throws TickDoesNotExistException |
||
| 269 | * @throws Exception |
||
| 270 | */ |
||
| 271 | private function indexDocuments( |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param IndexDocument $document |
||
| 319 | * |
||
| 320 | * @throws NotIndexableDocumentException |
||
| 321 | */ |
||
| 322 | private function filterDocumentBeforeIndex(IndexDocument $document) { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param IFullTextSearchPlatform $platform |
||
| 338 | * @param IFullTextSearchProvider $provider |
||
| 339 | * @param IndexDocument $document |
||
| 340 | * |
||
| 341 | * @return |
||
| 342 | * @throws Exception |
||
| 343 | */ |
||
| 344 | public function indexDocument( |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param IFullTextSearchPlatform $platform |
||
| 371 | * @param IFullTextSearchProvider $provider |
||
| 372 | * @param Index $index |
||
| 373 | * |
||
| 374 | * @internal param int|string $documentId |
||
| 375 | * @throws Exception |
||
| 376 | */ |
||
| 377 | public function updateDocument( |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @param Index[] $indexes |
||
| 421 | * |
||
| 422 | * @throws DatabaseException |
||
| 423 | */ |
||
| 424 | public function updateIndexes($indexes) { |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * @param Index $index |
||
| 438 | * |
||
| 439 | * @throws Exception |
||
| 440 | */ |
||
| 441 | private function updateIndex(Index $index) { |
||
| 465 | |||
| 466 | |||
| 467 | private function updateIndexError(Index $index) { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $providerId |
||
| 474 | * @param array $documentIds |
||
| 475 | * @param int $status |
||
| 476 | * @param bool $reset |
||
| 477 | * |
||
| 478 | * @throws DatabaseException |
||
| 479 | */ |
||
| 480 | public function updateIndexesStatus($providerId, $documentIds, $status, $reset = false) { |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * @param Index $index |
||
| 503 | */ |
||
| 504 | public function resetErrorFromIndex(Index $index) { |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * |
||
| 513 | */ |
||
| 514 | private function resetErrorFromQueue() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * |
||
| 522 | */ |
||
| 523 | public function resetErrorsAll() { |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * @return ExtendedIndex[] |
||
| 530 | */ |
||
| 531 | public function getErrorIndexes() { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $providerId |
||
| 538 | * @param array $documentId |
||
| 539 | * |
||
| 540 | * @return ExtendedIndex[] |
||
| 541 | * @throws IndexDoesNotExistException |
||
| 542 | */ |
||
| 543 | public function getIndexes($providerId, $documentId) { |
||
| 546 | |||
| 547 | |||
| 548 | /** |
||
| 549 | * @param bool $all |
||
| 550 | * |
||
| 551 | * @return Index[] |
||
| 552 | */ |
||
| 553 | public function getQueuedIndexes($all = false) { |
||
| 556 | |||
| 557 | |||
| 558 | /** |
||
| 559 | * @param string $providerId |
||
| 560 | * |
||
| 561 | * @throws Exception |
||
| 562 | */ |
||
| 563 | public function resetIndex($providerId = '') { |
||
| 586 | |||
| 587 | |||
| 588 | } |