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); |
||
| 40 | class QueueItemRepository extends AbstractRepository |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $table = 'tx_solr_indexqueue_item'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var SolrLogManager |
||
| 49 | */ |
||
| 50 | protected $logger; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * QueueItemRepository constructor. |
||
| 54 | * |
||
| 55 | * @param SolrLogManager|null $logManager |
||
| 56 | */ |
||
| 57 | public function __construct(SolrLogManager $logManager = null) |
||
| 58 | { |
||
| 59 | $this->logger = isset($logManager) ? $logManager : GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Fetches the last indexed row |
||
| 64 | * |
||
| 65 | * @param int $rootPageId The root page uid for which to get the last indexed row |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public function findLastIndexedRow(int $rootPageId) : array |
||
| 69 | { |
||
| 70 | $queryBuilder = $this->getQueryBuilder(); |
||
| 71 | $row = $queryBuilder |
||
| 72 | ->select('uid', 'indexed') |
||
| 73 | ->from($this->table) |
||
| 74 | ->where($queryBuilder->expr()->eq('root', $rootPageId)) |
||
| 75 | ->andWhere($queryBuilder->expr()->neq('indexed', 0)) |
||
| 76 | ->orderBy('indexed', 'DESC') |
||
| 77 | ->setMaxResults(1) |
||
| 78 | ->execute()->fetchAll(); |
||
| 79 | |||
| 80 | return $row; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Finds indexing errors for the current site |
||
| 85 | * |
||
| 86 | * @param Site $site |
||
| 87 | * @return array Error items for the current site's Index Queue |
||
| 88 | */ |
||
| 89 | public function findErrorsBySite(Site $site) : array |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Resets all the errors for all index queue items. |
||
| 106 | * |
||
| 107 | * @return int affected rows |
||
| 108 | */ |
||
| 109 | public function flushAllErrors() : int |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
| 124 | * |
||
| 125 | * @param string $itemType The item's type, usually a table name. |
||
| 126 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
| 127 | * different value for non-database-record types. |
||
| 128 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
| 129 | * @param int $rootPageId The uid of the rootPage |
||
| 130 | * @param int $changedTime The forced change time that should be used for updating |
||
| 131 | * @return int affected rows |
||
| 132 | */ |
||
| 133 | public function updateExistingItemByItemTypeAndItemUidAndRootPageId(string $itemType, int $itemUid, int $rootPageId, int $changedTime, string $indexingConfiguration = '') : int |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds an item to the index queue. |
||
| 154 | * |
||
| 155 | * Not meant for public use. |
||
| 156 | * |
||
| 157 | * @param string $itemType The item's type, usually a table name. |
||
| 158 | * @param int $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types. |
||
| 159 | * @param int $rootPageId |
||
| 160 | * @param int $changedTime |
||
| 161 | * @param string $indexingConfiguration The item's indexing configuration to use. Optional, overwrites existing / determined configuration. |
||
| 162 | * @return int the number of inserted rows, which is typically 1 |
||
| 163 | */ |
||
| 164 | public function add(string $itemType, int $itemUid, int $rootPageId, int $changedTime, string $indexingConfiguration) : int |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the most recent changed time of a page's content elements |
||
| 183 | * |
||
| 184 | * @param int $pageUid |
||
| 185 | * @return int|null Timestamp of the most recent content element change or null if nothing is found. |
||
| 186 | */ |
||
| 187 | public function getPageItemChangedTimeByPageUid(int $pageUid) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets the most recent changed time for an item taking into account |
||
| 204 | * localized records. |
||
| 205 | * |
||
| 206 | * @param string $itemType The item's type, usually a table name. |
||
| 207 | * @param int $itemUid The item's uid |
||
| 208 | * @return int Timestamp of the most recent content element change |
||
| 209 | */ |
||
| 210 | public function getLocalizableItemChangedTime(string $itemType, int $itemUid) : int |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns prepared QueryBuilder for contains* methods in this repository |
||
| 238 | * |
||
| 239 | * @param string $itemType |
||
| 240 | * @param int $itemUid |
||
| 241 | * @return QueryBuilder |
||
| 242 | */ |
||
| 243 | protected function getQueryBuilderForContainsMethods(string $itemType, int $itemUid) : QueryBuilder |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Checks whether the Index Queue contains a specific item. |
||
| 255 | * |
||
| 256 | * @param string $itemType The item's type, usually a table name. |
||
| 257 | * @param int $itemUid The item's uid |
||
| 258 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 259 | */ |
||
| 260 | public function containsItem(string $itemType, int $itemUid) : bool |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Checks whether the Index Queue contains a specific item. |
||
| 267 | * |
||
| 268 | * @param string $itemType The item's type, usually a table name. |
||
| 269 | * @param int $itemUid The item's uid |
||
| 270 | * @param integer $rootPageId |
||
| 271 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
| 272 | */ |
||
| 273 | public function containsItemWithRootPageId(string $itemType, int $itemUid, int $rootPageId) : bool |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Checks whether the Index Queue contains a specific item that has been |
||
| 283 | * marked as indexed. |
||
| 284 | * |
||
| 285 | * @param string $itemType The item's type, usually a table name. |
||
| 286 | * @param int $itemUid The item's uid |
||
| 287 | * @return bool TRUE if the item is found in the queue and marked as |
||
| 288 | * indexed, FALSE otherwise |
||
| 289 | */ |
||
| 290 | public function containsIndexedItem(string $itemType, int $itemUid) : bool |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the list with Uids to delete by given item type and optional item Uid |
||
| 300 | * |
||
| 301 | * @param string $itemType The item's type, usually a table name. |
||
| 302 | * @param int|null $itemUid The item's uid |
||
| 303 | * @return array the list with item Uids to delete |
||
| 304 | */ |
||
| 305 | protected function getItemListToDeleteByItemTypeAndOptionalItemUid(string $itemType, int $itemUid = null) : array |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Removes an item from the Index Queue. |
||
| 324 | * |
||
| 325 | * @todo: use transaction |
||
| 326 | * 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 |
||
| 327 | * @param string $itemType The type of the item to remove, usually a table name. |
||
| 328 | * @param int $itemUid The uid of the item to remove |
||
| 329 | */ |
||
| 330 | public function deleteItem(string $itemType, int $itemUid = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Removes all items of a certain type from the Index Queue. |
||
| 356 | * |
||
| 357 | * @param string $itemType The type of items to remove, usually a table name. |
||
| 358 | */ |
||
| 359 | public function deleteItemsByType(string $itemType) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
| 366 | * optional parameter to limit the deleted items by indexing configuration. |
||
| 367 | * |
||
| 368 | * @param Site $site The site to remove items for. |
||
| 369 | * @param string $indexingConfigurationName Name of a specific indexing configuration |
||
| 370 | * @throws \Exception |
||
| 371 | */ |
||
| 372 | public function deleteItemsBySite(Site $site, string $indexingConfigurationName = '') |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Removes all items from the Index Queue. |
||
| 412 | * |
||
| 413 | * @return int The number of affected rows. For a truncate this is unreliable as theres no meaningful information. |
||
| 414 | */ |
||
| 415 | public function deleteAllItems() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Gets a single Index Queue item by its uid. |
||
| 422 | * |
||
| 423 | * @param int $uid Index Queue item uid |
||
| 424 | * @return Item|null The request Index Queue item or NULL if no item with $itemId was found |
||
| 425 | */ |
||
| 426 | public function findItemByUid(int $uid) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Gets Index Queue items by type and uid. |
||
| 446 | * |
||
| 447 | * @param string $itemType item type, usually the table name |
||
| 448 | * @param int $itemUid item uid |
||
| 449 | * @return Item[] An array of items matching $itemType and $itemUid |
||
| 450 | */ |
||
| 451 | public function findItemsByItemTypeAndItemUid(string $itemType, int $itemUid) : array |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Returns a collection of items by CompositeExpression. |
||
| 463 | * D |
||
| 464 | * |
||
| 465 | * @param CompositeExpression|null $expression Optional expression to filter records. |
||
| 466 | * @param QueryBuilder|null $queryBuilder QueryBuilder to use |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | protected function getItemsByCompositeExpression(CompositeExpression $expression = null, QueryBuilder $queryBuilder = null) : array |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns all items in the queue. |
||
| 486 | * |
||
| 487 | * @return Item[] all Items from Queue without restrictions |
||
| 488 | */ |
||
| 489 | public function findAll() : array |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Gets $limit number of items to index for a particular $site. |
||
| 501 | * |
||
| 502 | * @param Site $site TYPO3 site |
||
| 503 | * @param int $limit Number of items to get from the queue |
||
| 504 | * @return Item[] Items to index to the given solr server |
||
| 505 | */ |
||
| 506 | public function findItemsToIndex(Site $site, int $limit = 50) : array |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
| 530 | * index queue records. |
||
| 531 | * |
||
| 532 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
| 533 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
| 534 | */ |
||
| 535 | protected function getIndexQueueItemObjectsFromRecords(array $indexQueueItemRecords) : array |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Returns the records for suitable item type. |
||
| 543 | * |
||
| 544 | * @param array $indexQueueItemRecords |
||
| 545 | * @return array |
||
| 546 | */ |
||
| 547 | protected function getAllQueueItemRecordsByUidsGroupedByTable(array $indexQueueItemRecords) : array |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Calls defined in postProcessFetchRecordsForIndexQueueItem hook method. |
||
| 581 | * |
||
| 582 | * @param string $table |
||
| 583 | * @param array $uids |
||
| 584 | * @param array $tableRecords |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | protected function hookPostProcessFetchRecordsForIndexQueueItem(string $table, array $uids, array &$tableRecords) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Instantiates a list of Item objects from database records. |
||
| 601 | * |
||
| 602 | * @param array $indexQueueItemRecords records from database |
||
| 603 | * @param array $tableRecords |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | protected function getQueueItemObjectsByRecords(array $indexQueueItemRecords, array $tableRecords) : array |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Marks an item as failed and causes the indexer to skip the item in the |
||
| 634 | * next run. |
||
| 635 | * |
||
| 636 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
| 637 | * @param string $errorMessage Error message |
||
| 638 | * @return int affected rows |
||
| 639 | */ |
||
| 640 | public function markItemAsFailed($item, string $errorMessage = ''): int |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Sets the timestamp of when an item last has been indexed. |
||
| 663 | * |
||
| 664 | * @param Item $item |
||
| 665 | * @return int affected rows |
||
| 666 | */ |
||
| 667 | public function updateIndexTimeByItem(Item $item) : int |
||
| 676 | } |
||
| 677 |