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 |
||
43 | class Queue |
||
44 | { |
||
45 | /** |
||
46 | * @var RootPageResolver |
||
47 | */ |
||
48 | protected $rootPageResolver; |
||
49 | |||
50 | /** |
||
51 | * @var ConfigurationAwareRecordService |
||
52 | */ |
||
53 | protected $recordService; |
||
54 | |||
55 | /** |
||
56 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
57 | */ |
||
58 | protected $logger = null; |
||
59 | |||
60 | /** |
||
61 | * Queue constructor. |
||
62 | * @param RootPageResolver|null $rootPageResolver |
||
63 | * @param ConfigurationAwareRecordService|null $recordService |
||
64 | */ |
||
65 | 68 | public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null) |
|
66 | { |
||
67 | 68 | $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
68 | 68 | $this->rootPageResolver = isset($rootPageResolver) ? $rootPageResolver : GeneralUtility::makeInstance(RootPageResolver::class); |
|
69 | 68 | $this->recordService = isset($recordService) ? $recordService : GeneralUtility::makeInstance(ConfigurationAwareRecordService::class); |
|
70 | 68 | } |
|
71 | |||
72 | // FIXME some of the methods should be renamed to plural forms |
||
73 | // FIXME singular form methods should deal with exactly one item only |
||
74 | |||
75 | /** |
||
76 | * Returns the timestamp of the last indexing run. |
||
77 | * |
||
78 | * @param int $rootPageId The root page uid for which to get |
||
79 | * the last indexed item id |
||
80 | * @return int Timestamp of last index run. |
||
81 | */ |
||
82 | public function getLastIndexTime($rootPageId) |
||
83 | { |
||
84 | $lastIndexTime = 0; |
||
85 | |||
86 | $lastIndexedRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
87 | 'indexed', |
||
88 | 'tx_solr_indexqueue_item', |
||
89 | 'root = ' . (int)$rootPageId, |
||
90 | '', |
||
91 | 'indexed DESC', |
||
92 | 1 |
||
93 | ); |
||
94 | |||
95 | if ($lastIndexedRow[0]['indexed']) { |
||
96 | $lastIndexTime = $lastIndexedRow[0]['indexed']; |
||
97 | } |
||
98 | |||
99 | return $lastIndexTime; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Returns the uid of the last indexed item in the queue |
||
104 | * |
||
105 | * @param int $rootPageId The root page uid for which to get |
||
106 | * the last indexed item id |
||
107 | * @return int The last indexed item's ID. |
||
108 | */ |
||
109 | public function getLastIndexedItemId($rootPageId) |
||
110 | { |
||
111 | $lastIndexedItemId = 0; |
||
112 | |||
113 | $lastIndexedItemRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
114 | 'uid', |
||
115 | 'tx_solr_indexqueue_item', |
||
116 | 'root = ' . (int)$rootPageId, |
||
117 | '', |
||
118 | 'indexed DESC', |
||
119 | 1 |
||
120 | ); |
||
121 | if ($lastIndexedItemRow[0]['uid']) { |
||
122 | $lastIndexedItemId = $lastIndexedItemRow[0]['uid']; |
||
123 | } |
||
124 | |||
125 | return $lastIndexedItemId; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
||
130 | * complete way to force reindexing, or to build the Index Queue for the |
||
131 | * first time. The Index Queue initialization is site-specific. |
||
132 | * |
||
133 | * @param Site $site The site to initialize |
||
134 | * @param string $indexingConfigurationName Name of a specific |
||
135 | * indexing configuration |
||
136 | * @return array An array of booleans, each representing whether the |
||
137 | * initialization for an indexing configuration was successful |
||
138 | */ |
||
139 | 6 | public function initialize(Site $site, $indexingConfigurationName = '') |
|
140 | { |
||
141 | 6 | $indexingConfigurations = []; |
|
142 | 6 | $initializationStatus = []; |
|
143 | |||
144 | 6 | if (empty($indexingConfigurationName)) { |
|
145 | 1 | $solrConfiguration = $site->getSolrConfiguration(); |
|
146 | 1 | $indexingConfigurations = $solrConfiguration->getEnabledIndexQueueConfigurationNames(); |
|
147 | } else { |
||
148 | 5 | $indexingConfigurations[] = $indexingConfigurationName; |
|
149 | } |
||
150 | |||
151 | 6 | foreach ($indexingConfigurations as $indexingConfigurationName) { |
|
152 | 6 | $initializationStatus[$indexingConfigurationName] = $this->initializeIndexingConfiguration( |
|
153 | $site, |
||
154 | $indexingConfigurationName |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | 6 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'])) { |
|
159 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'] as $classReference) { |
||
160 | $indexQueueInitializationPostProcessor = GeneralUtility::getUserObj($classReference); |
||
161 | |||
162 | if ($indexQueueInitializationPostProcessor instanceof InitializationPostProcessor) { |
||
163 | $indexQueueInitializationPostProcessor->postProcessIndexQueueInitialization( |
||
164 | $site, |
||
165 | $indexingConfigurations, |
||
166 | $initializationStatus |
||
167 | ); |
||
168 | } else { |
||
169 | throw new \UnexpectedValueException( |
||
170 | get_class($indexQueueInitializationPostProcessor) . |
||
171 | ' must implement interface ' . InitializationPostProcessor::class, |
||
172 | 1345815561 |
||
173 | ); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | 6 | return $initializationStatus; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Initializes the Index Queue for a specific indexing configuration. |
||
183 | * |
||
184 | * @param Site $site The site to initialize |
||
185 | * @param string $indexingConfigurationName name of a specific |
||
186 | * indexing configuration |
||
187 | * @return bool TRUE if the initialization was successful, FALSE otherwise |
||
188 | */ |
||
189 | 6 | protected function initializeIndexingConfiguration( |
|
190 | Site $site, |
||
191 | $indexingConfigurationName |
||
192 | ) { |
||
193 | // clear queue |
||
194 | 6 | $this->deleteItemsBySite($site, $indexingConfigurationName); |
|
195 | |||
196 | 6 | $solrConfiguration = $site->getSolrConfiguration(); |
|
197 | |||
198 | 6 | $tableToIndex = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName); |
|
199 | 6 | $initializerClass = $solrConfiguration->getIndexQueueInitializerClassByConfigurationName($indexingConfigurationName); |
|
200 | |||
201 | 6 | $initializer = GeneralUtility::makeInstance($initializerClass); |
|
202 | /** @var $initializer \ApacheSolrForTypo3\Solr\IndexQueue\Initializer\AbstractInitializer */ |
||
203 | 6 | $initializer->setSite($site); |
|
204 | 6 | $initializer->setType($tableToIndex); |
|
205 | 6 | $initializer->setIndexingConfigurationName($indexingConfigurationName); |
|
206 | |||
207 | 6 | $indexConfiguration = $solrConfiguration->getIndexQueueConfigurationByName($indexingConfigurationName); |
|
208 | 6 | $initializer->setIndexingConfiguration($indexConfiguration); |
|
209 | |||
210 | 6 | return $initializer->initialize(); |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Gets the indexing configuration to use for an item. |
||
215 | * Sometimes, when there are multiple configurations for a certain item type |
||
216 | * (table) it can be hard or even impossible to find which one to use |
||
217 | * though. |
||
218 | * Currently selects the first indexing configuration where the name matches |
||
219 | * the itemType or where the configured tbale is the same as the itemType. |
||
220 | * |
||
221 | * !!! Might return incorrect results for complex configurations !!! |
||
222 | * Try to set the indexingConfiguration directly when using the updateItem() |
||
223 | * method in such situations. |
||
224 | * |
||
225 | * @param string $itemType The item's type, usually a table name. |
||
226 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
227 | * different value for non-database-record types. |
||
228 | * @param int $rootPageId The configuration's page tree's root page id. |
||
229 | * Optional, not needed for all types. |
||
230 | * @return string The indexing configuration's name to use when indexing |
||
231 | * @deprecated since 6.1 will be removed in 7.0 |
||
232 | * Use getIndexingConfigurationsByItem() now, which behaves |
||
233 | * almost the same way but returns an array of configurations |
||
234 | */ |
||
235 | protected function getIndexingConfigurationByItem( |
||
236 | $itemType, |
||
237 | $itemUid, |
||
|
|||
238 | $rootPageId = null |
||
239 | ) { |
||
240 | GeneralUtility::logDeprecatedFunction(); |
||
241 | $indexingConfigurationName = ''; |
||
242 | |||
243 | $configurations = $this->getIndexingConfigurationsByItem($itemType, $rootPageId); |
||
244 | if (count($configurations) > 0) { |
||
245 | $indexingConfigurationName = $configurations[0]; |
||
246 | } |
||
247 | |||
248 | return $indexingConfigurationName; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Gets the indexing configurations to use for an item. |
||
253 | * Multiple configurations for a certain item type (table) might be available. |
||
254 | * |
||
255 | * @param string $itemType The item's type, usually a table name. |
||
256 | * @param int $rootPageId The configuration's page tree's root page id. |
||
257 | * Optional, not needed for all types. |
||
258 | * @return array<string> The indexing configurations names to use when indexing |
||
259 | * @deprecated since 6.1 will be removed in 7.0 |
||
260 | */ |
||
261 | protected function getIndexingConfigurationsByItem( |
||
262 | $itemType, |
||
263 | $rootPageId = null |
||
264 | ) { |
||
265 | GeneralUtility::logDeprecatedFunction(); |
||
266 | |||
267 | $possibleIndexingConfigurationNames = []; |
||
268 | |||
269 | if (!is_null($rootPageId)) { |
||
270 | // get configuration for the root's branch |
||
271 | $solrConfiguration = Util::getSolrConfigurationFromPageId($rootPageId); |
||
272 | $possibleIndexingConfigurationNames = $solrConfiguration->getIndexQueueConfigurationNamesByTableName($itemType); |
||
273 | } |
||
274 | |||
275 | return $possibleIndexingConfigurationNames; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Marks an item as needing (re)indexing. |
||
280 | * |
||
281 | * Like with Solr itself, there's no add method, just a simple update method |
||
282 | * that handles the adds, too. |
||
283 | * |
||
284 | * The method creates or updates the index queue items for all related rootPageIds. |
||
285 | * |
||
286 | * @param string $itemType The item's type, usually a table name. |
||
287 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
288 | * different value for non-database-record types. |
||
289 | * @param int $forcedChangeTime The change time for the item if set, otherwise |
||
290 | * value from getItemChangedTime() is used. |
||
291 | */ |
||
292 | 47 | public function updateItem($itemType, $itemUid, $forcedChangeTime = 0) |
|
293 | { |
||
294 | 47 | $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, $itemUid); |
|
295 | 46 | foreach ($rootPageIds as $rootPageId) { |
|
296 | 46 | $skipInvalidRootPage = $rootPageId === 0; |
|
297 | 46 | if ($skipInvalidRootPage) { |
|
298 | continue; |
||
299 | } |
||
300 | |||
301 | 46 | $solrConfiguration = Util::getSolrConfigurationFromPageId($rootPageId); |
|
302 | 46 | $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, $itemUid, $solrConfiguration); |
|
303 | 46 | $itemInQueueForRootPage = $this->containsItemWithRootPageId($itemType, $itemUid, $rootPageId); |
|
304 | 46 | if ($itemInQueueForRootPage) { |
|
305 | // update the existing queue item |
||
306 | 10 | $this->updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime); |
|
307 | } else { |
||
308 | // add the item since it's not in the queue yet |
||
309 | 46 | $this->addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId); |
|
310 | } |
||
311 | } |
||
312 | 46 | } |
|
313 | |||
314 | /** |
||
315 | * Updates an existing queue entry by $itemType $itemUid and $rootPageId. |
||
316 | * |
||
317 | * @param string $itemType The item's type, usually a table name. |
||
318 | * @param int $itemUid The item's uid, usually an integer uid, could be a |
||
319 | * different value for non-database-record types. |
||
320 | * @param string $indexingConfiguration The name of the related indexConfiguration |
||
321 | * @param int $rootPageId The uid of the rootPage |
||
322 | * @param int $forcedChangeTime The forced change time that should be used for updating |
||
323 | */ |
||
324 | 10 | protected function updateExistingItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $forcedChangeTime) |
|
341 | |||
342 | /** |
||
343 | * Adds an item to the index queue. |
||
344 | * |
||
345 | * Not meant for public use. |
||
346 | * |
||
347 | * @param string $itemType The item's type, usually a table name. |
||
348 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
349 | * different value for non-database-record types. |
||
350 | * @param string $indexingConfiguration The item's indexing configuration to use. |
||
351 | * Optional, overwrites existing / determined configuration. |
||
352 | * @return void |
||
353 | */ |
||
354 | 40 | private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId) |
|
379 | |||
380 | /** |
||
381 | * Get record to be added in addNewItem |
||
382 | * |
||
383 | * @param string $itemType The item's type, usually a table name. |
||
384 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
385 | * different value for non-database-record types. |
||
386 | * @param string $additionalRecordFields for sql-query |
||
387 | * |
||
388 | * @return array|NULL |
||
389 | */ |
||
390 | 40 | protected function getRecordCached($itemType, $itemUid, $additionalRecordFields) |
|
403 | |||
404 | /** |
||
405 | * Determines the time for when an item should be indexed. This timestamp |
||
406 | * is then stored in the changed column in the Index Queue. |
||
407 | * |
||
408 | * The changed timestamp usually is now - time(). For records which are set |
||
409 | * to published at a later time, this timestamp is the start time. So if a |
||
410 | * future start time has been set, that will be used to delay indexing |
||
411 | * of an item. |
||
412 | * |
||
413 | * @param string $itemType The item's table name. |
||
414 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
415 | * different value for non-database-record types. |
||
416 | * @return int Timestamp of the item's changed time or future start time |
||
417 | */ |
||
418 | 45 | protected function getItemChangedTime($itemType, $itemUid) |
|
463 | |||
464 | /** |
||
465 | * Gets the most recent changed time of a page's content elements |
||
466 | * |
||
467 | * @param array $page Partial page record |
||
468 | * @return int Timestamp of the most recent content element change |
||
469 | */ |
||
470 | 32 | protected function getPageItemChangedTime(array $page) |
|
486 | |||
487 | /** |
||
488 | * Gets the most recent changed time for an item taking into account |
||
489 | * localized records. |
||
490 | * |
||
491 | * @param string $itemType The item's type, usually a table name. |
||
492 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
493 | * different value for non-database-record types. |
||
494 | * @return int Timestamp of the most recent content element change |
||
495 | */ |
||
496 | 45 | protected function getLocalizableItemChangedTime($itemType, $itemUid) |
|
515 | |||
516 | /** |
||
517 | * Checks whether the Index Queue contains a specific item. |
||
518 | * |
||
519 | * @param string $itemType The item's type, usually a table name. |
||
520 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
521 | * different value for non-database-record types. |
||
522 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
523 | */ |
||
524 | 3 | public function containsItem($itemType, $itemUid) |
|
536 | |||
537 | /** |
||
538 | * Checks whether the Index Queue contains a specific item. |
||
539 | * |
||
540 | * @param string $itemType The item's type, usually a table name. |
||
541 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
542 | * different value for non-database-record types. |
||
543 | * @param integer $rootPageId |
||
544 | * @return bool TRUE if the item is found in the queue, FALSE otherwise |
||
545 | */ |
||
546 | 46 | public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId) |
|
558 | |||
559 | /** |
||
560 | * Checks whether the Index Queue contains a specific item that has been |
||
561 | * marked as indexed. |
||
562 | * |
||
563 | * @param string $itemType The item's type, usually a table name. |
||
564 | * @param string $itemUid The item's uid, usually an integer uid, could be a |
||
565 | * different value for non-database-record types. |
||
566 | * @return bool TRUE if the item is found in the queue and marked as |
||
567 | * indexed, FALSE otherwise |
||
568 | */ |
||
569 | 3 | public function containsIndexedItem($itemType, $itemUid) |
|
582 | |||
583 | /** |
||
584 | * Removes an item from the Index Queue. |
||
585 | * |
||
586 | * @param string $itemType The type of the item to remove, usually a table name. |
||
587 | * @param int $itemUid The uid of the item to remove |
||
588 | */ |
||
589 | 26 | public function deleteItem($itemType, $itemUid) |
|
617 | |||
618 | /** |
||
619 | * Removes all items of a certain type from the Index Queue. |
||
620 | * |
||
621 | * @param string $itemType The type of items to remove, usually a table name. |
||
622 | */ |
||
623 | 1 | public function deleteItemsByType($itemType) |
|
652 | |||
653 | /** |
||
654 | * Removes all items of a certain site from the Index Queue. Accepts an |
||
655 | * optional parameter to limit the deleted items by indexing configuration. |
||
656 | * |
||
657 | * @param Site $site The site to remove items for. |
||
658 | * @param string $indexingConfigurationName Name of a specific indexing |
||
659 | * configuration |
||
660 | */ |
||
661 | 6 | public function deleteItemsBySite( |
|
711 | |||
712 | /** |
||
713 | * Removes all items from the Index Queue. |
||
714 | * |
||
715 | */ |
||
716 | 1 | public function deleteAllItems() |
|
717 | { |
||
718 | 1 | $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('tx_solr_indexqueue_item', ''); |
|
719 | 1 | } |
|
720 | |||
721 | /** |
||
722 | * Gets a single Index Queue item by its uid. |
||
723 | * |
||
724 | * @param int $itemId Index Queue item uid |
||
725 | * @return Item The request Index Queue item or NULL |
||
726 | * if no item with $itemId was found |
||
727 | */ |
||
728 | 11 | public function getItem($itemId) |
|
749 | |||
750 | /** |
||
751 | * Gets Index Queue items by type and uid. |
||
752 | * |
||
753 | * @param string $itemType item type, usually the table name |
||
754 | * @param int $itemUid item uid |
||
755 | * @return Item[] An array of items matching $itemType and $itemUid |
||
756 | */ |
||
757 | 21 | public function getItems($itemType, $itemUid) |
|
769 | |||
770 | /** |
||
771 | * Gets number of Index Queue items for a specific site / indexing configuration |
||
772 | * optional parameter to limit the counted items by indexing configuration. |
||
773 | * |
||
774 | * @param Site $site The site to search for. |
||
775 | * @param string $indexingConfigurationName name of a specific indexing |
||
776 | * configuration |
||
777 | * @return int Number of items |
||
778 | */ |
||
779 | 2 | public function getItemsCountBySite(Site $site, $indexingConfigurationName = '') |
|
785 | |||
786 | /** |
||
787 | * Gets number of unprocessed Index Queue items for a specific site / indexing configuration |
||
788 | * optional parameter to limit the counted items by indexing configuration. |
||
789 | * |
||
790 | * @param Site $site The site to search for. |
||
791 | * @param string $indexingConfigurationName name of a specific indexing |
||
792 | * configuration |
||
793 | * @return int Number of items. |
||
794 | */ |
||
795 | 2 | public function getRemainingItemsCountBySite(Site $site, $indexingConfigurationName = '') |
|
801 | |||
802 | /** |
||
803 | * Returns the number of items for all queues. |
||
804 | * |
||
805 | * @return int |
||
806 | */ |
||
807 | 47 | public function getAllItemsCount() |
|
811 | |||
812 | /** |
||
813 | * @param string $where |
||
814 | * @return int |
||
815 | */ |
||
816 | 50 | private function getItemCount($where = '1=1') |
|
823 | |||
824 | /** |
||
825 | * Build a database constraint that limits to a certain indexConfigurationName |
||
826 | * |
||
827 | * @param string $indexingConfigurationName |
||
828 | * @return string |
||
829 | */ |
||
830 | 3 | protected function buildIndexConfigurationConstraint($indexingConfigurationName) |
|
839 | |||
840 | /** |
||
841 | * Gets $limit number of items to index for a particular $site. |
||
842 | * |
||
843 | * @param Site $site TYPO3 site |
||
844 | * @param int $limit Number of items to get from the queue |
||
845 | * @return Item[] Items to index to the given solr server |
||
846 | */ |
||
847 | 7 | public function getItemsToIndex(Site $site, $limit = 50) |
|
870 | |||
871 | /** |
||
872 | * Creates an array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects from an array of |
||
873 | * index queue records. |
||
874 | * |
||
875 | * @param array $indexQueueItemRecords Array of plain index queue records |
||
876 | * @return array Array of ApacheSolrForTypo3\Solr\IndexQueue\Item objects |
||
877 | */ |
||
878 | 26 | protected function getIndexQueueItemObjectsFromRecords( |
|
935 | |||
936 | /** |
||
937 | * Marks an item as failed and causes the indexer to skip the item in the |
||
938 | * next run. |
||
939 | * |
||
940 | * @param int|Item $item Either the item's Index Queue |
||
941 | * uid or the complete item |
||
942 | * @param string $errorMessage Error message |
||
943 | */ |
||
944 | public function markItemAsFailed($item, $errorMessage = '') |
||
965 | } |
||
966 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.