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 |
||
42 | class Queue |
||
43 | { |
||
44 | /** |
||
45 | * @var RootPageResolver |
||
46 | */ |
||
47 | protected $rootPageResolver; |
||
48 | |||
49 | /** |
||
50 | * @var ConfigurationAwareRecordService |
||
51 | */ |
||
52 | protected $recordService; |
||
53 | |||
54 | /** |
||
55 | * Queue constructor. |
||
56 | * @param RootPageResolver|null $rootPageResolver |
||
57 | * @param ConfigurationAwareRecordService|null $recordService |
||
58 | */ |
||
59 | 67 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null) |
|
64 | |||
65 | // FIXME some of the methods should be renamed to plural forms |
||
66 | // FIXME singular form methods should deal with exactly one item only |
||
67 | |||
68 | /** |
||
69 | * Returns the timestamp of the last indexing run. |
||
70 | * |
||
71 | * @param int $rootPageId The root page uid for which to get |
||
72 | * the last indexed item id |
||
73 | * @return int Timestamp of last index run. |
||
74 | */ |
||
75 | public function getLastIndexTime($rootPageId) |
||
94 | |||
95 | /** |
||
96 | * Returns the uid of the last indexed item in the queue |
||
97 | * |
||
98 | * @param int $rootPageId The root page uid for which to get |
||
99 | * the last indexed item id |
||
100 | * @return int The last indexed item's ID. |
||
101 | */ |
||
102 | public function getLastIndexedItemId($rootPageId) |
||
120 | |||
121 | /** |
||
122 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
123 | * complete way to force reindexing, or to build the Index Queue for the |
||
124 | * first time. The Index Queue initialization is site-specific. |
||
125 | * |
||
126 | * @param Site $site The site to initialize |
||
127 | * @param string $indexingConfigurationName Name of a specific |
||
128 | * indexing configuration |
||
129 | * @return array An array of booleans, each representing whether the |
||
130 | * initialization for an indexing configuration was successful |
||
131 | */ |
||
132 | 5 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
173 | |||
174 | /** |
||
175 | * Initializes the Index Queue for a specific indexing configuration. |
||
176 | * |
||
177 | * @param Site $site The site to initialize |
||
178 | * @param string $indexingConfigurationName name of a specific |
||
179 | * indexing configuration |
||
180 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
181 | */ |
||
182 | 5 | protected function initializeIndexingConfiguration( |
|
205 | |||
206 | /** |
||
207 | * Gets the indexing configuration to use for an item. |
||
208 | * Sometimes, when there are multiple configurations for a certain item type |
||
209 | * (table) it can be hard or even impossible to find which one to use |
||
210 | * though. |
||
211 | * Currently selects the first indexing configuration where the name matches |
||
212 | * the itemType or where the configured tbale is the same as the itemType. |
||
213 | * |
||
214 | * !!! Might return incorrect results for complex configurations !!! |
||
215 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
216 | * method in such situations. |
||
217 | * |
||
218 | * @param string $itemType The item's type, usually a table name. |
||
219 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
220 | * different value for non-database-record types. |
||
221 | * @param int $rootPageId The configuration's page tree's root page id. |
||
222 | * Optional, not needed for all types. |
||
223 | * @return string The indexing configuration's name to use when indexing |
||
224 | * @deprecated Use getIndexingConfigurationsByItem() now, which behaves |
||
225 | * almost the same way but returns an array of configurations, will be removed in version 7.0 |
||
226 | */ |
||
227 | protected function getIndexingConfigurationByItem( |
||
243 | |||
244 | /** |
||
245 | * Gets the indexing configurations to use for an item. |
||
246 | * Multiple configurations for a certain item type (table) might be available. |
||
247 | * |
||
248 | * @param string $itemType The item's type, usually a table name. |
||
249 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
250 | * different value for non-database-record types. |
||
251 | * @param int $rootPageId The configuration's page tree's root page id. |
||
252 | * Optional, not needed for all types. |
||
253 | * @return array<string> The indexing configurations names to use when indexing |
||
254 | */ |
||
255 | protected function getIndexingConfigurationsByItem( |
||
270 | |||
271 | /** |
||
272 | * Marks an item as needing (re)indexing. |
||
273 | * |
||
274 | * Like with Solr itself, there's no add method, just a simple update method |
||
275 | * that handles the adds, too. |
||
276 | * |
||
277 | * The method creates or updates the index queue items for all related rootPageIds. |
||
278 | * |
||
279 | * @param string $itemType The item's type, usually a table name. |
||
280 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
281 | * different value for non-database-record types. |
||
282 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
283 | * value from getItemChangedTime() is used. |
||
284 | */ |
||
285 | 47 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
306 | |||
307 | /** |
||
308 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
309 | * |
||
310 | * @param string $itemType The item's type, usually a table name. |
||
311 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
312 | * different value for non-database-record types. |
||
313 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
314 | * @param int $rootPageId The uid of the rootPage |
||
315 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
316 | */ |
||
317 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
334 | |||
335 | /** |
||
336 | * Adds an item to the index queue. |
||
337 | * |
||
338 | * Not meant for public use. |
||
339 | * |
||
340 | * @param string $itemType The item's type, usually a table name. |
||
341 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
342 | * different value for non-database-record types. |
||
343 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
344 | * Optional, overwrites existing / determined configuration. |
||
345 | * @return void |
||
346 | */ |
||
347 | 40 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
372 | |||
373 | /** |
||
374 | * Get record to be added in addNewItem |
||
375 | * |
||
376 | * @param string $itemType The item's type, usually a table name. |
||
377 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
378 | * different value for non-database-record types. |
||
379 | * @param string $additionalRecordFields for sql-query |
||
380 | * |
||
381 | * @return array|NULL |
||
382 | */ |
||
383 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
||
396 | |||
397 | 45 | /** |
|
398 | * Determines the time for when an item should be indexed. This timestamp |
||
399 | * is then stored in the changed column in the Index Queue. |
||
400 | 32 | * |
|
401 | * The changed timestamp usually is now - time(). For records which are set |
||
402 | * to published at a later time, this timestamp is the start time. So if a |
||
403 | 45 | * future start time has been set, that will be used to delay indexing |
|
404 | 45 | * of an item. |
|
405 | * |
||
406 | 45 | * @param string $itemType The item's table name. |
|
407 | 45 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
408 | * different value for non-database-record types. |
||
409 | * @return int Timestamp of the item's changed time or future start time |
||
410 | 45 | */ |
|
411 | 32 | protected function getItemChangedTime($itemType, $itemUid) |
|
412 | { |
||
413 | $itemTypeHasStartTimeColumn = false; |
||
414 | 32 | $changedTimeColumns = $GLOBALS['TCA'][$itemType]['ctrl']['tstamp']; |
|
415 | $startTime = 0; |
||
416 | $pageChangedTime = 0; |
||
417 | 45 | ||
418 | if (!empty($GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'])) { |
||
419 | $itemTypeHasStartTimeColumn = true; |
||
420 | $changedTimeColumns .= ', ' . $GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime']; |
||
421 | } |
||
422 | 45 | if ($itemType == 'pages') { |
|
423 | // does not carry time information directly, but needed to support |
||
424 | // canonical pages |
||
425 | $changedTimeColumns .= ', content_from_pid'; |
||
426 | } |
||
427 | |||
428 | $record = BackendUtility::getRecord($itemType, $itemUid, $changedTimeColumns); |
||
429 | 45 | $itemChangedTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['tstamp']]; |
|
430 | |||
431 | if ($itemTypeHasStartTimeColumn) { |
||
432 | $startTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime']]; |
||
433 | } |
||
434 | |||
435 | if ($itemType == 'pages') { |
||
436 | $record['uid'] = $itemUid; |
||
437 | // overrule the page's last changed time with the most recent |
||
438 | 32 | //content element change |
|
439 | $pageChangedTime = $this->getPageItemChangedTime($record); |
||
440 | 32 | } |
|
441 | |||
442 | $localizationsChangedTime = $this->getLocalizableItemChangedTime($itemType, $itemUid); |
||
443 | |||
444 | 32 | // if start time exists and start time is higher than last changed timestamp |
|
445 | 32 | // then set changed to the future start time to make the item |
|
446 | 32 | // indexed at a later time |
|
447 | 32 | $changedTime = max( |
|
448 | $itemChangedTime, |
||
449 | 32 | $pageChangedTime, |
|
450 | $localizationsChangedTime, |
||
451 | $startTime |
||
452 | 32 | ); |
|
453 | |||
454 | return $changedTime; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Gets the most recent changed time of a page's content elements |
||
459 | * |
||
460 | * @param array $page Partial page record |
||
461 | * @return int Timestamp of the most recent content element change |
||
462 | */ |
||
463 | protected function getPageItemChangedTime(array $page) |
||
464 | 45 | { |
|
465 | if (!empty($page['content_from_pid'])) { |
||
466 | 45 | // canonical page, get the original page's last changed time |
|
467 | $pageContentLastChangedTime = $this->getPageItemChangedTime(['uid' => $page['content_from_pid']]); |
||
468 | 45 | } else { |
|
469 | $pageContentLastChangedTime = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow( |
||
470 | 13 | 'MAX(tstamp) AS changed_time', |
|
471 | 'tt_content', |
||
472 | 13 | 'pid = ' . (int)$page['uid'] |
|
473 | 13 | ); |
|
474 | 13 | $pageContentLastChangedTime = $pageContentLastChangedTime['changed_time']; |
|
475 | } |
||
476 | 13 | ||
477 | return $pageContentLastChangedTime; |
||
478 | 13 | } |
|
479 | |||
480 | /** |
||
481 | 45 | * Gets the most recent changed time for an item taking into account |
|
482 | * localized records. |
||
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 int Timestamp of the most recent content element change |
||
488 | */ |
||
489 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
||
490 | { |
||
491 | $localizedChangedTime = 0; |
||
492 | 3 | ||
493 | if (isset($GLOBALS['TCA'][$itemType]['ctrl']['transOrigPointerField'])) { |
||
494 | 3 | // table is localizable |
|
495 | 3 | $translationOriginalPointerField = $GLOBALS['TCA'][$itemType]['ctrl']['transOrigPointerField']; |
|
496 | 3 | ||
497 | 3 | $itemUid = intval($itemUid); |
|
498 | 3 | $localizedChangedTime = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow( |
|
499 | 3 | 'MAX(tstamp) AS changed_time', |
|
500 | $itemType, |
||
501 | "uid = $itemUid OR $translationOriginalPointerField = $itemUid" |
||
502 | 3 | ); |
|
503 | $localizedChangedTime = $localizedChangedTime['changed_time']; |
||
504 | } |
||
505 | |||
506 | return $localizedChangedTime; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Checks whether the Index Queue contains a specific item. |
||
511 | * |
||
512 | * @param string $itemType The item's type, usually a table name. |
||
513 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
514 | 46 | * different value for non-database-record types. |
|
515 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
516 | 46 | */ |
|
517 | 46 | public function containsItem($itemType, $itemUid) |
|
518 | 46 | { |
|
519 | 46 | $itemIsInQueue = (boolean)$GLOBALS['TYPO3_DB']->exec_SELECTcountRows( |
|
520 | 46 | 'uid', |
|
521 | 46 | 'tx_solr_indexqueue_item', |
|
522 | 'item_type = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($itemType, |
||
523 | 'tx_solr_indexqueue_item') . |
||
524 | 46 | ' AND item_uid = ' . (int)$itemUid |
|
525 | ); |
||
526 | |||
527 | return $itemIsInQueue; |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Checks whether the Index Queue contains a specific item. |
||
532 | * |
||
533 | * @param string $itemType The item's type, usually a table name. |
||
534 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
535 | * different value for non-database-record types. |
||
536 | * @param integer $rootPageId |
||
537 | 3 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
|
538 | */ |
||
539 | 3 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
551 | |||
552 | /** |
||
553 | * Checks whether the Index Queue contains a specific item that has been |
||
554 | * marked as indexed. |
||
555 | * |
||
556 | * @param string $itemType The item's type, usually a table name. |
||
557 | 26 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
|
558 | * different value for non-database-record types. |
||
559 | 26 | * @return bool TRUE if the item is found in the queue and marked as |
|
560 | * indexed, FALSE otherwise |
||
561 | */ |
||
562 | 26 | public function containsIndexedItem($itemType, $itemUid) |
|
575 | 9 | ||
576 | 9 | /** |
|
577 | 9 | * Removes an item from the Index Queue. |
|
578 | * |
||
579 | 9 | * @param string $itemType The type of the item to remove, usually a table name. |
|
580 | 9 | * @param int $itemUid The uid of the item to remove |
|
581 | 9 | */ |
|
582 | public function deleteItem($itemType, $itemUid) |
||
610 | 1 | ||
611 | 1 | /** |
|
612 | 1 | * Removes all items of a certain type from the Index Queue. |
|
613 | * |
||
614 | 1 | * @param string $itemType The type of items to remove, usually a table name. |
|
615 | 1 | */ |
|
616 | 1 | public function deleteItemsByType($itemType) |
|
645 | 5 | ||
646 | 5 | /** |
|
647 | 5 | * Removes all items of a certain site from the Index Queue. Accepts an |
|
648 | * optional parameter to limit the deleted items by indexing configuration. |
||
649 | 5 | * |
|
650 | * @param Site $site The site to remove items for. |
||
651 | * @param string $indexingConfigurationName Name of a specific indexing |
||
652 | * configuration |
||
653 | */ |
||
654 | public function deleteItemsBySite( |
||
704 | |||
705 | /** |
||
706 | 11 | * Removes all items from the Index Queue. |
|
707 | 11 | * |
|
708 | */ |
||
709 | 11 | public function deleteAllItems() |
|
713 | |||
714 | /** |
||
715 | 11 | * Gets a single Index Queue item by its uid. |
|
716 | * |
||
717 | * @param int $itemId Index Queue item uid |
||
718 | * @return Item The request Index Queue item or NULL |
||
719 | * if no item with $itemId was found |
||
720 | */ |
||
721 | public function getItem($itemId) |
||
742 | |||
743 | /** |
||
744 | * Gets Index Queue items by type and uid. |
||
745 | * |
||
746 | * @param string $itemType item type, usually the table name |
||
747 | 2 | * @param int $itemUid item uid |
|
748 | * @return Item[] An array of items matching $itemType and $itemUid |
||
749 | 2 | */ |
|
750 | 2 | public function getItems($itemType, $itemUid) |
|
762 | |||
763 | 2 | /** |
|
764 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
765 | 2 | * optional parameter to limit the counted items by indexing configuration. |
|
766 | 2 | * |
|
767 | 2 | * @param Site $site The site to search for. |
|
768 | * @param string $indexingConfigurationName name of a specific indexing |
||
769 | * configuration |
||
770 | * @return int Number of items |
||
771 | */ |
||
772 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
778 | |||
779 | /** |
||
780 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
781 | * optional parameter to limit the counted items by indexing configuration. |
||
782 | * |
||
783 | * @param Site $site The site to search for. |
||
784 | 49 | * @param string $indexingConfigurationName name of a specific indexing |
|
785 | * configuration |
||
786 | * @return int Number of items. |
||
787 | 49 | */ |
|
788 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
||
794 | |||
795 | /** |
||
796 | * Returns the number of items for all queues. |
||
797 | * |
||
798 | 3 | * @return int |
|
799 | */ |
||
800 | 3 | public function getAllItemsCount() |
|
804 | |||
805 | 3 | /** |
|
806 | * @param string $where |
||
807 | * @return int |
||
808 | */ |
||
809 | private function getItemCount($where = '1=1') |
||
816 | |||
817 | 7 | /** |
|
818 | * Build a database constraint that limits to a certain indexConfigurationName |
||
819 | * |
||
820 | 7 | * @param string $indexingConfigurationName |
|
821 | 7 | * @return string |
|
822 | 7 | */ |
|
823 | 7 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
832 | |||
833 | 7 | /** |
|
834 | * Gets $limit number of items to index for a particular $site. |
||
835 | * |
||
836 | 7 | * @param Site $site TYPO3 site |
|
837 | * @param int $limit Number of items to get from the queue |
||
838 | * @return Item[] Items to index to the given solr server |
||
839 | */ |
||
840 | public function getItemsToIndex(Site $site, $limit = 50) |
||
863 | |||
864 | 25 | /** |
|
865 | 25 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
|
866 | 25 | * index queue records. |
|
867 | * |
||
868 | 25 | * @param array $indexQueueItemRecords Array of plain index queue records |
|
869 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
870 | 25 | */ |
|
871 | protected function getIndexQueueItemObjectsFromRecords( |
||
923 | |||
924 | /** |
||
925 | * Marks an item as failed and causes the indexer to skip the item in the |
||
926 | * next run. |
||
927 | * |
||
928 | * @param int|Item $item Either the item's Index Queue |
||
929 | * uid or the complete item |
||
930 | * @param string $errorMessage Error message |
||
931 | */ |
||
932 | public function markItemAsFailed($item, $errorMessage = '') |
||
953 | } |
||
954 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.