66
77namespace Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock ;
88
9+ use Exception ;
10+ use Magento \Catalog \Api \Data \ProductInterface ;
911use Magento \Catalog \Model \ResourceModel \Product \Indexer \AbstractIndexer ;
12+ use Magento \CatalogInventory \Model \Configuration ;
1013use Magento \CatalogInventory \Model \Stock ;
14+ use Magento \Eav \Model \Config ;
15+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1116use Magento \Framework \App \ObjectManager ;
1217use Magento \Framework \DB \Adapter \AdapterInterface ;
1318use Magento \CatalogInventory \Api \StockConfigurationInterface ;
1419use Magento \CatalogInventory \Model \Indexer \Stock \Action \Full ;
1520use 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 ;
1628
1729/**
1830 * CatalogInventory Default Stock Status Indexer Resource Model
@@ -37,7 +49,7 @@ class DefaultStock extends AbstractIndexer implements StockInterface
3749 protected $ _isComposite = false ;
3850
3951 /**
40- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
52+ * @var ScopeConfigInterface
4153 */
4254 protected $ _scopeConfig ;
4355
@@ -63,27 +75,35 @@ class DefaultStock extends AbstractIndexer implements StockInterface
6375 private $ getStatusExpression ;
6476
6577 /**
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
7282 * @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
7486 */
7587 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 ,
8092 $ connectionName = null ,
81- \Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \GetStatusExpression $ getStatusExpression = null
93+ GetStatusExpression $ getStatusExpression = null ,
94+ StockConfigurationInterface $ stockConfiguration = null ,
95+ QueryProcessorComposite $ queryProcessorComposite = null
8296 ) {
8397 $ this ->_scopeConfig = $ scopeConfig ;
8498 parent ::__construct ($ context , $ tableStrategy , $ eavConfig , $ connectionName );
8599 $ 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
87107 );
88108 }
89109
@@ -101,7 +121,7 @@ protected function _construct()
101121 * Reindex all stock status data for default logic product type
102122 *
103123 * @return $this
104- * @throws \ Exception
124+ * @throws Exception
105125 */
106126 public function reindexAll ()
107127 {
@@ -110,7 +130,7 @@ public function reindexAll()
110130 try {
111131 $ this ->_prepareIndexTable ();
112132 $ this ->commit ();
113- } catch (\ Exception $ e ) {
133+ } catch (Exception $ e ) {
114134 $ this ->rollBack ();
115135 throw $ e ;
116136 }
@@ -175,12 +195,12 @@ public function setTypeId($typeId)
175195 * Retrieve active Product Type Id
176196 *
177197 * @return string
178- * @throws \Magento\Framework\Exception\ LocalizedException
198+ * @throws LocalizedException
179199 */
180200 public function getTypeId ()
181201 {
182202 if ($ this ->_typeId === null ) {
183- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Undefined product type ' ));
203+ throw new LocalizedException (__ ('Undefined product type ' ));
184204 }
185205 return $ this ->_typeId ;
186206 }
@@ -216,8 +236,8 @@ public function getIsComposite()
216236 protected function _isManageStock ()
217237 {
218238 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
221241 );
222242 }
223243
@@ -226,14 +246,14 @@ protected function _isManageStock()
226246 *
227247 * @param int|array $entityIds
228248 * @param bool $usePrimaryTable use primary or temporary index table
229- * @return \Magento\Framework\DB\ Select
249+ * @return Select
230250 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
231251 */
232252 protected function _getStockStatusSelect ($ entityIds = null , $ usePrimaryTable = false )
233253 {
234254 $ connection = $ this ->getConnection ();
235255 $ 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);
237257 $ linkField = $ metadata ->getLinkField ();
238258
239259 $ select = $ connection ->select ()->from (
@@ -255,20 +275,20 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
255275 . ' AND mcpei.value = ' . ProductStatus::STATUS_ENABLED ,
256276 []
257277 )->joinLeft (
258- ['css ' => 'cataloginventory_stock_status ' ],
278+ ['css ' => $ this -> getTable ( 'cataloginventory_stock_status ' ) ],
259279 'css.product_id = e.entity_id ' ,
260280 []
261281 )->columns (
262282 ['qty ' => $ qtyExpr ]
263283 )->where (
264284 'cis.website_id = ? ' ,
265- $ this ->getStockConfiguration () ->getDefaultScopeId ()
285+ $ this ->stockConfiguration ->getDefaultScopeId ()
266286 )->where ('e.type_id = ? ' , $ this ->getTypeId ())
267287 ->group (['e.entity_id ' , 'cis.website_id ' , 'cis.stock_id ' ]);
268288
269289 $ select ->columns (['status ' => $ this ->getStatusExpression ($ connection , true )]);
270290 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 );
272292 }
273293
274294 return $ select ;
@@ -284,7 +304,7 @@ protected function _prepareIndexTable($entityIds = null)
284304 {
285305 $ connection = $ this ->getConnection ();
286306 $ select = $ this ->_getStockStatusSelect ($ entityIds , true );
287- $ select = $ this ->getQueryProcessorComposite () ->processQuery ($ select , $ entityIds );
307+ $ select = $ this ->queryProcessorComposite ->processQuery ($ select , $ entityIds );
288308 $ query = $ select ->insertFromSelect ($ this ->getIdxTable ());
289309 $ connection ->query ($ query );
290310
@@ -301,13 +321,13 @@ protected function _updateIndex($entityIds)
301321 {
302322 $ connection = $ this ->getConnection ();
303323 $ select = $ this ->_getStockStatusSelect ($ entityIds , true );
304- $ select = $ this ->getQueryProcessorComposite () ->processQuery ($ select , $ entityIds , true );
324+ $ select = $ this ->queryProcessorComposite ->processQuery ($ select , $ entityIds , true );
305325 $ query = $ connection ->query ($ select );
306326
307327 $ i = 0 ;
308328 $ data = [];
309329 $ savedEntityIds = [];
310- while ($ row = $ query ->fetch (\ PDO ::FETCH_ASSOC )) {
330+ while ($ row = $ query ->fetch (PDO ::FETCH_ASSOC )) {
311331 $ i ++;
312332 $ data [] = [
313333 'product_id ' => (int )$ row ['entity_id ' ],
@@ -336,7 +356,7 @@ protected function _updateIndex($entityIds)
336356 *
337357 * @param array $ids
338358 * @return void
339- * @throws \Magento\Framework\Exception\ LocalizedException
359+ * @throws LocalizedException
340360 */
341361 private function deleteOldRecords (array $ ids )
342362 {
@@ -398,24 +418,6 @@ protected function getStatusExpression(AdapterInterface $connection, $isAggregat
398418 */
399419 protected function getStockConfiguration ()
400420 {
401- if ($ this ->stockConfiguration === null ) {
402- $ this ->stockConfiguration = \Magento \Framework \App \ObjectManager::getInstance ()
403- ->get (\Magento \CatalogInventory \Api \StockConfigurationInterface::class);
404- }
405421 return $ this ->stockConfiguration ;
406422 }
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- }
421423}
0 commit comments