5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Category \Flat \Action ;
7
7
8
- use Magento \Framework \App \ResourceConnection ;
9
-
10
8
/**
11
9
* Class for full reindex flat categories
12
10
*/
@@ -24,28 +22,6 @@ class Full extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
24
22
*/
25
23
protected $ allowTableChanges = true ;
26
24
27
- /**
28
- * @var \Magento\Catalog\Helper\Product\Flat\Indexer
29
- */
30
- private $ indexer ;
31
-
32
- /**
33
- * @param ResourceConnection $resource
34
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
35
- * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
36
- * @param \Magento\Catalog\Helper\Product\Flat\Indexer $indexer
37
- */
38
- public function __construct (
39
- ResourceConnection $ resource ,
40
- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
41
- \Magento \Catalog \Model \ResourceModel \Helper $ resourceHelper ,
42
- \Magento \Catalog \Helper \Product \Flat \Indexer $ indexer = null
43
- ) {
44
- $ this ->indexer = $ indexer ?: \Magento \Framework \App \ObjectManager::getInstance ()
45
- ->get (\Magento \Catalog \Helper \Product \Flat \Indexer::class);
46
- parent ::__construct ($ resource , $ storeManager , $ resourceHelper );
47
- }
48
-
49
25
/**
50
26
* Add suffix to table name to show it is old
51
27
*
@@ -196,6 +172,43 @@ protected function switchTables(array $stores = [])
196
172
return $ this ;
197
173
}
198
174
175
+ /**
176
+ * Retrieve all actual Catalog Product Flat Table names
177
+ *
178
+ * @return string[]
179
+ */
180
+ private function getActualStoreTablesForCategoryFlat (): array
181
+ {
182
+ $ actualStoreTables = [];
183
+ foreach ($ this ->storeManager ->getStores () as $ store ) {
184
+ $ actualStoreTables [] = sprintf (
185
+ '%s_store_%s ' ,
186
+ $ this ->connection ->getTableName ('catalog_category_flat ' ), $ store ->getId ()
187
+ );
188
+ }
189
+
190
+ return $ actualStoreTables ;
191
+ }
192
+
193
+ /**
194
+ * Delete all category flat tables for not existing stores
195
+ *
196
+ * @return void
197
+ */
198
+ private function deleteAbandonedStoreCategoryFlatTables (): void
199
+ {
200
+ $ existentTables = $ this ->connection ->getTables (
201
+ $ this ->connection ->getTableName ('catalog_category_flat_store_% ' )
202
+ );
203
+ $ actualStoreTables = $ this ->getActualStoreTablesForCategoryFlat ();
204
+
205
+ $ tablesToDelete = array_diff ($ existentTables , $ actualStoreTables );
206
+
207
+ foreach ($ tablesToDelete as $ table ) {
208
+ $ this ->connection ->dropTable ($ table );
209
+ }
210
+ }
211
+
199
212
/**
200
213
* Transactional rebuild flat data from eav
201
214
*
@@ -211,7 +224,7 @@ public function reindexAll()
211
224
$ stores = $ this ->storeManager ->getStores ();
212
225
$ this ->populateFlatTables ($ stores );
213
226
$ this ->switchTables ($ stores );
214
- $ this ->indexer -> deleteAbandonedStoreCategoryFlatTables ();
227
+ $ this ->deleteAbandonedStoreCategoryFlatTables ();
215
228
$ this ->allowTableChanges = true ;
216
229
217
230
return $ this ;
0 commit comments