Skip to content

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

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation;

use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Model\Stock;
use Magento\Customer\Model\Session;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
Expand All @@ -16,7 +15,6 @@
use Magento\Framework\DB\Select;
use Magento\Framework\Search\Adapter\Mysql\Aggregation\DataProviderInterface;
use Magento\Framework\Search\Request\BucketInterface;
use Magento\Framework\App\ObjectManager;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -48,23 +46,31 @@ class DataProvider implements DataProviderInterface
*/
private $connection;

/**
* @var StockSelectProviderInterface
*/
private $selectProvider;

/**
* @param Config $eavConfig
* @param ResourceConnection $resource
* @param ScopeResolverInterface $scopeResolver
* @param Session $customerSession
* @param StockSelectProviderInterface $selectProvider
*/
public function __construct(
Config $eavConfig,
ResourceConnection $resource,
ScopeResolverInterface $scopeResolver,
Session $customerSession
Session $customerSession,
StockSelectProviderInterface $selectProvider
Copy link
Contributor

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

) {
$this->eavConfig = $eavConfig;
$this->resource = $resource;
$this->connection = $resource->getConnection();
$this->scopeResolver = $scopeResolver;
$this->customerSession = $customerSession;
$this->selectProvider = $selectProvider;
}

/**
Expand Down Expand Up @@ -99,25 +105,7 @@ public function getDataSet(
->where('main_table.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
->where('main_table.website_id = ?', $store->getWebsiteId());
} else {
$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 = $this->getSelect();
$parentSelect->from(['main_table' => $subSelect], ['main_table.value']);
$select = $parentSelect;
$select = $this->selectProvider->get($currentScope, $attribute, $select);
}

return $select;
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is stock select?
why do we need it?
Please describe that in comments. Also select is not good extension point, because you don't know fields and tables in select you can reffecrence to

*
* @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
Expand Up @@ -37,7 +37,7 @@ class FilterMapper
private $visibilityFilter;

/**
* @var StockStatusFilter
* @var StockStatusFilterInterface
*/
private $stockStatusFilter;

Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -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';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are backward incompatible changes

/**
* @var ConditionManager
*/
Expand Down Expand Up @@ -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));
}

Expand Down
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';
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
Loading