9
9
use Magento \CatalogInventory \Model \Configuration as CatalogInventoryConfiguration ;
10
10
use Magento \CatalogInventory \Model \Stock ;
11
11
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
12
- use Magento \Framework \App \ObjectManager ;
13
12
use Magento \Framework \App \ResourceConnection ;
14
13
use Magento \Framework \App \ScopeResolverInterface ;
15
14
use Magento \Framework \DB \Adapter \AdapterInterface ;
16
15
use Magento \Framework \DB \Select ;
17
16
use Magento \Framework \Search \Request \BucketInterface ;
18
17
19
18
/**
20
- * Class for query building for Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider.
19
+ * Attribute query builder
21
20
*/
22
21
class QueryBuilder
23
22
{
@@ -36,27 +35,19 @@ class QueryBuilder
36
35
*/
37
36
private $ inventoryConfig ;
38
37
39
- /**
40
- * @var AdapterInterface
41
- */
42
- private $ connection ;
43
-
44
38
/**
45
39
* @param ResourceConnection $resource
46
40
* @param ScopeResolverInterface $scopeResolver
47
- * @param CatalogInventoryConfiguration|null $inventoryConfig
41
+ * @param CatalogInventoryConfiguration $inventoryConfig
48
42
*/
49
43
public function __construct (
50
44
ResourceConnection $ resource ,
51
45
ScopeResolverInterface $ scopeResolver ,
52
- CatalogInventoryConfiguration $ inventoryConfig = null
46
+ CatalogInventoryConfiguration $ inventoryConfig
53
47
) {
54
48
$ this ->resource = $ resource ;
55
49
$ this ->scopeResolver = $ scopeResolver ;
56
- $ this ->inventoryConfig = $ inventoryConfig ?: ObjectManager::getInstance ()->get (
57
- CatalogInventoryConfiguration::class
58
- );
59
- $ this ->connection = $ resource ->getConnection ();
50
+ $ this ->inventoryConfig = $ inventoryConfig ;
60
51
}
61
52
62
53
/**
@@ -71,86 +62,72 @@ public function __construct(
71
62
*/
72
63
public function build (
73
64
AbstractAttribute $ attribute ,
74
- $ tableName ,
75
- $ currentScope ,
76
- $ customerGroupId
77
- ) {
78
- $ select = $ this ->getSelect ();
79
-
65
+ string $ tableName ,
66
+ int $ currentScope ,
67
+ int $ customerGroupId
68
+ ) : Select {
69
+ $ select = $ this ->resource ->getConnection ()->select ();
80
70
$ select ->joinInner (
81
71
['entities ' => $ tableName ],
82
72
'main_table.entity_id = entities.entity_id ' ,
83
73
[]
84
74
);
85
75
86
76
if ($ attribute ->getAttributeCode () === 'price ' ) {
87
- /** @var \Magento\Store\Model\Store $store */
88
- $ store = $ this ->scopeResolver ->getScope ($ currentScope );
89
- if (!$ store instanceof \Magento \Store \Model \Store) {
90
- throw new \RuntimeException ('Illegal scope resolved ' );
91
- }
92
-
93
- $ select = $ this ->buildIfPrice (
94
- $ store ->getWebsiteId (),
95
- $ customerGroupId ,
96
- $ select
97
- );
98
- } else {
99
- $ currentScopeId = $ this ->scopeResolver ->getScope ($ currentScope )
100
- ->getId ();
101
-
102
- $ select = $ this ->buildIfNotPrice (
103
- $ currentScopeId ,
104
- $ attribute ,
105
- $ select
106
- );
77
+ return $ this ->buildQueryForPriceAttribute ($ currentScope , $ customerGroupId , $ select );
107
78
}
108
79
109
- return $ select ;
80
+ return $ this -> buildQueryForAttribute ( $ currentScope , $ attribute , $ select) ;
110
81
}
111
82
112
83
/**
113
- * Build select if it is price attribute.
84
+ * Build select for price attribute.
114
85
*
115
- * @param int $websiteId
86
+ * @param int $currentScope
116
87
* @param int $customerGroupId
117
88
* @param Select $select
118
89
*
119
90
* @return Select
120
91
*/
121
- private function buildIfPrice (
122
- $ websiteId ,
123
- $ customerGroupId ,
92
+ private function buildQueryForPriceAttribute (
93
+ int $ currentScope ,
94
+ int $ customerGroupId ,
124
95
Select $ select
125
- ) {
96
+ ) : Select {
97
+ /** @var \Magento\Store\Model\Store $store */
98
+ $ store = $ this ->scopeResolver ->getScope ($ currentScope );
99
+ if (!$ store instanceof \Magento \Store \Model \Store) {
100
+ throw new \RuntimeException ('Illegal scope resolved ' );
101
+ }
102
+
126
103
$ table = $ this ->resource ->getTableName ('catalog_product_index_price ' );
127
104
$ select ->from (['main_table ' => $ table ], null )
128
105
->columns ([BucketInterface::FIELD_VALUE => 'main_table.min_price ' ])
129
106
->where ('main_table.customer_group_id = ? ' , $ customerGroupId )
130
- ->where ('main_table.website_id = ? ' , $ websiteId );
107
+ ->where ('main_table.website_id = ? ' , $ store -> getWebsiteId () );
131
108
132
109
return $ select ;
133
110
}
134
111
135
112
/**
136
- * Build select if it is not price attribute.
113
+ * Build select for attribute.
137
114
*
138
- * @param int $currentScopeId
115
+ * @param int $currentScope
139
116
* @param AbstractAttribute $attribute
140
117
* @param Select $select
141
118
*
142
119
* @return Select
143
120
*/
144
- private function buildIfNotPrice (
145
- $ currentScopeId ,
121
+ private function buildQueryForAttribute (
122
+ int $ currentScope ,
146
123
AbstractAttribute $ attribute ,
147
124
Select $ select
148
- ) {
125
+ ) : Select {
126
+ $ currentScopeId = $ this ->scopeResolver ->getScope ($ currentScope )->getId ();
149
127
$ table = $ this ->resource ->getTableName (
150
128
'catalog_product_index_eav ' . ($ attribute ->getBackendType () === 'decimal ' ? '_decimal ' : '' )
151
129
);
152
- $ subSelect = $ select ;
153
- $ subSelect ->from (['main_table ' => $ table ], ['main_table.entity_id ' , 'main_table.value ' ])
130
+ $ select ->from (['main_table ' => $ table ], ['main_table.entity_id ' , 'main_table.value ' ])
154
131
->distinct ()
155
132
->joinLeft (
156
133
['stock_index ' => $ this ->resource ->getTableName ('cataloginventory_stock_status ' )],
@@ -161,23 +138,11 @@ private function buildIfNotPrice(
161
138
->where ('main_table.store_id = ? ' , $ currentScopeId );
162
139
163
140
if (!$ this ->inventoryConfig ->isShowOutOfStock ($ currentScopeId )) {
164
- $ subSelect ->where ('stock_index.stock_status = ? ' , Stock::STOCK_IN_STOCK );
141
+ $ select ->where ('stock_index.stock_status = ? ' , Stock::STOCK_IN_STOCK );
165
142
}
166
143
167
- $ parentSelect = $ this ->getSelect ();
168
- $ parentSelect ->from (['main_table ' => $ subSelect ], ['main_table.value ' ]);
169
- $ select = $ parentSelect ;
170
-
171
- return $ select ;
172
- }
173
-
174
- /**
175
- * Get empty select.
176
- *
177
- * @return Select
178
- */
179
- private function getSelect ()
180
- {
181
- return $ this ->connection ->select ();
144
+ $ parentSelect = $ this ->resource ->getConnection ()->select ();
145
+ $ parentSelect ->from (['main_table ' => $ select ], ['main_table.value ' ]);
146
+ return $ parentSelect ;
182
147
}
183
148
}
0 commit comments