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 |
||
| 44 | class Queue |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var RootPageResolver |
||
| 48 | */ |
||
| 49 | protected $rootPageResolver; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ConfigurationAwareRecordService |
||
| 53 | */ |
||
| 54 | protected $recordService; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 58 | */ |
||
| 59 | protected $logger = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var QueueItemRepository |
||
| 63 | */ |
||
| 64 | protected $queueItemRepository; |
||
| 65 | |||
| 66 | 102 | /** |
|
| 67 | * Queue constructor. |
||
| 68 | 102 | * @param RootPageResolver|null $rootPageResolver |
|
| 69 | 102 | * @param ConfigurationAwareRecordService|null $recordService |
|
| 70 | 102 | * @param QueueItemRepository|null $queueItemRepository |
|
| 71 | 102 | */ |
|
| 72 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null, QueueItemRepository $queueItemRepository = null) |
||
| 79 | |||
| 80 | // FIXME some of the methods should be renamed to plural forms |
||
| 81 | // FIXME singular form methods should deal with exactly one item only |
||
| 82 | |||
| 83 | 2 | /** |
|
| 84 | * Returns the timestamp of the last indexing run. |
||
| 85 | 2 | * |
|
| 86 | * @param int $rootPageId The root page uid for which to get |
||
| 87 | 2 | * the last indexed item id |
|
| 88 | * @return int Timestamp of last index run. |
||
| 89 | 2 | */ |
|
| 90 | 1 | public function getLastIndexTime($rootPageId) |
|
| 102 | |||
| 103 | 3 | /** |
|
| 104 | * Returns the uid of the last indexed item in the queue |
||
| 105 | 3 | * |
|
| 106 | * @param int $rootPageId The root page uid for which to get |
||
| 107 | 3 | * the last indexed item id |
|
| 108 | 3 | * @return int The last indexed item's ID. |
|
| 109 | 2 | */ |
|
| 110 | public function getLastIndexedItemId($rootPageId) |
||
| 121 | 5 | ||
| 122 | /** |
||
| 123 | 5 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
|
| 124 | 5 | * complete way to force reindexing, or to build the Index Queue for the |
|
| 125 | 5 | * first time. The Index Queue initialization is site-specific. |
|
| 126 | 5 | * |
|
| 127 | 5 | * @param Site $site The site to initialize |
|
| 128 | 5 | * @param string $indexingConfigurationName Name of a specific |
|
| 129 | 5 | * indexing configuration |
|
| 130 | * @return array An array of booleans, each representing whether the |
||
| 131 | * initialization for an indexing configuration was successful |
||
| 132 | 5 | */ |
|
| 133 | 3 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Initializes the Index Queue for a specific indexing configuration. |
||
| 177 | * |
||
| 178 | * @param Site $site The site to initialize |
||
| 179 | * @param string $indexingConfigurationName name of a specific |
||
| 180 | * indexing configuration |
||
| 181 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
| 182 | */ |
||
| 183 | protected function initializeIndexingConfiguration(Site $site, $indexingConfigurationName) |
||
| 204 | |||
| 205 | 6 | /** |
|
| 206 | * Marks an item as needing (re)indexing. |
||
| 207 | 6 | * |
|
| 208 | * Like with Solr itself, there's no add method, just a simple update method |
||
| 209 | 6 | * that handles the adds, too. |
|
| 210 | 6 | * |
|
| 211 | * The method creates or updates the index queue items for all related rootPageIds. |
||
| 212 | 6 | * |
|
| 213 | * @param string $itemType The item's type, usually a table name. |
||
| 214 | 6 | * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types. |
|
| 215 | 6 | * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used. |
|
| 216 | 6 | */ |
|
| 217 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
||
| 239 | |||
| 240 | 56 | /** |
|
| 241 | 55 | * Finds indexing errors for the current site |
|
| 242 | 55 | * |
|
| 243 | 55 | * @param Site $site |
|
| 244 | * @return array Error items for the current site's Index Queue |
||
| 245 | */ |
||
| 246 | public function getErrorsBySite(Site $site) |
||
| 250 | 55 | ||
| 251 | /** |
||
| 252 | 11 | * Resets all the errors for all index queue items. |
|
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | 55 | */ |
|
| 256 | public function resetAllErrors() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Adds an item to the index queue. |
||
| 263 | * |
||
| 264 | * Not meant for public use. |
||
| 265 | * |
||
| 266 | * @param string $itemType The item's type, usually a table name. |
||
| 267 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 268 | * different value for non-database-record types. |
||
| 269 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 270 | * Optional, overwrites existing / determined configuration. |
||
| 271 | * @param $rootPageId |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get record to be added in addNewItem |
||
| 294 | * |
||
| 295 | * @param string $itemType The item's type, usually a table name. |
||
| 296 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 297 | * different value for non-database-record types. |
||
| 298 | * @param string $additionalRecordFields for sql-query |
||
| 299 | 11 | * |
|
| 300 | * @return array|NULL |
||
| 301 | */ |
||
| 302 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
||
| 315 | 11 | ||
| 316 | /** |
||
| 317 | * Determines the time for when an item should be indexed. This timestamp |
||
| 318 | * is then stored in the changed column in the Index Queue. |
||
| 319 | * |
||
| 320 | * The changed timestamp usually is now - time(). For records which are set |
||
| 321 | * to published at a later time, this timestamp is the start time. So if a |
||
| 322 | * future start time has been set, that will be used to delay indexing |
||
| 323 | * of an item. |
||
| 324 | * |
||
| 325 | * @param string $itemType The item's table name. |
||
| 326 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 327 | * different value for non-database-record types. |
||
| 328 | * @return int Timestamp of the item's changed time or future start time |
||
| 329 | 49 | */ |
|
| 330 | protected function getItemChangedTime($itemType, $itemUid) |
||
| 375 | |||
| 376 | 49 | /** |
|
| 377 | * Gets the most recent changed time of a page's content elements |
||
| 378 | * |
||
| 379 | * @param array $page Partial page record |
||
| 380 | * @return int Timestamp of the most recent content element change |
||
| 381 | */ |
||
| 382 | protected function getPageItemChangedTime(array $page) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Checks whether the Index Queue contains a specific item. |
||
| 393 | 54 | * |
|
| 394 | * @param string $itemType The item's type, usually a table name. |
||
| 395 | 54 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
| 396 | 54 | * different value for non-database-record types. |
|
| 397 | 54 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
|
| 398 | 54 | */ |
|
| 399 | public function containsItem($itemType, $itemUid) |
||
| 403 | |||
| 404 | 54 | /** |
|
| 405 | * Checks whether the Index Queue contains a specific item. |
||
| 406 | * |
||
| 407 | 35 | * @param string $itemType The item's type, usually a table name. |
|
| 408 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 409 | * different value for non-database-record types. |
||
| 410 | 54 | * @param integer $rootPageId |
|
| 411 | 54 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
|
| 412 | */ |
||
| 413 | 54 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
| 417 | 54 | ||
| 418 | 35 | /** |
|
| 419 | * Checks whether the Index Queue contains a specific item that has been |
||
| 420 | * marked as indexed. |
||
| 421 | 35 | * |
|
| 422 | * @param string $itemType The item's type, usually a table name. |
||
| 423 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 424 | 54 | * different value for non-database-record types. |
|
| 425 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 426 | * indexed, FALSE otherwise |
||
| 427 | */ |
||
| 428 | public function containsIndexedItem($itemType, $itemUid) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Removes an item from the Index Queue. |
||
| 435 | * |
||
| 436 | 54 | * @param string $itemType The type of the item to remove, usually a table name. |
|
| 437 | * @param int $itemUid The uid of the item to remove |
||
| 438 | */ |
||
| 439 | public function deleteItem($itemType, $itemUid) |
||
| 443 | |||
| 444 | /** |
||
| 445 | 35 | * Removes all items of a certain type from the Index Queue. |
|
| 446 | * |
||
| 447 | 35 | * @param string $itemType The type of items to remove, usually a table name. |
|
| 448 | */ |
||
| 449 | public function deleteItemsByType($itemType) |
||
| 453 | 35 | ||
| 454 | 35 | /** |
|
| 455 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 456 | 35 | * optional parameter to limit the deleted items by indexing configuration. |
|
| 457 | * |
||
| 458 | * @param Site $site The site to remove items for. |
||
| 459 | 35 | * @param string $indexingConfigurationName Name of a specific indexing |
|
| 460 | * configuration |
||
| 461 | */ |
||
| 462 | public function deleteItemsBySite(Site $site, $indexingConfigurationName = '') |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Removes all items from the Index Queue. |
||
| 469 | * |
||
| 470 | */ |
||
| 471 | 54 | public function deleteAllItems() |
|
| 475 | 54 | ||
| 476 | 35 | /** |
|
| 477 | * Gets a single Index Queue item by its uid. |
||
| 478 | * |
||
| 479 | 54 | * @param int $itemId Index Queue item uid |
|
| 480 | * @return Item The request Index Queue item or NULL if no item with $itemId was found |
||
| 481 | 54 | */ |
|
| 482 | public function getItem($itemId) |
||
| 486 | |||
| 487 | 54 | /** |
|
| 488 | * Gets Index Queue items by type and uid. |
||
| 489 | 54 | * |
|
| 490 | * @param string $itemType item type, usually the table name |
||
| 491 | * @param int $itemUid item uid |
||
| 492 | 54 | * @return Item[] An array of items matching $itemType and $itemUid |
|
| 493 | */ |
||
| 494 | public function getItems($itemType, $itemUid) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Returns all items in the queue. |
||
| 501 | * |
||
| 502 | * @return Item[] An array of items |
||
| 503 | 6 | */ |
|
| 504 | public function getAllItems() |
||
| 508 | 6 | ||
| 509 | 6 | /** |
|
| 510 | 6 | * Returns the number of items for all queues. |
|
| 511 | * |
||
| 512 | * @return int |
||
| 513 | 6 | */ |
|
| 514 | public function getAllItemsCount() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $where |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | private function getItemCount($where = '1=1') |
||
| 530 | 55 | ||
| 531 | 55 | /** |
|
| 532 | 55 | * Extracts the number of pending, indexed and erroneous items from the |
|
| 533 | * Index Queue. |
||
| 534 | * |
||
| 535 | 55 | * @param Site $site |
|
| 536 | * @param string $indexingConfigurationName |
||
| 537 | * |
||
| 538 | * @return QueueStatistic |
||
| 539 | */ |
||
| 540 | public function getStatisticsBySite(Site $site, $indexingConfigurationName = '') |
||
| 568 | 29 | ||
| 569 | /** |
||
| 570 | 29 | * Build a database constraint that limits to a certain indexConfigurationName |
|
| 571 | * |
||
| 572 | * @param string $indexingConfigurationName |
||
| 573 | 29 | * @return string |
|
| 574 | 29 | */ |
|
| 575 | 29 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
| 584 | |||
| 585 | /** |
||
| 586 | 11 | * Gets $limit number of items to index for a particular $site. |
|
| 587 | 11 | * |
|
| 588 | 11 | * @param Site $site TYPO3 site |
|
| 589 | * @param int $limit Number of items to get from the queue |
||
| 590 | 11 | * @return Item[] Items to index to the given solr server |
|
| 591 | 11 | */ |
|
| 592 | 11 | public function getItemsToIndex(Site $site, $limit = 50) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 599 | * next run. |
||
| 600 | * |
||
| 601 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
| 602 | 1 | * @param string $errorMessage Error message |
|
| 603 | */ |
||
| 604 | 1 | public function markItemAsFailed($item, $errorMessage = '') |
|
| 608 | 1 | ||
| 609 | 1 | /** |
|
| 610 | 1 | * Sets the timestamp of when an item last has been indexed. |
|
| 611 | * |
||
| 612 | 1 | * @param Item $item |
|
| 613 | */ |
||
| 614 | public function updateIndexTimeByItem(Item $item) |
||
| 618 | } |
||
| 619 |