-
Notifications
You must be signed in to change notification settings - Fork 253
MSI: #302 (Refactoring CatalogSearch Module) #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5138282
83ef9f6
790dda1
0ee076f
8d0ffae
dacc674
44984da
37914bd
1d4b00b
f3adef2
d55e388
2d28b67
ac7f448
cc7e3fd
2decb67
df8d71d
ed92322
436888c
82ce1ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation; | ||
|
||
use Magento\CatalogInventory\Model\Stock; | ||
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\App\ScopeResolverInterface; | ||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class StockSelectProvider implements StockSelectProviderInterface | ||
{ | ||
|
||
/** | ||
* @var Resource | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @var ScopeResolverInterface | ||
*/ | ||
private $scopeResolver; | ||
|
||
/** | ||
* @param ResourceConnection $resource | ||
* @param ScopeResolverInterface $scopeResolver | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
ScopeResolverInterface $scopeResolver | ||
) { | ||
$this->resource = $resource; | ||
$this->scopeResolver = $scopeResolver; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function get(int $currentScope, AbstractAttribute $attribute, Select $select): Select | ||
{ | ||
$connection = $this->resource->getConnection(); | ||
$currentScopeId = $this->scopeResolver->getScope($currentScope) | ||
->getId(); | ||
$table = $this->resource->getTableName( | ||
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '') | ||
); | ||
$subSelect = $select; | ||
$subSelect->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value']) | ||
->distinct() | ||
->joinLeft( | ||
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')], | ||
'main_table.source_id = stock_index.product_id', | ||
[] | ||
) | ||
->where('main_table.attribute_id = ?', $attribute->getAttributeId()) | ||
->where('main_table.store_id = ? ', $currentScopeId) | ||
->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK); | ||
$parentSelect = $connection->select(); | ||
$parentSelect->from(['main_table' => $subSelect], ['main_table.value']); | ||
$select = $parentSelect; | ||
return $select; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation; | ||
|
||
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; | ||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* Allow to use different stock query (Service Provider Interface - SPI) | ||
* @api | ||
*/ | ||
interface StockSelectProviderInterface | ||
{ | ||
/** | ||
* Returns the stock select by current scope | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is stock select? |
||
* | ||
* @param int $currentScope | ||
* @param AbstractAttribute $attribute | ||
* @param Select $select | ||
* @return Select | ||
*/ | ||
public function get(int $currentScope, AbstractAttribute $attribute, Select $select): Select; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ class FilterMapper | |
private $visibilityFilter; | ||
|
||
/** | ||
* @var StockStatusFilter | ||
* @var StockStatusFilterInterface | ||
*/ | ||
private $stockStatusFilter; | ||
|
||
|
@@ -46,14 +46,14 @@ class FilterMapper | |
* @param CustomAttributeFilter $customAttributeFilter | ||
* @param FilterStrategyInterface $filterStrategy | ||
* @param VisibilityFilter $visibilityFilter | ||
* @param StockStatusFilter $stockStatusFilter | ||
* @param StockStatusFilterInterface $stockStatusFilter | ||
*/ | ||
public function __construct( | ||
AliasResolver $aliasResolver, | ||
CustomAttributeFilter $customAttributeFilter, | ||
FilterStrategyInterface $filterStrategy, | ||
VisibilityFilter $visibilityFilter, | ||
StockStatusFilter $stockStatusFilter | ||
StockStatusFilterInterface $stockStatusFilter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the changes to existing code of catalog search - supposed to be backward compatible |
||
) { | ||
$this->aliasResolver = $aliasResolver; | ||
$this->customAttributeFilter = $customAttributeFilter; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\CatalogSearch\Model\Search\FilterMapper; | ||
|
||
use Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Select; | ||
|
||
class StockJoinProvider implements StockJoinProviderInterface | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct(ResourceConnection $resourceConnection) | ||
{ | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function add(Select $select, $alias) | ||
{ | ||
$stockAlias = $alias . AliasResolver::STOCK_FILTER_SUFFIX; | ||
$select->joinLeft( | ||
[ | ||
$stockAlias => $this->resourceConnection->getTableName('cataloginventory_stock_status'), | ||
], | ||
sprintf('%2$s.product_id = %1$s.source_id', $alias, $stockAlias), | ||
[] | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\CatalogSearch\Model\Search\FilterMapper; | ||
|
||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* SPI Interface to change the stock join | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. description of the interface looks awkward. It's not clear the main responsibility of this entity and why we need it |
||
*/ | ||
interface StockJoinProviderInterface | ||
{ | ||
|
||
/** | ||
* Add stock join to the given select. | ||
* | ||
* @param Select $select | ||
* @param $alias | ||
* @return void | ||
*/ | ||
public function add(Select $select, $alias); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need the interface which adds JOIN to some Select object? Why it's not enough just to introduce table resolver, which returns stock index table based on context ? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,29 +16,13 @@ | |
* Class StockStatusFilter | ||
* Adds filter by stock status to base select | ||
*/ | ||
class StockStatusFilter | ||
class StockStatusFilter implements StockStatusFilterInterface | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* Defines strategy of how filter should be applied | ||
* | ||
* Stock status filter will be applied only on parent products | ||
* (e.g. only for configurable products, without options) | ||
*/ | ||
const FILTER_JUST_ENTITY = 'general_filter'; | ||
|
||
/** | ||
* Defines strategy of how filter should be applied | ||
* | ||
* Stock status filter will be applied on parent products with its child | ||
* (e.g. for configurable products and options) | ||
*/ | ||
const FILTER_ENTITY_AND_SUB_PRODUCTS = 'filter_with_sub_products'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are backward incompatible changes |
||
/** | ||
* @var ConditionManager | ||
*/ | ||
|
@@ -84,7 +68,7 @@ public function __construct( | |
*/ | ||
public function apply(Select $select, $stockValues, $type, $showOutOfStockFlag) | ||
{ | ||
if ($type !== self::FILTER_JUST_ENTITY && $type !== self::FILTER_ENTITY_AND_SUB_PRODUCTS) { | ||
if ($type !== static::FILTER_JUST_ENTITY && $type !== static::FILTER_ENTITY_AND_SUB_PRODUCTS) { | ||
throw new \InvalidArgumentException(sprintf('Invalid filter type: %s', $type)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\CatalogSearch\Model\Search\FilterMapper; | ||
|
||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* Adds filter by stock status to base select (SPI) | ||
* @api | ||
*/ | ||
interface StockStatusFilterInterface | ||
{ | ||
/** | ||
* Defines strategy of how filter should be applied | ||
* | ||
* Stock status filter will be applied only on parent products | ||
* (e.g. only for configurable products, without options) | ||
*/ | ||
const FILTER_JUST_ENTITY = 'general_filter'; | ||
const FILTER_ENTITY_AND_SUB_PRODUCTS = 'filter_with_sub_products'; | ||
const FILTER_JUST_SUB_PRODUCTS = 'filter_just_sub_products'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's sounds like violation of modularity principles |
||
|
||
/** | ||
* Adds filter by stock status to base select | ||
* | ||
* @param Select $select | ||
* @param mixed $stockValues | ||
* @param string $type | ||
* @param bool $showOutOfStockFlag | ||
* @return Select | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function apply(Select $select, $stockValues, $type, $showOutOfStockFlag); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new parameter must me optional