33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
7+ declare (strict_types=1 );
8+
69namespace Magento \Catalog \Model \Indexer \Category \Product \Action ;
710
11+ use Magento \Catalog \Api \Data \ProductInterface ;
12+ use Magento \Catalog \Model \Config ;
13+ use Magento \Catalog \Model \Indexer \Category \Product \AbstractAction ;
814use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
15+ use Magento \Framework \App \ObjectManager ;
16+ use Magento \Framework \DB \Adapter \AdapterInterface ;
917use Magento \Framework \DB \Query \Generator as QueryGenerator ;
1018use Magento \Framework \App \ResourceConnection ;
19+ use Magento \Framework \DB \Select ;
20+ use Magento \Framework \EntityManager \MetadataPool ;
21+ use Magento \Framework \Indexer \BatchProviderInterface ;
22+ use Magento \Framework \Indexer \BatchSizeManagementInterface ;
1123use Magento \Indexer \Model \ProcessManager ;
24+ use Magento \Store \Model \Store ;
25+ use Magento \Store \Model \StoreManagerInterface ;
1226
1327/**
1428 * Class Full reindex action
1529 *
16- * @package Magento\Catalog\Model\Indexer\Category\Product\Action
1730 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1831 */
19- class Full extends \ Magento \ Catalog \ Model \ Indexer \ Category \ Product \ AbstractAction
32+ class Full extends AbstractAction
2033{
2134 /**
22- * @var \Magento\Framework\Indexer\ BatchSizeManagementInterface
35+ * @var BatchSizeManagementInterface
2336 */
2437 private $ batchSizeManagement ;
2538
2639 /**
27- * @var \Magento\Framework\Indexer\ BatchProviderInterface
40+ * @var BatchProviderInterface
2841 */
2942 private $ batchProvider ;
3043
3144 /**
32- * @var \Magento\Framework\EntityManager\ MetadataPool
45+ * @var MetadataPool
3346 */
3447 protected $ metadataPool ;
3548
@@ -52,25 +65,25 @@ class Full extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
5265
5366 /**
5467 * @param ResourceConnection $resource
55- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
56- * @param \Magento\Catalog\Model\ Config $config
68+ * @param StoreManagerInterface $storeManager
69+ * @param Config $config
5770 * @param QueryGenerator|null $queryGenerator
58- * @param \Magento\Framework\Indexer\ BatchSizeManagementInterface|null $batchSizeManagement
59- * @param \Magento\Framework\Indexer\ BatchProviderInterface|null $batchProvider
60- * @param \Magento\Framework\EntityManager\ MetadataPool|null $metadataPool
71+ * @param BatchSizeManagementInterface|null $batchSizeManagement
72+ * @param BatchProviderInterface|null $batchProvider
73+ * @param MetadataPool|null $metadataPool
6174 * @param int|null $batchRowsCount
6275 * @param ActiveTableSwitcher|null $activeTableSwitcher
6376 * @param ProcessManager $processManager
6477 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
6578 */
6679 public function __construct (
67- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
68- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
69- \ Magento \ Catalog \ Model \ Config $ config ,
80+ ResourceConnection $ resource ,
81+ StoreManagerInterface $ storeManager ,
82+ Config $ config ,
7083 QueryGenerator $ queryGenerator = null ,
71- \ Magento \ Framework \ Indexer \ BatchSizeManagementInterface $ batchSizeManagement = null ,
72- \ Magento \ Framework \ Indexer \ BatchProviderInterface $ batchProvider = null ,
73- \ Magento \ Framework \ EntityManager \ MetadataPool $ metadataPool = null ,
84+ BatchSizeManagementInterface $ batchSizeManagement = null ,
85+ BatchProviderInterface $ batchProvider = null ,
86+ MetadataPool $ metadataPool = null ,
7487 $ batchRowsCount = null ,
7588 ActiveTableSwitcher $ activeTableSwitcher = null ,
7689 ProcessManager $ processManager = null
@@ -81,49 +94,55 @@ public function __construct(
8194 $ config ,
8295 $ queryGenerator
8396 );
84- $ objectManager = \ Magento \ Framework \ App \ ObjectManager::getInstance ();
97+ $ objectManager = ObjectManager::getInstance ();
8598 $ this ->batchSizeManagement = $ batchSizeManagement ?: $ objectManager ->get (
86- \ Magento \ Framework \ Indexer \ BatchSizeManagementInterface::class
99+ BatchSizeManagementInterface::class
87100 );
88101 $ this ->batchProvider = $ batchProvider ?: $ objectManager ->get (
89- \ Magento \ Framework \ Indexer \ BatchProviderInterface::class
102+ BatchProviderInterface::class
90103 );
91104 $ this ->metadataPool = $ metadataPool ?: $ objectManager ->get (
92- \ Magento \ Framework \ EntityManager \ MetadataPool::class
105+ MetadataPool::class
93106 );
94107 $ this ->batchRowsCount = $ batchRowsCount ;
95108 $ this ->activeTableSwitcher = $ activeTableSwitcher ?: $ objectManager ->get (ActiveTableSwitcher::class);
96109 $ this ->processManager = $ processManager ?: $ objectManager ->get (ProcessManager::class);
97110 }
98111
99112 /**
113+ * Create the store tables
114+ *
100115 * @return void
101116 */
102- private function createTables ()
117+ private function createTables (): void
103118 {
104119 foreach ($ this ->storeManager ->getStores () as $ store ) {
105- $ this ->tableMaintainer ->createTablesForStore ($ store ->getId ());
120+ $ this ->tableMaintainer ->createTablesForStore (( int ) $ store ->getId ());
106121 }
107122 }
108123
109124 /**
125+ * Truncates the replica tables
126+ *
110127 * @return void
111128 */
112- private function clearReplicaTables ()
129+ private function clearReplicaTables (): void
113130 {
114131 foreach ($ this ->storeManager ->getStores () as $ store ) {
115- $ this ->connection ->truncateTable ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()));
132+ $ this ->connection ->truncateTable ($ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ()));
116133 }
117134 }
118135
119136 /**
137+ * Switches the active table
138+ *
120139 * @return void
121140 */
122- private function switchTables ()
141+ private function switchTables (): void
123142 {
124143 $ tablesToSwitch = [];
125144 foreach ($ this ->storeManager ->getStores () as $ store ) {
126- $ tablesToSwitch [] = $ this ->tableMaintainer ->getMainTable ($ store ->getId ());
145+ $ tablesToSwitch [] = $ this ->tableMaintainer ->getMainTable (( int ) $ store ->getId ());
127146 }
128147 $ this ->activeTableSwitcher ->switchTable ($ this ->connection , $ tablesToSwitch );
129148 }
@@ -133,12 +152,13 @@ private function switchTables()
133152 *
134153 * @return $this
135154 */
136- public function execute ()
155+ public function execute (): self
137156 {
138157 $ this ->createTables ();
139158 $ this ->clearReplicaTables ();
140159 $ this ->reindex ();
141160 $ this ->switchTables ();
161+
142162 return $ this ;
143163 }
144164
@@ -147,7 +167,7 @@ public function execute()
147167 *
148168 * @return void
149169 */
150- protected function reindex ()
170+ protected function reindex (): void
151171 {
152172 $ userFunctions = [];
153173
@@ -165,9 +185,9 @@ protected function reindex()
165185 /**
166186 * Execute indexation by store
167187 *
168- * @param \Magento\Store\Model\ Store $store
188+ * @param Store $store
169189 */
170- private function reindexStore ($ store )
190+ private function reindexStore ($ store ): void
171191 {
172192 $ this ->reindexRootCategory ($ store );
173193 $ this ->reindexAnchorCategories ($ store );
@@ -177,31 +197,31 @@ private function reindexStore($store)
177197 /**
178198 * Publish data from tmp to replica table
179199 *
180- * @param \Magento\Store\Model\ Store $store
200+ * @param Store $store
181201 * @return void
182202 */
183- private function publishData ($ store )
203+ private function publishData ($ store ): void
184204 {
185- $ select = $ this ->connection ->select ()->from ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()));
205+ $ select = $ this ->connection ->select ()->from ($ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()));
186206 $ columns = array_keys (
187- $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ()))
207+ $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ()))
188208 );
189- $ tableName = $ this ->tableMaintainer ->getMainReplicaTable ($ store ->getId ());
209+ $ tableName = $ this ->tableMaintainer ->getMainReplicaTable (( int ) $ store ->getId ());
190210
191211 $ this ->connection ->query (
192212 $ this ->connection ->insertFromSelect (
193213 $ select ,
194214 $ tableName ,
195215 $ columns ,
196- \ Magento \ Framework \ DB \ Adapter \ AdapterInterface::INSERT_ON_DUPLICATE
216+ AdapterInterface::INSERT_ON_DUPLICATE
197217 )
198218 );
199219 }
200220
201221 /**
202- * { @inheritdoc}
222+ * @inheritdoc
203223 */
204- protected function reindexRootCategory (\ Magento \ Store \ Model \ Store $ store )
224+ protected function reindexRootCategory (Store $ store ): void
205225 {
206226 if ($ this ->isIndexRootCategoryNeeded ()) {
207227 $ this ->reindexCategoriesBySelect ($ this ->getAllProducts ($ store ), 'cp.entity_id IN (?) ' , $ store );
@@ -211,62 +231,64 @@ protected function reindexRootCategory(\Magento\Store\Model\Store $store)
211231 /**
212232 * Reindex products of anchor categories
213233 *
214- * @param \Magento\Store\Model\ Store $store
234+ * @param Store $store
215235 * @return void
216236 */
217- protected function reindexAnchorCategories (\ Magento \ Store \ Model \ Store $ store )
237+ protected function reindexAnchorCategories (Store $ store ): void
218238 {
219239 $ this ->reindexCategoriesBySelect ($ this ->getAnchorCategoriesSelect ($ store ), 'ccp.product_id IN (?) ' , $ store );
220240 }
221241
222242 /**
223243 * Reindex products of non anchor categories
224244 *
225- * @param \Magento\Store\Model\ Store $store
245+ * @param Store $store
226246 * @return void
227247 */
228- protected function reindexNonAnchorCategories (\ Magento \ Store \ Model \ Store $ store )
248+ protected function reindexNonAnchorCategories (Store $ store ): void
229249 {
230250 $ this ->reindexCategoriesBySelect ($ this ->getNonAnchorCategoriesSelect ($ store ), 'ccp.product_id IN (?) ' , $ store );
231251 }
232252
233253 /**
234254 * Reindex categories using given SQL select and condition.
235255 *
236- * @param \Magento\Framework\DB\ Select $basicSelect
256+ * @param Select $basicSelect
237257 * @param string $whereCondition
238- * @param \Magento\Store\Model\ Store $store
258+ * @param Store $store
239259 * @return void
240260 */
241- private function reindexCategoriesBySelect (\ Magento \ Framework \ DB \ Select $ basicSelect , $ whereCondition , $ store )
261+ private function reindexCategoriesBySelect (Select $ basicSelect , $ whereCondition , $ store ): void
242262 {
243- $ this ->tableMaintainer ->createMainTmpTable ($ store ->getId ());
263+ $ this ->tableMaintainer ->createMainTmpTable (( int ) $ store ->getId ());
244264
245- $ entityMetadata = $ this ->metadataPool ->getMetadata (\ Magento \ Catalog \ Api \ Data \ ProductInterface::class);
265+ $ entityMetadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
246266 $ columns = array_keys (
247- $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()))
267+ $ this ->connection ->describeTable ($ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()))
248268 );
249269 $ this ->batchSizeManagement ->ensureBatchSize ($ this ->connection , $ this ->batchRowsCount );
250- $ batches = $ this ->batchProvider ->getBatches (
251- $ this ->connection ,
252- $ entityMetadata ->getEntityTable (),
270+
271+ $ select = $ this ->connection ->select ();
272+ $ select ->distinct (true );
273+ $ select ->from (['e ' => $ entityMetadata ->getEntityTable ()], $ entityMetadata ->getIdentifierField ());
274+
275+ $ batchQueries = $ this ->prepareSelectsByRange (
276+ $ select ,
253277 $ entityMetadata ->getIdentifierField (),
254- $ this ->batchRowsCount
278+ ( int ) $ this ->batchRowsCount
255279 );
256- foreach ($ batches as $ batch ) {
257- $ this ->connection ->delete ($ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()));
280+
281+ foreach ($ batchQueries as $ query ) {
282+ $ this ->connection ->delete ($ this ->tableMaintainer ->getMainTmpTable ((int )$ store ->getId ()));
283+ $ entityIds = $ this ->connection ->fetchCol ($ query );
258284 $ resultSelect = clone $ basicSelect ;
259- $ select = $ this ->connection ->select ();
260- $ select ->distinct (true );
261- $ select ->from (['e ' => $ entityMetadata ->getEntityTable ()], $ entityMetadata ->getIdentifierField ());
262- $ entityIds = $ this ->batchProvider ->getBatchIds ($ this ->connection , $ select , $ batch );
263285 $ resultSelect ->where ($ whereCondition , $ entityIds );
264286 $ this ->connection ->query (
265287 $ this ->connection ->insertFromSelect (
266288 $ resultSelect ,
267- $ this ->tableMaintainer ->getMainTmpTable ($ store ->getId ()),
289+ $ this ->tableMaintainer ->getMainTmpTable (( int ) $ store ->getId ()),
268290 $ columns ,
269- \ Magento \ Framework \ DB \ Adapter \ AdapterInterface::INSERT_ON_DUPLICATE
291+ AdapterInterface::INSERT_ON_DUPLICATE
270292 )
271293 );
272294 $ this ->publishData ($ store );
0 commit comments