6
6
7
7
namespace Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock ;
8
8
9
+ use Exception ;
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
9
11
use Magento \Catalog \Model \ResourceModel \Product \Indexer \AbstractIndexer ;
12
+ use Magento \CatalogInventory \Model \Configuration ;
10
13
use Magento \CatalogInventory \Model \Stock ;
14
+ use Magento \Eav \Model \Config ;
15
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
11
16
use Magento \Framework \App \ObjectManager ;
12
17
use Magento \Framework \DB \Adapter \AdapterInterface ;
13
18
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
14
19
use Magento \CatalogInventory \Model \Indexer \Stock \Action \Full ;
15
20
use Magento \Catalog \Model \Product \Attribute \Source \Status as ProductStatus ;
21
+ use Magento \Framework \DB \Select ;
22
+ use Magento \Framework \Exception \LocalizedException ;
23
+ use Magento \Framework \Indexer \Table \StrategyInterface ;
24
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
25
+ use Magento \Store \Model \ScopeInterface ;
26
+ use PDO ;
27
+ use Zend_Db ;
16
28
17
29
/**
18
30
* CatalogInventory Default Stock Status Indexer Resource Model
@@ -37,7 +49,7 @@ class DefaultStock extends AbstractIndexer implements StockInterface
37
49
protected $ _isComposite = false ;
38
50
39
51
/**
40
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
52
+ * @var ScopeConfigInterface
41
53
*/
42
54
protected $ _scopeConfig ;
43
55
@@ -63,27 +75,35 @@ class DefaultStock extends AbstractIndexer implements StockInterface
63
75
private $ getStatusExpression ;
64
76
65
77
/**
66
- * Class constructor
67
- *
68
- * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
69
- * @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy
70
- * @param \Magento\Eav\Model\Config $eavConfig
71
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
78
+ * @param Context $context
79
+ * @param StrategyInterface $tableStrategy
80
+ * @param Config $eavConfig
81
+ * @param ScopeConfigInterface $scopeConfig
72
82
* @param string $connectionName
73
- * @param \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\GetStatusExpression|null $getStatusExpression
83
+ * @param GetStatusExpression|null $getStatusExpression
84
+ * @param StockConfigurationInterface|null $stockConfiguration
85
+ * @param QueryProcessorComposite|null $queryProcessorComposite
74
86
*/
75
87
public function __construct (
76
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ Context $ context ,
77
- \ Magento \ Framework \ Indexer \ Table \ StrategyInterface $ tableStrategy ,
78
- \ Magento \ Eav \ Model \ Config $ eavConfig ,
79
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
88
+ Context $ context ,
89
+ StrategyInterface $ tableStrategy ,
90
+ Config $ eavConfig ,
91
+ ScopeConfigInterface $ scopeConfig ,
80
92
$ connectionName = null ,
81
- \Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \GetStatusExpression $ getStatusExpression = null
93
+ GetStatusExpression $ getStatusExpression = null ,
94
+ StockConfigurationInterface $ stockConfiguration = null ,
95
+ QueryProcessorComposite $ queryProcessorComposite = null
82
96
) {
83
97
$ this ->_scopeConfig = $ scopeConfig ;
84
98
parent ::__construct ($ context , $ tableStrategy , $ eavConfig , $ connectionName );
85
99
$ this ->getStatusExpression = $ getStatusExpression ?: ObjectManager::getInstance ()->get (
86
- \Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \GetStatusExpression::class
100
+ GetStatusExpression::class
101
+ );
102
+ $ this ->stockConfiguration = $ stockConfiguration ?: ObjectManager::getInstance ()->get (
103
+ StockConfigurationInterface::class
104
+ );
105
+ $ this ->queryProcessorComposite = $ queryProcessorComposite ?: ObjectManager::getInstance ()->get (
106
+ QueryProcessorComposite::class
87
107
);
88
108
}
89
109
@@ -101,7 +121,7 @@ protected function _construct()
101
121
* Reindex all stock status data for default logic product type
102
122
*
103
123
* @return $this
104
- * @throws \ Exception
124
+ * @throws Exception
105
125
*/
106
126
public function reindexAll ()
107
127
{
@@ -110,7 +130,7 @@ public function reindexAll()
110
130
try {
111
131
$ this ->_prepareIndexTable ();
112
132
$ this ->commit ();
113
- } catch (\ Exception $ e ) {
133
+ } catch (Exception $ e ) {
114
134
$ this ->rollBack ();
115
135
throw $ e ;
116
136
}
@@ -175,12 +195,12 @@ public function setTypeId($typeId)
175
195
* Retrieve active Product Type Id
176
196
*
177
197
* @return string
178
- * @throws \Magento\Framework\Exception\ LocalizedException
198
+ * @throws LocalizedException
179
199
*/
180
200
public function getTypeId ()
181
201
{
182
202
if ($ this ->_typeId === null ) {
183
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Undefined product type ' ));
203
+ throw new LocalizedException (__ ('Undefined product type ' ));
184
204
}
185
205
return $ this ->_typeId ;
186
206
}
@@ -216,8 +236,8 @@ public function getIsComposite()
216
236
protected function _isManageStock ()
217
237
{
218
238
return $ this ->_scopeConfig ->isSetFlag (
219
- \ Magento \ CatalogInventory \ Model \ Configuration::XML_PATH_MANAGE_STOCK ,
220
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
239
+ Configuration::XML_PATH_MANAGE_STOCK ,
240
+ ScopeInterface::SCOPE_STORE
221
241
);
222
242
}
223
243
@@ -226,14 +246,14 @@ protected function _isManageStock()
226
246
*
227
247
* @param int|array $entityIds
228
248
* @param bool $usePrimaryTable use primary or temporary index table
229
- * @return \Magento\Framework\DB\ Select
249
+ * @return Select
230
250
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
231
251
*/
232
252
protected function _getStockStatusSelect ($ entityIds = null , $ usePrimaryTable = false )
233
253
{
234
254
$ connection = $ this ->getConnection ();
235
255
$ qtyExpr = $ connection ->getCheckSql ('cisi.qty > 0 ' , 'cisi.qty ' , 0 );
236
- $ metadata = $ this ->getMetadataPool ()->getMetadata (\ Magento \ Catalog \ Api \ Data \ ProductInterface::class);
256
+ $ metadata = $ this ->getMetadataPool ()->getMetadata (ProductInterface::class);
237
257
$ linkField = $ metadata ->getLinkField ();
238
258
239
259
$ select = $ connection ->select ()->from (
@@ -255,20 +275,20 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
255
275
. ' AND mcpei.value = ' . ProductStatus::STATUS_ENABLED ,
256
276
[]
257
277
)->joinLeft (
258
- ['css ' => 'cataloginventory_stock_status ' ],
278
+ ['css ' => $ this -> getTable ( 'cataloginventory_stock_status ' ) ],
259
279
'css.product_id = e.entity_id ' ,
260
280
[]
261
281
)->columns (
262
282
['qty ' => $ qtyExpr ]
263
283
)->where (
264
284
'cis.website_id = ? ' ,
265
- $ this ->getStockConfiguration () ->getDefaultScopeId ()
285
+ $ this ->stockConfiguration ->getDefaultScopeId ()
266
286
)->where ('e.type_id = ? ' , $ this ->getTypeId ())
267
287
->group (['e.entity_id ' , 'cis.website_id ' , 'cis.stock_id ' ]);
268
288
269
289
$ select ->columns (['status ' => $ this ->getStatusExpression ($ connection , true )]);
270
290
if ($ entityIds !== null ) {
271
- $ select ->where ('e.entity_id IN(?) ' , $ entityIds , \ Zend_Db::INT_TYPE );
291
+ $ select ->where ('e.entity_id IN(?) ' , $ entityIds , Zend_Db::INT_TYPE );
272
292
}
273
293
274
294
return $ select ;
@@ -284,7 +304,7 @@ protected function _prepareIndexTable($entityIds = null)
284
304
{
285
305
$ connection = $ this ->getConnection ();
286
306
$ select = $ this ->_getStockStatusSelect ($ entityIds , true );
287
- $ select = $ this ->getQueryProcessorComposite () ->processQuery ($ select , $ entityIds );
307
+ $ select = $ this ->queryProcessorComposite ->processQuery ($ select , $ entityIds );
288
308
$ query = $ select ->insertFromSelect ($ this ->getIdxTable ());
289
309
$ connection ->query ($ query );
290
310
@@ -301,13 +321,13 @@ protected function _updateIndex($entityIds)
301
321
{
302
322
$ connection = $ this ->getConnection ();
303
323
$ select = $ this ->_getStockStatusSelect ($ entityIds , true );
304
- $ select = $ this ->getQueryProcessorComposite () ->processQuery ($ select , $ entityIds , true );
324
+ $ select = $ this ->queryProcessorComposite ->processQuery ($ select , $ entityIds , true );
305
325
$ query = $ connection ->query ($ select );
306
326
307
327
$ i = 0 ;
308
328
$ data = [];
309
329
$ savedEntityIds = [];
310
- while ($ row = $ query ->fetch (\ PDO ::FETCH_ASSOC )) {
330
+ while ($ row = $ query ->fetch (PDO ::FETCH_ASSOC )) {
311
331
$ i ++;
312
332
$ data [] = [
313
333
'product_id ' => (int )$ row ['entity_id ' ],
@@ -336,7 +356,7 @@ protected function _updateIndex($entityIds)
336
356
*
337
357
* @param array $ids
338
358
* @return void
339
- * @throws \Magento\Framework\Exception\ LocalizedException
359
+ * @throws LocalizedException
340
360
*/
341
361
private function deleteOldRecords (array $ ids )
342
362
{
@@ -398,24 +418,6 @@ protected function getStatusExpression(AdapterInterface $connection, $isAggregat
398
418
*/
399
419
protected function getStockConfiguration ()
400
420
{
401
- if ($ this ->stockConfiguration === null ) {
402
- $ this ->stockConfiguration = \Magento \Framework \App \ObjectManager::getInstance ()
403
- ->get (\Magento \CatalogInventory \Api \StockConfigurationInterface::class);
404
- }
405
421
return $ this ->stockConfiguration ;
406
422
}
407
-
408
- /**
409
- * Get query processor composite
410
- *
411
- * @return QueryProcessorComposite
412
- */
413
- private function getQueryProcessorComposite ()
414
- {
415
- if (null === $ this ->queryProcessorComposite ) {
416
- $ this ->queryProcessorComposite = \Magento \Framework \App \ObjectManager::getInstance ()
417
- ->get (\Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \QueryProcessorComposite::class);
418
- }
419
- return $ this ->queryProcessorComposite ;
420
- }
421
423
}
0 commit comments