Skip to content

Commit 1ef474c

Browse files
author
Stanislav Idolov
committed
1 parent 0c82649 commit 1ef474c

File tree

2 files changed

+38
-71
lines changed

2 files changed

+38
-71
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Aggregation/DataProvider/QueryBuilder.php

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration;
1010
use Magento\CatalogInventory\Model\Stock;
1111
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
12-
use Magento\Framework\App\ObjectManager;
1312
use Magento\Framework\App\ResourceConnection;
1413
use Magento\Framework\App\ScopeResolverInterface;
1514
use Magento\Framework\DB\Adapter\AdapterInterface;
1615
use Magento\Framework\DB\Select;
1716
use Magento\Framework\Search\Request\BucketInterface;
1817

1918
/**
20-
* Class for query building for Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider.
19+
* Attribute query builder
2120
*/
2221
class QueryBuilder
2322
{
@@ -36,27 +35,19 @@ class QueryBuilder
3635
*/
3736
private $inventoryConfig;
3837

39-
/**
40-
* @var AdapterInterface
41-
*/
42-
private $connection;
43-
4438
/**
4539
* @param ResourceConnection $resource
4640
* @param ScopeResolverInterface $scopeResolver
47-
* @param CatalogInventoryConfiguration|null $inventoryConfig
41+
* @param CatalogInventoryConfiguration $inventoryConfig
4842
*/
4943
public function __construct(
5044
ResourceConnection $resource,
5145
ScopeResolverInterface $scopeResolver,
52-
CatalogInventoryConfiguration $inventoryConfig = null
46+
CatalogInventoryConfiguration $inventoryConfig
5347
) {
5448
$this->resource = $resource;
5549
$this->scopeResolver = $scopeResolver;
56-
$this->inventoryConfig = $inventoryConfig ?: ObjectManager::getInstance()->get(
57-
CatalogInventoryConfiguration::class
58-
);
59-
$this->connection = $resource->getConnection();
50+
$this->inventoryConfig = $inventoryConfig;
6051
}
6152

6253
/**
@@ -71,86 +62,72 @@ public function __construct(
7162
*/
7263
public function build(
7364
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();
8070
$select->joinInner(
8171
['entities' => $tableName],
8272
'main_table.entity_id = entities.entity_id',
8373
[]
8474
);
8575

8676
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);
10778
}
10879

109-
return $select;
80+
return $this->buildQueryForAttribute($currentScope, $attribute, $select);
11081
}
11182

11283
/**
113-
* Build select if it is price attribute.
84+
* Build select for price attribute.
11485
*
115-
* @param int $websiteId
86+
* @param int $currentScope
11687
* @param int $customerGroupId
11788
* @param Select $select
11889
*
11990
* @return Select
12091
*/
121-
private function buildIfPrice(
122-
$websiteId,
123-
$customerGroupId,
92+
private function buildQueryForPriceAttribute(
93+
int $currentScope,
94+
int $customerGroupId,
12495
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+
126103
$table = $this->resource->getTableName('catalog_product_index_price');
127104
$select->from(['main_table' => $table], null)
128105
->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
129106
->where('main_table.customer_group_id = ?', $customerGroupId)
130-
->where('main_table.website_id = ?', $websiteId);
107+
->where('main_table.website_id = ?', $store->getWebsiteId());
131108

132109
return $select;
133110
}
134111

135112
/**
136-
* Build select if it is not price attribute.
113+
* Build select for attribute.
137114
*
138-
* @param int $currentScopeId
115+
* @param int $currentScope
139116
* @param AbstractAttribute $attribute
140117
* @param Select $select
141118
*
142119
* @return Select
143120
*/
144-
private function buildIfNotPrice(
145-
$currentScopeId,
121+
private function buildQueryForAttribute(
122+
int $currentScope,
146123
AbstractAttribute $attribute,
147124
Select $select
148-
) {
125+
) : Select {
126+
$currentScopeId = $this->scopeResolver->getScope($currentScope)->getId();
149127
$table = $this->resource->getTableName(
150128
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
151129
);
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'])
154131
->distinct()
155132
->joinLeft(
156133
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
@@ -161,23 +138,11 @@ private function buildIfNotPrice(
161138
->where('main_table.store_id = ? ', $currentScopeId);
162139

163140
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);
165142
}
166143

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;
182147
}
183148
}

app/code/Magento/CatalogSearch/Test/Unit/Model/Adapter/Mysql/Aggregation/DataProvider/QueryBuilderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ protected function setUp()
5353
$this->adapterMock = $this->createMock(AdapterInterface::class);
5454
$this->inventoryConfigMock = $this->createMock(CatalogInventoryConfiguration::class);
5555

56-
$this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($this->adapterMock);
56+
$this->resourceConnectionMock->expects($this->atLeastOnce())
57+
->method('getConnection')
58+
->willReturn($this->adapterMock);
5759

5860
$this->model = new QueryBuilder(
5961
$this->resourceConnectionMock,

0 commit comments

Comments
 (0)