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 | * Queue constructor. |
||
| 63 | * @param RootPageResolver|null $rootPageResolver |
||
| 64 | * @param ConfigurationAwareRecordService|null $recordService |
||
| 65 | */ |
||
| 66 | 89 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null) |
|
| 72 | |||
| 73 | // FIXME some of the methods should be renamed to plural forms |
||
| 74 | // FIXME singular form methods should deal with exactly one item only |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns the timestamp of the last indexing run. |
||
| 78 | * |
||
| 79 | * @param int $rootPageId The root page uid for which to get |
||
| 80 | * the last indexed item id |
||
| 81 | * @return int Timestamp of last index run. |
||
| 82 | */ |
||
| 83 | 2 | public function getLastIndexTime($rootPageId) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Returns the uid of the last indexed item in the queue |
||
| 98 | * |
||
| 99 | * @param int $rootPageId The root page uid for which to get |
||
| 100 | * the last indexed item id |
||
| 101 | * @return int The last indexed item's ID. |
||
| 102 | */ |
||
| 103 | 3 | public function getLastIndexedItemId($rootPageId) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Fetches the last indexed row |
||
| 117 | * |
||
| 118 | * @param int $rootPageId The root page uid for which to get the last indexed row |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 5 | protected function getLastIndexedRow($rootPageId) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
| 141 | * complete way to force reindexing, or to build the Index Queue for the |
||
| 142 | * first time. The Index Queue initialization is site-specific. |
||
| 143 | * |
||
| 144 | * @param Site $site The site to initialize |
||
| 145 | * @param string $indexingConfigurationName Name of a specific |
||
| 146 | * indexing configuration |
||
| 147 | * @return array An array of booleans, each representing whether the |
||
| 148 | * initialization for an indexing configuration was successful |
||
| 149 | */ |
||
| 150 | 6 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Initializes the Index Queue for a specific indexing configuration. |
||
| 194 | * |
||
| 195 | * @param Site $site The site to initialize |
||
| 196 | * @param string $indexingConfigurationName name of a specific |
||
| 197 | * indexing configuration |
||
| 198 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
| 199 | */ |
||
| 200 | 6 | protected function initializeIndexingConfiguration( |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the indexing configuration to use for an item. |
||
| 226 | * Sometimes, when there are multiple configurations for a certain item type |
||
| 227 | * (table) it can be hard or even impossible to find which one to use |
||
| 228 | * though. |
||
| 229 | * Currently selects the first indexing configuration where the name matches |
||
| 230 | * the itemType or where the configured tbale is the same as the itemType. |
||
| 231 | * |
||
| 232 | * !!! Might return incorrect results for complex configurations !!! |
||
| 233 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
| 234 | * method in such situations. |
||
| 235 | * |
||
| 236 | * @param string $itemType The item's type, usually a table name. |
||
| 237 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 238 | * different value for non-database-record types. |
||
| 239 | * @param int $rootPageId The configuration's page tree's root page id. |
||
| 240 | * Optional, not needed for all types. |
||
| 241 | * @return string The indexing configuration's name to use when indexing |
||
| 242 | * @deprecated since 6.1 will be removed in 7.0 |
||
| 243 | * Use getIndexingConfigurationsByItem() now, which behaves |
||
| 244 | * almost the same way but returns an array of configurations |
||
| 245 | */ |
||
| 246 | protected function getIndexingConfigurationByItem( |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Gets the indexing configurations to use for an item. |
||
| 264 | * Multiple configurations for a certain item type (table) might be available. |
||
| 265 | * |
||
| 266 | * @param string $itemType The item's type, usually a table name. |
||
| 267 | * @param int $rootPageId The configuration's page tree's root page id. |
||
| 268 | * Optional, not needed for all types. |
||
| 269 | * @return array<string> The indexing configurations names to use when indexing |
||
| 270 | * @deprecated since 6.1 will be removed in 7.0 |
||
| 271 | */ |
||
| 272 | protected function getIndexingConfigurationsByItem( |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Marks an item as needing (re)indexing. |
||
| 291 | * |
||
| 292 | * Like with Solr itself, there's no add method, just a simple update method |
||
| 293 | * that handles the adds, too. |
||
| 294 | * |
||
| 295 | * The method creates or updates the index queue items for all related rootPageIds. |
||
| 296 | * |
||
| 297 | * @param string $itemType The item's type, usually a table name. |
||
| 298 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 299 | * different value for non-database-record types. |
||
| 300 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
| 301 | * value from getItemChangedTime() is used. |
||
| 302 | */ |
||
| 303 | 50 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Finds indexing errors for the current site |
||
| 327 | * |
||
| 328 | * @param Site $site |
||
| 329 | * @return array Error items for the current site's Index Queue |
||
| 330 | */ |
||
| 331 | public function getErrorsBySite(Site $site) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Resets all the errors for all index queue items. |
||
| 342 | * |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | public function resetAllErrors() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
| 356 | * |
||
| 357 | * @param string $itemType The item's type, usually a table name. |
||
| 358 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
| 359 | * different value for non-database-record types. |
||
| 360 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
| 361 | * @param int $rootPageId The uid of the rootPage |
||
| 362 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
| 363 | */ |
||
| 364 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Adds an item to the index queue. |
||
| 384 | * |
||
| 385 | * Not meant for public use. |
||
| 386 | * |
||
| 387 | * @param string $itemType The item's type, usually a 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 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
| 391 | * Optional, overwrites existing / determined configuration. |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | 43 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Get record to be added in addNewItem |
||
| 422 | * |
||
| 423 | * @param string $itemType The item's type, usually a table name. |
||
| 424 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 425 | * different value for non-database-record types. |
||
| 426 | * @param string $additionalRecordFields for sql-query |
||
| 427 | * |
||
| 428 | * @return array|NULL |
||
| 429 | */ |
||
| 430 | 43 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Determines the time for when an item should be indexed. This timestamp |
||
| 446 | * is then stored in the changed column in the Index Queue. |
||
| 447 | * |
||
| 448 | * The changed timestamp usually is now - time(). For records which are set |
||
| 449 | * to published at a later time, this timestamp is the start time. So if a |
||
| 450 | * future start time has been set, that will be used to delay indexing |
||
| 451 | * of an item. |
||
| 452 | * |
||
| 453 | * @param string $itemType The item's table name. |
||
| 454 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 455 | * different value for non-database-record types. |
||
| 456 | * @return int Timestamp of the item's changed time or future start time |
||
| 457 | */ |
||
| 458 | 48 | protected function getItemChangedTime($itemType, $itemUid) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Gets the most recent changed time of a page's content elements |
||
| 506 | * |
||
| 507 | * @param array $page Partial page record |
||
| 508 | * @return int Timestamp of the most recent content element change |
||
| 509 | */ |
||
| 510 | 35 | protected function getPageItemChangedTime(array $page) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Gets the most recent changed time for an item taking into account |
||
| 529 | * localized records. |
||
| 530 | * |
||
| 531 | * @param string $itemType The item's type, usually a table name. |
||
| 532 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 533 | * different value for non-database-record types. |
||
| 534 | * @return int Timestamp of the most recent content element change |
||
| 535 | */ |
||
| 536 | 48 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Checks whether the Index Queue contains a specific item. |
||
| 558 | * |
||
| 559 | * @param string $itemType The item's type, usually a table name. |
||
| 560 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 561 | * different value for non-database-record types. |
||
| 562 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 563 | */ |
||
| 564 | 3 | public function containsItem($itemType, $itemUid) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Checks whether the Index Queue contains a specific item. |
||
| 579 | * |
||
| 580 | * @param string $itemType The item's type, usually a table name. |
||
| 581 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 582 | * different value for non-database-record types. |
||
| 583 | * @param integer $rootPageId |
||
| 584 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 585 | */ |
||
| 586 | 49 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Checks whether the Index Queue contains a specific item that has been |
||
| 601 | * marked as indexed. |
||
| 602 | * |
||
| 603 | * @param string $itemType The item's type, usually a table name. |
||
| 604 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
| 605 | * different value for non-database-record types. |
||
| 606 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 607 | * indexed, FALSE otherwise |
||
| 608 | */ |
||
| 609 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Removes an item from the Index Queue. |
||
| 625 | * |
||
| 626 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 627 | * @param int $itemUid The uid of the item to remove |
||
| 628 | */ |
||
| 629 | 29 | public function deleteItem($itemType, $itemUid) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Removes all items of a certain type from the Index Queue. |
||
| 660 | * |
||
| 661 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 662 | */ |
||
| 663 | 1 | public function deleteItemsByType($itemType) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 695 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 696 | * |
||
| 697 | * @param Site $site The site to remove items for. |
||
| 698 | * @param string $indexingConfigurationName Name of a specific indexing |
||
| 699 | * configuration |
||
| 700 | */ |
||
| 701 | 6 | public function deleteItemsBySite( |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Removes all items from the Index Queue. |
||
| 754 | * |
||
| 755 | */ |
||
| 756 | 1 | public function deleteAllItems() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Gets a single Index Queue item by its uid. |
||
| 763 | * |
||
| 764 | * @param int $itemId Index Queue item uid |
||
| 765 | * @return Item The request Index Queue item or NULL |
||
| 766 | * if no item with $itemId was found |
||
| 767 | */ |
||
| 768 | 19 | public function getItem($itemId) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Gets Index Queue items by type and uid. |
||
| 791 | * |
||
| 792 | * @param string $itemType item type, usually the table name |
||
| 793 | * @param int $itemUid item uid |
||
| 794 | * @return Item[] An array of items matching $itemType and $itemUid |
||
| 795 | */ |
||
| 796 | 22 | public function getItems($itemType, $itemUid) |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
| 811 | * optional parameter to limit the counted items by indexing configuration. |
||
| 812 | * |
||
| 813 | * @param Site $site The site to search for. |
||
| 814 | * @param string $indexingConfigurationName name of a specific indexing |
||
| 815 | * configuration |
||
| 816 | * @deprecated since 6.1 will be removed in 7.0 use getStatisticsBySite()->getTotalCount() please |
||
| 817 | * @return int Number of items |
||
| 818 | */ |
||
| 819 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
| 827 | * optional parameter to limit the counted items by indexing configuration. |
||
| 828 | * |
||
| 829 | * @param Site $site The site to search for. |
||
| 830 | * @param string $indexingConfigurationName name of a specific indexing |
||
| 831 | * configuration |
||
| 832 | * @deprecated since 6.1 will be removed in 7.0 use getStatisticsBySite()->getPendingCount() please |
||
| 833 | * @return int Number of items. |
||
| 834 | */ |
||
| 835 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns the number of items for all queues. |
||
| 843 | * |
||
| 844 | * @return int |
||
| 845 | */ |
||
| 846 | 64 | public function getAllItemsCount() |
|
| 850 | |||
| 851 | /** |
||
| 852 | * @param string $where |
||
| 853 | * @return int |
||
| 854 | */ |
||
| 855 | 64 | private function getItemCount($where = '1=1') |
|
| 862 | |||
| 863 | /** |
||
| 864 | * Extracts the number of pending, indexed and erroneous items from the |
||
| 865 | * Index Queue. |
||
| 866 | * |
||
| 867 | * @param Site $site |
||
| 868 | * @param string $indexingConfigurationName |
||
| 869 | * |
||
| 870 | * @return QueueStatistic |
||
| 871 | */ |
||
| 872 | 5 | public function getStatisticsBySite(Site $site, $indexingConfigurationName = '') |
|
| 900 | |||
| 901 | /** |
||
| 902 | * Build a database constraint that limits to a certain indexConfigurationName |
||
| 903 | * |
||
| 904 | * @param string $indexingConfigurationName |
||
| 905 | * @return string |
||
| 906 | */ |
||
| 907 | 5 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Gets $limit number of items to index for a particular $site. |
||
| 919 | * |
||
| 920 | * @param Site $site TYPO3 site |
||
| 921 | * @param int $limit Number of items to get from the queue |
||
| 922 | * @return Item[] Items to index to the given solr server |
||
| 923 | */ |
||
| 924 | 7 | public function getItemsToIndex(Site $site, $limit = 50) |
|
| 947 | |||
| 948 | /** |
||
| 949 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
| 950 | * index queue records. |
||
| 951 | * |
||
| 952 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
| 953 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
| 954 | */ |
||
| 955 | 27 | protected function getIndexQueueItemObjectsFromRecords( |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 1015 | * next run. |
||
| 1016 | * |
||
| 1017 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
| 1018 | * @param string $errorMessage Error message |
||
| 1019 | */ |
||
| 1020 | 6 | public function markItemAsFailed($item, $errorMessage = '') |
|
| 1021 | { |
||
| 1022 | 6 | if ($item instanceof Item) { |
|
| 1023 | 2 | $itemUid = $item->getIndexQueueUid(); |
|
| 1024 | 2 | } else { |
|
| 1025 | 4 | $itemUid = (int)$item; |
|
| 1026 | } |
||
| 1027 | |||
| 1028 | 6 | if (empty($errorMessage)) { |
|
| 1029 | // simply set to "TRUE" |
||
| 1030 | 2 | $errorMessage = '1'; |
|
| 1031 | 2 | } |
|
| 1032 | |||
| 1033 | 6 | $GLOBALS['TYPO3_DB']->exec_UPDATEquery( |
|
| 1034 | 6 | 'tx_solr_indexqueue_item', |
|
| 1035 | 6 | 'uid = ' . $itemUid, |
|
| 1036 | [ |
||
| 1037 | 'errors' => $errorMessage |
||
| 1038 | 6 | ] |
|
| 1039 | 6 | ); |
|
| 1040 | 6 | } |
|
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Sets the timestamp of when an item last has been indexed. |
||
| 1044 | * |
||
| 1045 | * @param Item $item |
||
| 1046 | */ |
||
| 1047 | 6 | public function updateIndexTimeByItem(Item $item) |
|
| 1055 | } |
||
| 1056 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.