5
5
*/
6
6
namespace Magento \Bundle \Model \ResourceModel \Indexer ;
7
7
8
- use Magento \Catalog \Api \Data \ProductInterface ;
9
8
use Magento \CatalogInventory \Model \Indexer \Stock \Action \Full ;
10
- use Magento \Catalog \ Model \ Product \ Attribute \ Source \ Status as ProductStatus ;
9
+ use Magento \Framework \ App \ ObjectManager ;
11
10
12
11
/**
13
12
* Bundle Stock Status Indexer Resource Model
14
13
*
15
14
* @author Magento Core Team <core@magentocommerce.com>
16
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
15
*/
18
16
class Stock extends \Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \DefaultStock
19
17
{
@@ -22,6 +20,16 @@ class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\
22
20
*/
23
21
private $ indexerStockFrontendResource ;
24
22
23
+ /**
24
+ * @var \Magento\Bundle\Model\ResourceModel\Indexer\StockStatusSelectBuilder
25
+ */
26
+ private $ stockStatusSelectBuilder ;
27
+
28
+ /**
29
+ * @var \Magento\Bundle\Model\ResourceModel\Indexer\BundleOptionStockDataSelectBuilder
30
+ */
31
+ private $ bundleOptionStockDataSelectBuilder ;
32
+
25
33
/**
26
34
* Class constructor
27
35
*
@@ -32,6 +40,7 @@ class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\
32
40
* @param string $connectionName
33
41
* @param null|\Magento\Indexer\Model\Indexer\StateFactory $stateFactory
34
42
* @param null|\Magento\Indexer\Model\ResourceModel\FrontendResource $indexerStockFrontendResource
43
+ * @param null|\Magento\Bundle\Model\ResourceModel\Indexer\StockStatusSelectBuilder $stockStatusSelectBuilder
35
44
*/
36
45
public function __construct (
37
46
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
@@ -40,11 +49,20 @@ public function __construct(
40
49
\Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
41
50
$ connectionName = null ,
42
51
\Magento \Indexer \Model \Indexer \StateFactory $ stateFactory = null ,
43
- \Magento \Indexer \Model \ResourceModel \FrontendResource $ indexerStockFrontendResource = null
52
+ \Magento \Indexer \Model \ResourceModel \FrontendResource $ indexerStockFrontendResource = null ,
53
+ StockStatusSelectBuilder $ stockStatusSelectBuilder = null ,
54
+ BundleOptionStockDataSelectBuilder $ bundleOptionStockDataSelectBuilder = null
44
55
) {
45
56
parent ::__construct ($ context , $ tableStrategy , $ eavConfig , $ scopeConfig , $ connectionName , $ stateFactory );
57
+
46
58
$ this ->indexerStockFrontendResource = $ indexerStockFrontendResource ?: ObjectManager::getInstance ()
47
59
->get (\Magento \CatalogInventory \Model \ResourceModel \Indexer \Stock \FrontendResource::class);
60
+
61
+ $ this ->stockStatusSelectBuilder = $ stockStatusSelectBuilder ?: ObjectManager::getInstance ()
62
+ ->get (StockStatusSelectBuilder::class);
63
+
64
+ $ this ->bundleOptionStockDataSelectBuilder = $ bundleOptionStockDataSelectBuilder ?: ObjectManager::getInstance ()
65
+ ->get (BundleOptionStockDataSelectBuilder::class);
48
66
}
49
67
50
68
/**
@@ -67,46 +85,21 @@ protected function _getBundleOptionTable()
67
85
protected function _prepareBundleOptionStockData ($ entityIds = null , $ usePrimaryTable = false )
68
86
{
69
87
$ this ->_cleanBundleOptionStockData ();
70
- $ linkField = $ this ->getMetadataPool ()-> getMetadata (ProductInterface::class)-> getLinkField ();
88
+ $ connection = $ this ->getConnection ();
71
89
$ table = $ this ->getActionType () === Full::ACTION_TYPE
72
90
? $ this ->getMainTable ()
73
91
: $ this ->indexerStockFrontendResource ->getMainTable ();
74
92
$ idxTable = $ usePrimaryTable ? $ table : $ this ->getIdxTable ();
75
- $ connection = $ this ->getConnection ();
76
- $ select = $ connection ->select ()->from (
77
- ['product ' => $ this ->getTable ('catalog_product_entity ' )],
78
- ['entity_id ' ]
79
- );
80
- $ select ->join (
81
- ['bo ' => $ this ->getTable ('catalog_product_bundle_option ' )],
82
- "bo.parent_id = product. $ linkField " ,
83
- []
84
- );
93
+ $ select = $ this ->bundleOptionStockDataSelectBuilder ->buildSelect ($ idxTable );
94
+
85
95
$ status = new \Zend_Db_Expr (
86
- 'MAX( ' . $ connection ->getCheckSql ('e.required_options = 0 ' , 'i.stock_status ' , '0 ' ) . ') '
87
- );
88
- $ select ->join (
89
- ['cis ' => $ this ->getTable ('cataloginventory_stock ' )],
90
- '' ,
91
- ['website_id ' , 'stock_id ' ]
92
- )->joinLeft (
93
- ['bs ' => $ this ->getTable ('catalog_product_bundle_selection ' )],
94
- 'bs.option_id = bo.option_id ' ,
95
- []
96
- )->joinLeft (
97
- ['i ' => $ idxTable ],
98
- 'i.product_id = bs.product_id AND i.website_id = cis.website_id AND i.stock_id = cis.stock_id ' ,
99
- []
100
- )->joinLeft (
101
- ['e ' => $ this ->getTable ('catalog_product_entity ' )],
102
- 'e.entity_id = bs.product_id ' ,
103
- []
104
- )->group (
105
- ['product.entity_id ' , 'cis.website_id ' , 'cis.stock_id ' , 'bo.option_id ' ]
106
- )->columns (
107
- ['option_id ' => 'bo.option_id ' , 'status ' => $ status ]
96
+ 'MAX( '
97
+ . $ connection ->getCheckSql ('e.required_options = 0 ' , 'i.stock_status ' , '0 ' )
98
+ . ') '
108
99
);
109
100
101
+ $ select ->columns (['status ' => $ status ]);
102
+
110
103
if ($ entityIds !== null ) {
111
104
$ select ->where ('product.entity_id IN(?) ' , $ entityIds );
112
105
}
@@ -136,56 +129,18 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
136
129
{
137
130
$ this ->_prepareBundleOptionStockData ($ entityIds , $ usePrimaryTable );
138
131
$ connection = $ this ->getConnection ();
139
- $ select = parent ::_getStockStatusSelect ($ entityIds , $ usePrimaryTable );
140
- $ metadata = $ this ->getMetadataPool ()->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class);
141
- $ linkField = $ metadata ->getLinkField ();
142
-
143
- $ select ->reset (
144
- \Magento \Framework \DB \Select::COLUMNS
145
- )->columns (
146
- ['e.entity_id ' , 'cis.website_id ' , 'cis.stock_id ' ]
147
- )->joinLeft (
148
- ['o ' => $ this ->_getBundleOptionTable ()],
149
- 'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id ' ,
150
- []
151
- )->joinInner (
152
- ['cpr ' => $ this ->getTable ('catalog_product_relation ' )],
153
- 'e. ' . $ linkField . ' = cpr.parent_id ' ,
154
- []
155
- )->columns (
156
- ['qty ' => new \Zend_Db_Expr ('0 ' )]
157
- );
158
132
159
- if ($ metadata ->getIdentifierField () === $ metadata ->getLinkField ()) {
160
- $ select ->joinInner (
161
- ['cpei ' => $ this ->getTable ('catalog_product_entity_int ' )],
162
- 'cpr.child_id = cpei. ' . $ linkField
163
- . ' AND cpei.attribute_id = ' . $ this ->_getAttribute ('status ' )->getId ()
164
- . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED ,
165
- []
166
- );
167
- } else {
168
- $ select ->joinInner (
169
- ['cpel ' => $ this ->getTable ('catalog_product_entity ' )],
170
- 'cpel.entity_id = cpr.child_id ' ,
171
- []
172
- )->joinInner (
173
- ['cpei ' => $ this ->getTable ('catalog_product_entity_int ' )],
174
- 'cpel. ' . $ linkField . ' = cpei. ' . $ linkField
175
- . ' AND cpei.attribute_id = ' . $ this ->_getAttribute ('status ' )->getId ()
176
- . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED ,
177
- []
178
- );
179
- }
133
+ $ select = parent ::_getStockStatusSelect ($ entityIds , $ usePrimaryTable );
134
+ $ select = $ this ->stockStatusSelectBuilder ->buildSelect ($ select );
180
135
136
+ $ statusNotNullExpr = $ connection ->getCheckSql ('o.stock_status IS NOT NULL ' , 'o.stock_status ' , '0 ' );
181
137
$ statusExpr = $ this ->getStatusExpression ($ connection );
138
+
182
139
$ select ->columns (
183
140
[
184
141
'status ' => $ connection ->getLeastSql (
185
142
[
186
- new \Zend_Db_Expr (
187
- 'MIN( ' . $ connection ->getCheckSql ('o.stock_status IS NOT NULL ' , 'o.stock_status ' , '0 ' ) . ') '
188
- ),
143
+ new \Zend_Db_Expr ('MIN( ' . $ statusNotNullExpr . ') ' ),
189
144
new \Zend_Db_Expr ('MIN( ' . $ statusExpr . ') ' ),
190
145
]
191
146
),
0 commit comments