Complex classes like Queue 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 Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Queue |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var RootPageResolver |
||
| 49 | */ |
||
| 50 | protected $rootPageResolver; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ConfigurationAwareRecordService |
||
| 54 | */ |
||
| 55 | protected $recordService; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 59 | */ |
||
| 60 | protected $logger = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var QueueItemRepository |
||
| 64 | */ |
||
| 65 | protected $queueItemRepository; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var QueueStatisticsRepository |
||
| 69 | */ |
||
| 70 | protected $queueStatisticsRepository; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Queue constructor. |
||
| 74 | * @param RootPageResolver|null $rootPageResolver |
||
| 75 | * @param ConfigurationAwareRecordService|null $recordService |
||
| 76 | * @param QueueItemRepository|null $queueItemRepository |
||
| 77 | * @param QueueStatisticsRepository|null $queueStatisticsRepository |
||
| 78 | */ |
||
| 79 | 107 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null, QueueItemRepository $queueItemRepository = null, QueueStatisticsRepository $queueStatisticsRepository = null) |
|
| 87 | |||
| 88 | // FIXME some of the methods should be renamed to plural forms |
||
| 89 | // FIXME singular form methods should deal with exactly one item only |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the timestamp of the last indexing run. |
||
| 93 | * |
||
| 94 | * @param int $rootPageId The root page uid for which to get |
||
| 95 | * the last indexed item id |
||
| 96 | * @return int Timestamp of last index run. |
||
| 97 | */ |
||
| 98 | 2 | public function getLastIndexTime($rootPageId) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the uid of the last indexed item in the queue |
||
| 113 | * |
||
| 114 | * @param int $rootPageId The root page uid for which to get |
||
| 115 | * the last indexed item id |
||
| 116 | * @return int The last indexed item's ID. |
||
| 117 | */ |
||
| 118 | 3 | public function getLastIndexedItemId($rootPageId) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
| 132 | * complete way to force reindexing, or to build the Index Queue for the |
||
| 133 | * first time. The Index Queue initialization is site-specific. |
||
| 134 | * |
||
| 135 | * @param Site $site The site to initialize |
||
| 136 | * @param string $indexingConfigurationName Name of a specific |
||
| 137 | * indexing configuration |
||
| 138 | * @return array An array of booleans, each representing whether the |
||
| 139 | * initialization for an indexing configuration was successful |
||
| 140 | */ |
||
| 141 | 6 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Initializes the Index Queue for a specific indexing configuration. |
||
| 185 | * |
||
| 186 | * @param Site $site The site to initialize |
||
| 187 | * @param string $indexingConfigurationName name of a specific |
||
| 188 | * indexing configuration |
||
| 189 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
| 190 | */ |
||
| 191 | 6 | protected function initializeIndexingConfiguration(Site $site, $indexingConfigurationName) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Marks an item as needing (re)indexing. |
||
| 215 | * |
||
| 216 | * Like with Solr itself, there's no add method, just a simple update method |
||
| 217 | * that handles the adds, too. |
||
| 218 | * |
||
| 219 | * The method creates or updates the index queue items for all related rootPageIds. |
||
| 220 | * |
||
| 221 | * @param string $itemType The item's type, usually a table name. |
||
| 222 | * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types. |
||
| 223 | * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used. |
||
| 224 | * @return int Number of updated/created items |
||
| 225 | 59 | */ |
|
| 226 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
||
| 233 | |||
| 234 | 58 | /** |
|
| 235 | 58 | * Updates or add's the item for all relevant root pages. |
|
| 236 | 58 | * |
|
| 237 | 58 | * @param string $itemType The item's type, usually a table name. |
|
| 238 | * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types. |
||
| 239 | 11 | * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used. |
|
| 240 | 11 | * @return int |
|
| 241 | */ |
||
| 242 | protected function updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime): int |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Executes the updateItem post processing hook. |
||
| 272 | * |
||
| 273 | * @param string $itemType |
||
| 274 | * @param int $itemUid |
||
| 275 | * @param int $updateCount |
||
| 276 | * @param int $forcedChangeTime |
||
| 277 | * @return int |
||
| 278 | */ |
||
| 279 | protected function postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime = 0) |
||
| 292 | 1 | ||
| 293 | /** |
||
| 294 | * @param string $classReference |
||
| 295 | 51 | * @return object |
|
| 296 | */ |
||
| 297 | 51 | protected function getHookImplementation($classReference) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Finds indexing errors for the current site |
||
| 304 | * |
||
| 305 | * @param Site $site |
||
| 306 | * @return array Error items for the current site's Index Queue |
||
| 307 | */ |
||
| 308 | public function getErrorsBySite(Site $site) |
||
| 312 | 52 | ||
| 313 | 52 | /** |
|
| 314 | * Resets all the errors for all index queue items. |
||
| 315 | 52 | * |
|
| 316 | 52 | * @return mixed |
|
| 317 | 52 | */ |
|
| 318 | 52 | public function resetAllErrors() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Adds an item to the index queue. |
||
| 325 | * |
||
| 326 | * Not meant for public use. |
||
| 327 | * |
||
| 328 | * @param string $itemType The item's type, usually a table name. |
||
| 329 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 330 | * different value for non-database-record types. |
||
| 331 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 332 | * Optional, overwrites existing / determined configuration. |
||
| 333 | * @param $rootPageId |
||
| 334 | * @return int |
||
| 335 | */ |
||
| 336 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
||
| 353 | |||
| 354 | /** |
||
| 355 | 57 | * Get record to be added in addNewItem |
|
| 356 | 57 | * |
|
| 357 | * @param string $itemType The item's type, usually a table name. |
||
| 358 | 57 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
| 359 | 57 | * different value for non-database-record types. |
|
| 360 | * @param string $additionalRecordFields for sql-query |
||
| 361 | * |
||
| 362 | 57 | * @return array|NULL |
|
| 363 | 35 | */ |
|
| 364 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
||
| 377 | 57 | ||
| 378 | 57 | /** |
|
| 379 | * Determines the time for when an item should be indexed. This timestamp |
||
| 380 | * is then stored in the changed column in the Index Queue. |
||
| 381 | 57 | * |
|
| 382 | * The changed timestamp usually is now - time(). For records which are set |
||
| 383 | * to published at a later time, this timestamp is the start time. So if a |
||
| 384 | * future start time has been set, that will be used to delay indexing |
||
| 385 | * of an item. |
||
| 386 | * |
||
| 387 | * @param string $itemType The item's table name. |
||
| 388 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 389 | * different value for non-database-record types. |
||
| 390 | 35 | * @return int Timestamp of the item's changed time or future start time |
|
| 391 | */ |
||
| 392 | 35 | protected function getItemChangedTime($itemType, $itemUid) |
|
| 437 | |||
| 438 | 3 | /** |
|
| 439 | * Gets the most recent changed time of a page's content elements |
||
| 440 | * |
||
| 441 | * @param array $page Partial page record |
||
| 442 | * @return int Timestamp of the most recent content element change |
||
| 443 | */ |
||
| 444 | protected function getPageItemChangedTime(array $page) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Checks whether the Index Queue contains a specific item. |
||
| 455 | * |
||
| 456 | * @param string $itemType The item's type, usually a table name. |
||
| 457 | 1 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
| 458 | * different value for non-database-record types. |
||
| 459 | 1 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
|
| 460 | 1 | */ |
|
| 461 | public function containsItem($itemType, $itemUid) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Checks whether the Index Queue contains a specific item. |
||
| 468 | * |
||
| 469 | * @param string $itemType The item's type, usually a table name. |
||
| 470 | 6 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
| 471 | * different value for non-database-record types. |
||
| 472 | 6 | * @param integer $rootPageId |
|
| 473 | 6 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
|
| 474 | */ |
||
| 475 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
||
| 479 | 1 | ||
| 480 | /** |
||
| 481 | 1 | * Checks whether the Index Queue contains a specific item that has been |
|
| 482 | 1 | * marked as indexed. |
|
| 483 | * |
||
| 484 | * @param string $itemType The item's type, usually a table name. |
||
| 485 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 486 | * different value for non-database-record types. |
||
| 487 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 488 | * indexed, FALSE otherwise |
||
| 489 | */ |
||
| 490 | 23 | public function containsIndexedItem($itemType, $itemUid) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Removes an item from the Index Queue. |
||
| 497 | * |
||
| 498 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 499 | * @param int $itemUid The uid of the item to remove |
||
| 500 | */ |
||
| 501 | public function deleteItem($itemType, $itemUid) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Removes all items of a certain type from the Index Queue. |
||
| 508 | * |
||
| 509 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 510 | */ |
||
| 511 | public function deleteItemsByType($itemType) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 518 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 519 | * |
||
| 520 | * @param Site $site The site to remove items for. |
||
| 521 | * @param string $indexingConfigurationName Name of a specific indexing |
||
| 522 | 69 | * configuration |
|
| 523 | */ |
||
| 524 | 69 | public function deleteItemsBySite(Site $site, $indexingConfigurationName = '') |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Removes all items from the Index Queue. |
||
| 531 | * |
||
| 532 | */ |
||
| 533 | public function deleteAllItems() |
||
| 537 | |||
| 538 | 6 | /** |
|
| 539 | * Gets a single Index Queue item by its uid. |
||
| 540 | * |
||
| 541 | * @param int $itemId Index Queue item uid |
||
| 542 | * @return Item The request Index Queue item or NULL if no item with $itemId was found |
||
| 543 | */ |
||
| 544 | public function getItem($itemId) |
||
| 548 | 7 | ||
| 549 | /** |
||
| 550 | 7 | * Gets Index Queue items by type and uid. |
|
| 551 | * |
||
| 552 | * @param string $itemType item type, usually the table name |
||
| 553 | * @param int $itemUid item uid |
||
| 554 | * @return Item[] An array of items matching $itemType and $itemUid |
||
| 555 | */ |
||
| 556 | public function getItems($itemType, $itemUid) |
||
| 560 | 6 | ||
| 561 | /** |
||
| 562 | 6 | * Returns all items in the queue. |
|
| 563 | 6 | * |
|
| 564 | * @return Item[] An array of items |
||
| 565 | */ |
||
| 566 | public function getAllItems() |
||
| 570 | 6 | ||
| 571 | /** |
||
| 572 | 6 | * Returns the number of items for all queues. |
|
| 573 | 6 | * |
|
| 574 | * @return int |
||
| 575 | */ |
||
| 576 | public function getAllItemsCount() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Extracts the number of pending, indexed and erroneous items from the |
||
| 583 | * Index Queue. |
||
| 584 | * |
||
| 585 | * @param Site $site |
||
| 586 | * @param string $indexingConfigurationName |
||
| 587 | * |
||
| 588 | * @return QueueStatistic |
||
| 589 | */ |
||
| 590 | public function getStatisticsBySite(Site $site, $indexingConfigurationName = '') |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Gets $limit number of items to index for a particular $site. |
||
| 597 | * |
||
| 598 | * @param Site $site TYPO3 site |
||
| 599 | * @param int $limit Number of items to get from the queue |
||
| 600 | * @return Item[] Items to index to the given solr server |
||
| 601 | */ |
||
| 602 | public function getItemsToIndex(Site $site, $limit = 50) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 609 | * next run. |
||
| 610 | * |
||
| 611 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
| 612 | * @param string $errorMessage Error message |
||
| 613 | */ |
||
| 614 | public function markItemAsFailed($item, $errorMessage = '') |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Sets the timestamp of when an item last has been indexed. |
||
| 621 | * |
||
| 622 | * @param Item $item |
||
| 623 | */ |
||
| 624 | public function updateIndexTimeByItem(Item $item) |
||
| 628 | } |
||
| 629 |