Complex classes like QueueItemRepository 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 QueueItemRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 41 | class QueueItemRepository extends AbstractRepository |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $table = 'tx_solr_indexqueue_item'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var SolrLogManager |
||
| 50 | */ |
||
| 51 | protected $logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * QueueItemRepository constructor. |
||
| 55 | * |
||
| 56 | * @param SolrLogManager|null $logManager |
||
| 57 | */ |
||
| 58 | public function __construct(SolrLogManager $logManager = null) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Fetches the last indexed row |
||
| 65 | * |
||
| 66 | * @param int $rootPageId The root page uid for which to get the last indexed row |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function findLastIndexedRow(int $rootPageId) : array |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Finds indexing errors for the current site |
||
| 86 | * |
||
| 87 | * @param Site $site |
||
| 88 | * @return array Error items for the current site's Index Queue |
||
| 89 | */ |
||
| 90 | public function findErrorsBySite(Site $site) : array |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Resets all the errors for all index queue items. |
||
| 107 | * |
||
| 108 | * @return int affected rows |
||
| 109 | */ |
||
| 110 | public function flushAllErrors() : int |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
| 125 | * |
||
| 126 | * @param string $itemType The item's type, usually a table name. |
||
| 127 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
| 128 | * different value for non-database-record types. |
||
| 129 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
| 130 | * @param int $rootPageId The uid of the rootPage |
||
| 131 | * @param int $changedTime The forced change time that should be used for updating |
||
| 132 | * @return int affected rows |
||
| 133 | */ |
||
| 134 | public function updateExistingItemByItemTypeAndItemUidAndRootPageId(string $itemType, int $itemUid, int $rootPageId, int $changedTime, string $indexingConfiguration = '') : int |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Adds an item to the index queue. |
||
| 155 | * |
||
| 156 | * Not meant for public use. |
||
| 157 | * |
||
| 158 | * @param string $itemType The item's type, usually a table name. |
||
| 159 | * @param int $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types. |
||
| 160 | * @param int $rootPageId |
||
| 161 | * @param int $changedTime |
||
| 162 | * @param string $indexingConfiguration The item's indexing configuration to use. Optional, overwrites existing / determined configuration. |
||
| 163 | * @return int the number of inserted rows, which is typically 1 |
||
| 164 | */ |
||
| 165 | public function add(string $itemType, int $itemUid, int $rootPageId, int $changedTime, string $indexingConfiguration) : int |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the most recent changed time of a page's content elements |
||
| 184 | * |
||
| 185 | * @param int $pageUid |
||
| 186 | * @return int|null Timestamp of the most recent content element change or null if nothing is found. |
||
| 187 | */ |
||
| 188 | public function getPageItemChangedTimeByPageUid(int $pageUid) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Gets the most recent changed time for an item taking into account |
||
| 205 | * localized records. |
||
| 206 | * |
||
| 207 | * @param string $itemType The item's type, usually a table name. |
||
| 208 | * @param int $itemUid The item's uid |
||
| 209 | * @return int Timestamp of the most recent content element change |
||
| 210 | */ |
||
| 211 | public function getLocalizableItemChangedTime(string $itemType, int $itemUid) : int |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns prepared QueryBuilder for contains* methods in this repository |
||
| 239 | * |
||
| 240 | * @param string $itemType |
||
| 241 | * @param int $itemUid |
||
| 242 | * @return QueryBuilder |
||
| 243 | */ |
||
| 244 | protected function getQueryBuilderForContainsMethods(string $itemType, int $itemUid) : QueryBuilder |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Checks whether the Index Queue contains a specific item. |
||
| 256 | * |
||
| 257 | * @param string $itemType The item's type, usually a table name. |
||
| 258 | * @param int $itemUid The item's uid |
||
| 259 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 260 | */ |
||
| 261 | public function containsItem(string $itemType, int $itemUid) : bool |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Checks whether the Index Queue contains a specific item. |
||
| 268 | * |
||
| 269 | * @param string $itemType The item's type, usually a table name. |
||
| 270 | * @param int $itemUid The item's uid |
||
| 271 | * @param integer $rootPageId |
||
| 272 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 273 | */ |
||
| 274 | public function containsItemWithRootPageId(string $itemType, int $itemUid, int $rootPageId) : bool |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Checks whether the Index Queue contains a specific item that has been |
||
| 284 | * marked as indexed. |
||
| 285 | * |
||
| 286 | * @param string $itemType The item's type, usually a table name. |
||
| 287 | * @param int $itemUid The item's uid |
||
| 288 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 289 | * indexed, FALSE otherwise |
||
| 290 | */ |
||
| 291 | public function containsIndexedItem(string $itemType, int $itemUid) : bool |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the list with Uids to delete by given item type and optional item Uid |
||
| 301 | * |
||
| 302 | * @param string $itemType The item's type, usually a table name. |
||
| 303 | * @param int|null $itemUid The item's uid |
||
| 304 | * @return array the list with item Uids to delete |
||
| 305 | */ |
||
| 306 | protected function getItemListToDeleteByItemTypeAndOptionalItemUid(string $itemType, int $itemUid = null) : array |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Removes an item from the Index Queue. |
||
| 325 | * |
||
| 326 | * @todo: use transaction |
||
| 327 | * use sub-select for tx_solr_indexqueue_indexing_property IN() clause and native WHERE clause for tx_solr_indexqueue_item instead of fetching and concat it with PHP |
||
| 328 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 329 | * @param int $itemUid The uid of the item to remove |
||
| 330 | */ |
||
| 331 | public function deleteItem(string $itemType, int $itemUid = null) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Removes all items of a certain type from the Index Queue. |
||
| 357 | * |
||
| 358 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 359 | */ |
||
| 360 | public function deleteItemsByType(string $itemType) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 367 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 368 | * |
||
| 369 | * @param Site $site The site to remove items for. |
||
| 370 | * @param string $indexingConfigurationName Name of a specific indexing configuration |
||
| 371 | * @throws \Exception |
||
| 372 | */ |
||
| 373 | public function deleteItemsBySite(Site $site, string $indexingConfigurationName = '') |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Removes all items from the Index Queue. |
||
| 413 | * |
||
| 414 | * @return int The number of affected rows. For a truncate this is unreliable as theres no meaningful information. |
||
| 415 | */ |
||
| 416 | public function deleteAllItems() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets a single Index Queue item by its uid. |
||
| 423 | * |
||
| 424 | * @param int $uid Index Queue item uid |
||
| 425 | * @return Item|null The request Index Queue item or NULL if no item with $itemId was found |
||
| 426 | */ |
||
| 427 | public function findItemByUid(int $uid) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Gets Index Queue items by type and uid. |
||
| 447 | * |
||
| 448 | * @param string $itemType item type, usually the table name |
||
| 449 | * @param int $itemUid item uid |
||
| 450 | * @return Item[] An array of items matching $itemType and $itemUid |
||
| 451 | */ |
||
| 452 | public function findItemsByItemTypeAndItemUid(string $itemType, int $itemUid) : array |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns a collection of items by CompositeExpression. |
||
| 464 | * D |
||
| 465 | * |
||
| 466 | * @param CompositeExpression|null $expression Optional expression to filter records. |
||
| 467 | * @param QueryBuilder|null $queryBuilder QueryBuilder to use |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | protected function getItemsByCompositeExpression(CompositeExpression $expression = null, QueryBuilder $queryBuilder = null) : array |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns all items in the queue. |
||
| 487 | * |
||
| 488 | * @return Item[] all Items from Queue without restrictions |
||
| 489 | */ |
||
| 490 | public function findAll() : array |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Gets $limit number of items to index for a particular $site. |
||
| 502 | * |
||
| 503 | * @param Site $site TYPO3 site |
||
| 504 | * @param int $limit Number of items to get from the queue |
||
| 505 | * @return Item[] Items to index to the given solr server |
||
| 506 | */ |
||
| 507 | public function findItemsToIndex(Site $site, int $limit = 50) : array |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
| 531 | * index queue records. |
||
| 532 | * |
||
| 533 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
| 534 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
| 535 | */ |
||
| 536 | protected function getIndexQueueItemObjectsFromRecords(array $indexQueueItemRecords) : array |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the records for suitable item type. |
||
| 544 | * |
||
| 545 | * @param array $indexQueueItemRecords |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | protected function getAllQueueItemRecordsByUidsGroupedByTable(array $indexQueueItemRecords) : array |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Calls defined in postProcessFetchRecordsForIndexQueueItem hook method. |
||
| 582 | * |
||
| 583 | * @param string $table |
||
| 584 | * @param array $uids |
||
| 585 | * @param array $tableRecords |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | */ |
||
| 589 | protected function hookPostProcessFetchRecordsForIndexQueueItem(string $table, array $uids, array &$tableRecords) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Instantiates a list of Item objects from database records. |
||
| 602 | * |
||
| 603 | * @param array $indexQueueItemRecords records from database |
||
| 604 | * @param array $tableRecords |
||
| 605 | * @return array |
||
| 606 | */ |
||
| 607 | protected function getQueueItemObjectsByRecords(array $indexQueueItemRecords, array $tableRecords) : array |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 635 | * next run. |
||
| 636 | * |
||
| 637 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
| 638 | * @param string $errorMessage Error message |
||
| 639 | * @return int affected rows |
||
| 640 | */ |
||
| 641 | public function markItemAsFailed($item, string $errorMessage = ''): int |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Sets the timestamp of when an item last has been indexed. |
||
| 664 | * |
||
| 665 | * @param Item $item |
||
| 666 | * @return int affected rows |
||
| 667 | */ |
||
| 668 | public function updateIndexTimeByItem(Item $item) : int |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Initializes Queue by given sql |
||
| 680 | * |
||
| 681 | * Note: Do not use platform specific functions! |
||
| 682 | * |
||
| 683 | * @param string $sqlStatement Native SQL statement |
||
| 684 | * @return int The number of affected rows. |
||
| 685 | * @internal |
||
| 686 | * @throws DBALException |
||
| 687 | */ |
||
| 688 | public function initializeByNativeSQLStatement(string $sqlStatement) : int |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Retrieves an array of pageIds from mountPoints that allready have a queue entry. |
||
| 695 | * |
||
| 696 | * @param string $identifier identifier of the mount point |
||
| 697 | * @return array pageIds from mountPoints that allready have a queue entry |
||
| 698 | */ |
||
| 699 | public function findPageIdsOfExistingMountPagesByMountIdentifier(string $identifier) : array |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Retrieves an array of items for mount destinations mathed by root page ID, Mount Identifier and a list of mounted page IDs. |
||
| 725 | * |
||
| 726 | * @param int $rootPid |
||
| 727 | * @param string $identifier identifier of the mount point |
||
| 728 | * @param array $mountedPids An array of mounted page IDs |
||
| 729 | * @return array |
||
| 730 | */ |
||
| 731 | public function findAllIndexQueueItemsByRootPidAndMountIdentifierAndMountedPids(int $rootPid, string $identifier, array $mountedPids) : array |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Updates has_indexing_properties field for given Item |
||
| 749 | * |
||
| 750 | * @param int $itemUid |
||
| 751 | * @param bool $hasIndexingPropertiesFlag |
||
| 752 | * @return int number of affected rows, 1 on success |
||
| 753 | */ |
||
| 754 | public function updateHasIndexingPropertiesFlagByItemUid(int $itemUid, bool $hasIndexingPropertiesFlag): int |
||
| 763 | } |
||
| 764 |