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 | 94 | 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) |
|
84 | { |
||
85 | 2 | $lastIndexTime = 0; |
|
86 | |||
87 | 2 | $lastIndexedRow = $this->getLastIndexedRow($rootPageId); |
|
88 | |||
89 | 2 | if ($lastIndexedRow[0]['indexed']) { |
|
90 | 1 | $lastIndexTime = $lastIndexedRow[0]['indexed']; |
|
91 | } |
||
92 | |||
93 | 2 | return $lastIndexTime; |
|
94 | } |
||
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) |
|
104 | { |
||
105 | 3 | $lastIndexedItemId = 0; |
|
106 | |||
107 | 3 | $lastIndexedItemRow = $this->getLastIndexedRow($rootPageId); |
|
108 | 3 | if ($lastIndexedItemRow[0]['uid']) { |
|
109 | 2 | $lastIndexedItemId = $lastIndexedItemRow[0]['uid']; |
|
110 | } |
||
111 | |||
112 | 3 | return $lastIndexedItemId; |
|
113 | } |
||
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 = '') |
|
151 | { |
||
152 | 6 | $indexingConfigurations = []; |
|
153 | 6 | $initializationStatus = []; |
|
154 | |||
155 | 6 | if (empty($indexingConfigurationName)) { |
|
156 | 1 | $solrConfiguration = $site->getSolrConfiguration(); |
|
157 | 1 | $indexingConfigurations = $solrConfiguration->getEnabledIndexQueueConfigurationNames(); |
|
158 | } else { |
||
159 | 5 | $indexingConfigurations[] = $indexingConfigurationName; |
|
160 | } |
||
161 | |||
162 | 6 | foreach ($indexingConfigurations as $indexingConfigurationName) { |
|
163 | 6 | $initializationStatus[$indexingConfigurationName] = $this->initializeIndexingConfiguration( |
|
164 | $site, |
||
165 | $indexingConfigurationName |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | 6 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'])) { |
|
170 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'] as $classReference) { |
||
171 | $indexQueueInitializationPostProcessor = GeneralUtility::getUserObj($classReference); |
||
172 | |||
173 | if ($indexQueueInitializationPostProcessor instanceof InitializationPostProcessor) { |
||
174 | $indexQueueInitializationPostProcessor->postProcessIndexQueueInitialization( |
||
175 | $site, |
||
176 | $indexingConfigurations, |
||
177 | $initializationStatus |
||
178 | ); |
||
179 | } else { |
||
180 | throw new \UnexpectedValueException( |
||
181 | get_class($indexQueueInitializationPostProcessor) . |
||
182 | ' must implement interface ' . InitializationPostProcessor::class, |
||
183 | 1345815561 |
||
184 | ); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | 6 | return $initializationStatus; |
|
190 | } |
||
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 | * Marks an item as needing (re)indexing. |
||
226 | * |
||
227 | * Like with Solr itself, there's no add method, just a simple update method |
||
228 | * that handles the adds, too. |
||
229 | * |
||
230 | * The method creates or updates the index queue items for all related rootPageIds. |
||
231 | * |
||
232 | * @param string $itemType The item's type, usually a table name. |
||
233 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
234 | * different value for non-database-record types. |
||
235 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
236 | * value from getItemChangedTime() is used. |
||
237 | */ |
||
238 | 50 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
259 | |||
260 | /** |
||
261 | * Finds indexing errors for the current site |
||
262 | * |
||
263 | * @param Site $site |
||
264 | * @return array Error items for the current site's Index Queue |
||
265 | */ |
||
266 | public function getErrorsBySite(Site $site) |
||
274 | |||
275 | /** |
||
276 | * Resets all the errors for all index queue items. |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function resetAllErrors() |
||
288 | |||
289 | /** |
||
290 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
291 | * |
||
292 | * @param string $itemType The item's type, usually a table name. |
||
293 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
294 | * different value for non-database-record types. |
||
295 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
296 | * @param int $rootPageId The uid of the rootPage |
||
297 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
298 | */ |
||
299 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
316 | |||
317 | /** |
||
318 | * Adds an item to the index queue. |
||
319 | * |
||
320 | * Not meant for public use. |
||
321 | * |
||
322 | * @param string $itemType The item's type, usually a table name. |
||
323 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
324 | * different value for non-database-record types. |
||
325 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
326 | * Optional, overwrites existing / determined configuration. |
||
327 | * @return void |
||
328 | */ |
||
329 | 43 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
354 | |||
355 | /** |
||
356 | * Get record to be added in addNewItem |
||
357 | * |
||
358 | * @param string $itemType The item's type, usually a table name. |
||
359 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
360 | * different value for non-database-record types. |
||
361 | * @param string $additionalRecordFields for sql-query |
||
362 | * |
||
363 | * @return array|NULL |
||
364 | */ |
||
365 | 43 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
|
378 | |||
379 | /** |
||
380 | * Determines the time for when an item should be indexed. This timestamp |
||
381 | * is then stored in the changed column in the Index Queue. |
||
382 | * |
||
383 | * The changed timestamp usually is now - time(). For records which are set |
||
384 | * to published at a later time, this timestamp is the start time. So if a |
||
385 | * future start time has been set, that will be used to delay indexing |
||
386 | * of an item. |
||
387 | * |
||
388 | * @param string $itemType The item's table name. |
||
389 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
390 | * different value for non-database-record types. |
||
391 | * @return int Timestamp of the item's changed time or future start time |
||
392 | */ |
||
393 | 48 | protected function getItemChangedTime($itemType, $itemUid) |
|
438 | |||
439 | /** |
||
440 | * Gets the most recent changed time of a page's content elements |
||
441 | * |
||
442 | * @param array $page Partial page record |
||
443 | * @return int Timestamp of the most recent content element change |
||
444 | */ |
||
445 | 35 | protected function getPageItemChangedTime(array $page) |
|
461 | |||
462 | /** |
||
463 | * Gets the most recent changed time for an item taking into account |
||
464 | * localized records. |
||
465 | * |
||
466 | * @param string $itemType The item's type, usually a table name. |
||
467 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
468 | * different value for non-database-record types. |
||
469 | * @return int Timestamp of the most recent content element change |
||
470 | */ |
||
471 | 48 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
490 | |||
491 | /** |
||
492 | * Checks whether the Index Queue contains a specific item. |
||
493 | * |
||
494 | * @param string $itemType The item's type, usually a table name. |
||
495 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
496 | * different value for non-database-record types. |
||
497 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
498 | */ |
||
499 | 6 | public function containsItem($itemType, $itemUid) |
|
511 | |||
512 | /** |
||
513 | * Checks whether the Index Queue contains a specific item. |
||
514 | * |
||
515 | * @param string $itemType The item's type, usually a table name. |
||
516 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
517 | * different value for non-database-record types. |
||
518 | * @param integer $rootPageId |
||
519 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
520 | */ |
||
521 | 49 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
533 | |||
534 | /** |
||
535 | * Checks whether the Index Queue contains a specific item that has been |
||
536 | * marked as indexed. |
||
537 | * |
||
538 | * @param string $itemType The item's type, usually a table name. |
||
539 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
540 | * different value for non-database-record types. |
||
541 | * @return bool TRUE if the item is found in the queue and marked as |
||
542 | * indexed, FALSE otherwise |
||
543 | */ |
||
544 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
557 | |||
558 | /** |
||
559 | * Removes an item from the Index Queue. |
||
560 | * |
||
561 | * @param string $itemType The type of the item to remove, usually a table name. |
||
562 | * @param int $itemUid The uid of the item to remove |
||
563 | */ |
||
564 | 29 | public function deleteItem($itemType, $itemUid) |
|
592 | |||
593 | /** |
||
594 | * Removes all items of a certain type from the Index Queue. |
||
595 | * |
||
596 | * @param string $itemType The type of items to remove, usually a table name. |
||
597 | */ |
||
598 | 1 | public function deleteItemsByType($itemType) |
|
627 | |||
628 | /** |
||
629 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
630 | * optional parameter to limit the deleted items by indexing configuration. |
||
631 | * |
||
632 | * @param Site $site The site to remove items for. |
||
633 | * @param string $indexingConfigurationName Name of a specific indexing |
||
634 | * configuration |
||
635 | */ |
||
636 | 6 | public function deleteItemsBySite( |
|
686 | |||
687 | /** |
||
688 | * Removes all items from the Index Queue. |
||
689 | * |
||
690 | */ |
||
691 | 1 | public function deleteAllItems() |
|
695 | |||
696 | /** |
||
697 | * Gets a single Index Queue item by its uid. |
||
698 | * |
||
699 | * @param int $itemId Index Queue item uid |
||
700 | * @return Item The request Index Queue item or NULL |
||
701 | * if no item with $itemId was found |
||
702 | */ |
||
703 | 21 | public function getItem($itemId) |
|
723 | |||
724 | /** |
||
725 | * Gets Index Queue items by type and uid. |
||
726 | * |
||
727 | * @param string $itemType item type, usually the table name |
||
728 | * @param int $itemUid item uid |
||
729 | * @return Item[] An array of items matching $itemType and $itemUid |
||
730 | */ |
||
731 | 25 | public function getItems($itemType, $itemUid) |
|
736 | |||
737 | /** |
||
738 | * Returns all items in the queue. |
||
739 | * |
||
740 | * @return Item[] An array of items matching $itemType and $itemUid |
||
741 | */ |
||
742 | public function getAllItems() |
||
746 | |||
747 | /** |
||
748 | * Returns a collection of items by whereClause. |
||
749 | * |
||
750 | * @param string $whereClause |
||
751 | * @return array |
||
752 | */ |
||
753 | 25 | protected function getItemsByWhereClause($whereClause = '1=1') |
|
754 | { |
||
755 | 25 | $indexQueueItemRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
756 | 25 | '*', |
|
757 | 25 | 'tx_solr_indexqueue_item', |
|
758 | $whereClause |
||
759 | |||
760 | ); |
||
761 | |||
762 | 25 | return $this->getIndexQueueItemObjectsFromRecords($indexQueueItemRecords); |
|
763 | } |
||
764 | |||
765 | /** |
||
766 | * Returns the number of items for all queues. |
||
767 | * |
||
768 | * @return int |
||
769 | */ |
||
770 | 67 | public function getAllItemsCount() |
|
774 | |||
775 | /** |
||
776 | * @param string $where |
||
777 | * @return int |
||
778 | */ |
||
779 | 67 | private function getItemCount($where = '1=1') |
|
786 | |||
787 | /** |
||
788 | * Extracts the number of pending, indexed and erroneous items from the |
||
789 | * Index Queue. |
||
790 | * |
||
791 | * @param Site $site |
||
792 | * @param string $indexingConfigurationName |
||
793 | * |
||
794 | * @return QueueStatistic |
||
795 | */ |
||
796 | 5 | public function getStatisticsBySite(Site $site, $indexingConfigurationName = '') |
|
797 | { |
||
798 | 5 | $indexingConfigurationConstraint = $this->buildIndexConfigurationConstraint($indexingConfigurationName); |
|
799 | 5 | $where = 'root = ' . (int)$site->getRootPageId() . $indexingConfigurationConstraint; |
|
800 | |||
801 | 5 | $indexQueueStats = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
802 | 'indexed < changed as pending,' |
||
803 | . '(errors not like "") as failed,' |
||
804 | 5 | . 'COUNT(*) as count', |
|
805 | 5 | 'tx_solr_indexqueue_item', |
|
806 | $where, |
||
807 | 5 | 'pending, failed' |
|
808 | ); |
||
809 | /** @var $statistic QueueStatistic */ |
||
810 | 5 | $statistic = GeneralUtility::makeInstance(QueueStatistic::class); |
|
811 | |||
812 | 5 | foreach ($indexQueueStats as $row) { |
|
813 | 5 | if ($row['failed'] == 1) { |
|
814 | 1 | $statistic->setFailedCount((int)$row['count']); |
|
815 | 5 | } elseif ($row['pending'] == 1) { |
|
816 | 5 | $statistic->setPendingCount((int)$row['count']); |
|
817 | } else { |
||
818 | 5 | $statistic->setSuccessCount((int)$row['count']); |
|
819 | } |
||
820 | } |
||
821 | |||
822 | 5 | return $statistic; |
|
823 | } |
||
824 | |||
825 | /** |
||
826 | * Build a database constraint that limits to a certain indexConfigurationName |
||
827 | * |
||
828 | * @param string $indexingConfigurationName |
||
829 | * @return string |
||
830 | */ |
||
831 | 5 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
840 | |||
841 | /** |
||
842 | * Gets $limit number of items to index for a particular $site. |
||
843 | * |
||
844 | * @param Site $site TYPO3 site |
||
845 | * @param int $limit Number of items to get from the queue |
||
846 | * @return Item[] Items to index to the given solr server |
||
847 | */ |
||
848 | 7 | public function getItemsToIndex(Site $site, $limit = 50) |
|
871 | |||
872 | /** |
||
873 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
874 | * index queue records. |
||
875 | * |
||
876 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
877 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
878 | */ |
||
879 | 30 | protected function getIndexQueueItemObjectsFromRecords( |
|
880 | array $indexQueueItemRecords |
||
881 | ) { |
||
882 | 30 | $indexQueueItems = []; |
|
883 | 30 | $tableUids = []; |
|
884 | 30 | $tableRecords = []; |
|
885 | |||
886 | // grouping records by table |
||
887 | 30 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
|
888 | 30 | $tableUids[$indexQueueItemRecord['item_type']][] = $indexQueueItemRecord['item_uid']; |
|
889 | } |
||
890 | |||
891 | // fetching records by table, saves us a lot of single queries |
||
892 | 30 | foreach ($tableUids as $table => $uids) { |
|
893 | 30 | $uidList = implode(',', $uids); |
|
894 | 30 | $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
895 | 30 | '*', |
|
896 | $table, |
||
897 | 30 | 'uid IN(' . $uidList . ')', |
|
898 | 30 | '', '', '', // group, order, limit |
|
899 | 30 | 'uid' |
|
900 | ); |
||
901 | 30 | $tableRecords[$table] = $records; |
|
902 | |||
903 | 30 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'])) { |
|
904 | $params = ['table' => $table, 'uids' => $uids, 'tableRecords' => &$tableRecords]; |
||
905 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessFetchRecordsForIndexQueueItem'] as $reference) { |
||
906 | GeneralUtility::callUserFunction($reference, $params, $this); |
||
907 | } |
||
908 | 30 | unset($params); |
|
909 | } |
||
910 | } |
||
911 | |||
912 | // creating index queue item objects and assigning / mapping |
||
913 | // records to index queue items |
||
914 | 30 | foreach ($indexQueueItemRecords as $indexQueueItemRecord) { |
|
915 | 30 | if (isset($tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']])) { |
|
916 | 29 | $indexQueueItems[] = GeneralUtility::makeInstance( |
|
917 | 29 | Item::class, |
|
918 | $indexQueueItemRecord, |
||
919 | 29 | $tableRecords[$indexQueueItemRecord['item_type']][$indexQueueItemRecord['item_uid']] |
|
920 | ); |
||
921 | } else { |
||
922 | 1 | $this->logger->log( |
|
923 | 1 | SolrLogManager::ERROR, |
|
924 | 1 | 'Record missing for Index Queue item. Item removed.', |
|
925 | [ |
||
926 | 1 | $indexQueueItemRecord |
|
927 | ] |
||
928 | ); |
||
929 | 1 | $this->deleteItem($indexQueueItemRecord['item_type'], |
|
930 | 30 | $indexQueueItemRecord['item_uid']); |
|
931 | } |
||
932 | } |
||
933 | |||
934 | 30 | return $indexQueueItems; |
|
935 | } |
||
936 | |||
937 | /** |
||
938 | * Marks an item as failed and causes the indexer to skip the item in the |
||
939 | * next run. |
||
940 | * |
||
941 | * @param int|Item $item Either the item's Index Queue uid or the complete item |
||
942 | * @param string $errorMessage Error message |
||
943 | */ |
||
944 | 6 | public function markItemAsFailed($item, $errorMessage = '') |
|
965 | |||
966 | /** |
||
967 | * Sets the timestamp of when an item last has been indexed. |
||
968 | * |
||
969 | * @param Item $item |
||
970 | */ |
||
971 | 6 | public function updateIndexTimeByItem(Item $item) |
|
979 | } |
||
980 |