From dc402a40a9c6eebfa4379123aa54821a7f8863e3 Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Mon, 24 Aug 2020 15:00:23 +0300 Subject: [PATCH] Removed collection processors which were not working properly. --- .../CollectionProcessor/PriceProcessor.php | 7 +- .../CollectionProcessor/FilterProcessor.php | 126 ----------------- .../FilterProcessor/CategoryFilter.php | 115 ---------------- .../FilterProcessor/ConditionsFilter.php | 129 ------------------ .../ConfigurableProductAttributeFilter.php | 113 --------------- .../FilterProcessor/CustomerGroupFilter.php | 53 ------- .../FilterProcessor/ProductIdFilter.php | 42 ------ src/etc/graphql/di.xml | 38 ++---- 8 files changed, 12 insertions(+), 611 deletions(-) delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor.php delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CategoryFilter.php delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConditionsFilter.php delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConfigurableProductAttributeFilter.php delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CustomerGroupFilter.php delete mode 100644 src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ProductIdFilter.php diff --git a/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php b/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php index 7420e49..f821f27 100644 --- a/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php +++ b/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php @@ -10,7 +10,7 @@ use Magento\Catalog\Model\ResourceModel\Product\Collection; use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface; use Magento\Framework\Api\SearchCriteriaInterface; -use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CustomerGroupFilter; + /** * Adds price data to product collection * @@ -19,6 +19,7 @@ class PriceProcessor implements CollectionProcessorInterface { const PRICE_FIELD = 'price_range'; + const FLAG_CUSTOMER_GROUP_PRICE_ADDED = 'customer_group_price_data_added'; /** * {@inheritdoc} @@ -28,7 +29,7 @@ public function process( SearchCriteriaInterface $searchCriteria, array $attributeNames ): Collection { - $isPriceDataAdded = $collection->getFlag(CustomerGroupFilter::FLAG_CUSTOMER_GROUP_PRICE_ADDED); + $isPriceDataAdded = $collection->getFlag(self::FLAG_CUSTOMER_GROUP_PRICE_ADDED); // add tax percent, no-matter what $collection->addTaxPercents(); @@ -44,4 +45,4 @@ public function process( return $collection; } -} \ No newline at end of file +} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor.php deleted file mode 100644 index c617fde..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor.php +++ /dev/null @@ -1,126 +0,0 @@ - - * @copyright Copyright (c) 2019 Scandiweb, Ltd (https://scandiweb.com) - */ -namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor; - -use Magento\Framework\Api\SearchCriteriaInterface; -use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; -use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; -use Magento\Framework\Api\Search\FilterGroup; -use Magento\Framework\Data\Collection\AbstractDb; - -class FilterProcessor implements CollectionProcessorInterface -{ - /** - * @var CustomFilterInterface[] - */ - private $customFilters; - - /** - * @var array - */ - private $fieldMapping; - - /** - * @var CustomFilterInterface - */ - private $defaultFilter; - - - /** - * @param CustomFilterInterface $defaultFilter - * @param CustomFilterInterface[] $customFilters - * @param array $fieldMapping - */ - public function __construct( - CustomFilterInterface $defaultFilter, - array $customFilters = [], - array $fieldMapping = [] - ) { - $this->defaultFilter = $defaultFilter; - $this->customFilters = $customFilters; - $this->fieldMapping = $fieldMapping; - } - - /** - * Apply Search Criteria Filters to collection - * - * @param SearchCriteriaInterface $searchCriteria - * @param AbstractDb $collection - * @return void - */ - public function process(SearchCriteriaInterface $searchCriteria, AbstractDb $collection) - { - foreach ($searchCriteria->getFilterGroups() as $group) { - $this->addFilterGroupToCollection($group, $collection); - } - } - - /** - * Add FilterGroup to the collection - * - * @param FilterGroup $filterGroup - * @param AbstractDb $collection - * @return void - */ - private function addFilterGroupToCollection( - FilterGroup $filterGroup, - AbstractDb $collection - ) { - foreach ($filterGroup->getFilters() as $filter) { - $isApplied = false; - $customFilter = $this->getCustomFilterForField($filter->getField()); - - if ($customFilter) { - $isApplied = $customFilter->apply($filter, $collection); - } - - if (!$isApplied) { - $filter->setField($this->getFieldMapping($filter->getField())); - $this->defaultFilter->apply($filter, $collection); - } - } - } - - /** - * Return custom filters for field if exists - * - * @param string $field - * @return CustomFilterInterface|null - * @throws \InvalidArgumentException - */ - private function getCustomFilterForField($field) - { - $filter = null; - if (isset($this->customFilters[$field])) { - $filter = $this->customFilters[$field]; - if (!($this->customFilters[$field] instanceof CustomFilterInterface)) { - throw new \InvalidArgumentException( - sprintf( - 'Filter for %s must implement %s interface.', - $field, - CustomFilterInterface::class - ) - ); - } - } - return $filter; - } - - /** - * Return mapped field name - * - * @param string $field - * @return string - */ - private function getFieldMapping($field) - { - return isset($this->fieldMapping[$field]) ? $this->fieldMapping[$field] : $field; - } -} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CategoryFilter.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CategoryFilter.php deleted file mode 100644 index 0743a03..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CategoryFilter.php +++ /dev/null @@ -1,115 +0,0 @@ - - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) - */ - -declare(strict_types=1); - -namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; - -use Magento\Catalog\Model\CategoryFactory; -use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel; -use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryResourceCollection; -use Magento\Catalog\Model\ResourceModel\Product\Collection; -use Magento\Framework\Api\Filter; -use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; -use Magento\Framework\Data\Collection\AbstractDb; -use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Registry; - -/** - * Category filter allows to filter products collection using custom defined filters from search criteria. - */ -class CategoryFilter implements CustomFilterInterface -{ - /** - * @var CategoryFactory - */ - private $categoryFactory; - - /** - * @var CategoryResourceModel - */ - private $categoryResourceModel; - - /** - * @var CategoryResourceCollection - */ - private $categoryResourceCollection; - - /** - * @var Registry - */ - private $registry; - - /** - * CategoryFilter constructor. - * @param CategoryFactory $categoryFactory - * @param CategoryResourceModel $categoryResourceModel - * @param CategoryResourceCollection $categoryResourceCollection - * @param Registry $registry - */ - public function __construct( - CategoryFactory $categoryFactory, - CategoryResourceModel $categoryResourceModel, - CategoryResourceCollection $categoryResourceCollection, - Registry $registry - ) { - $this->categoryFactory = $categoryFactory; - $this->categoryResourceModel = $categoryResourceModel; - $this->categoryResourceCollection = $categoryResourceCollection; - $this->registry = $registry; - } - - /** - * Apply filter by custom field to product collection. - * - * For anchor categories, the products from all children categories will be present in the result. - * - * @param Filter $filter - * @param AbstractDb $collection - * @return bool Whether the filter is applied - * @throws LocalizedException - */ - public function apply(Filter $filter, AbstractDb $collection) - { - if ($this->registry->registry('current_category')) { - return true; - } - - $conditionType = $filter->getConditionType(); - $rawFilterField = $filter->getField(); - - if ($conditionType !== 'eq') { - throw new LocalizedException(__($rawFilterField . " only supports 'eq' condition type.")); - } - - $filterField = str_replace("category_", "", $rawFilterField); - $filterValue = $filter->getValue(); - - /** @var Collection $collection */ - $category = $this->categoryFactory->create(); - - if ($filterField !== 'id') { - $categoryId = $this->categoryResourceCollection - ->addAttributeToFilter($filterField, $filterValue) - ->addAttributeToSelect(['entity_id']) - ->getFirstItem() - ->getEntityId(); - - $this->categoryResourceModel->load($category, $categoryId); - } else { - $this->categoryResourceModel->load($category, $filterValue); - } - - $this->registry->register('current_category', $category); - $collection->addCategoryFilter($category); - - return true; - } -} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConditionsFilter.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConditionsFilter.php deleted file mode 100644 index c3028c3..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConditionsFilter.php +++ /dev/null @@ -1,129 +0,0 @@ -conditionsHelper = $conditionsHelper; - $this->rule = $rule; - $this->sqlBuilder = $sqlBuilder; - $this->collectionFactory = $collectionFactory; - } - - /** - * Get conditions - * - * @return \Magento\Rule\Model\Condition\Combine - */ - protected function getConditions($conditions) - { - $conditions = $this->conditionsHelper->decode($conditions); - - foreach ($conditions as $key => $condition) { - if (!empty($condition['attribute']) - && in_array($condition['attribute'], ['special_from_date', 'special_to_date']) - ) { - $conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value'])); - } - } - - $this->rule->loadPost(['conditions' => $conditions]); - - return $this->rule->getConditions(); - } - - /** - * @inheritDoc - */ - public function apply(Filter $filter, AbstractDb $collection) - { - $conditionType = $filter->getConditionType(); - $rawFilterField = $filter->getField(); - - if ($conditionType !== 'eq') { - throw new LocalizedException(__($rawFilterField . " only supports 'eq' condition type.")); - } - - $conditions = base64_decode($filter->getValue()); - $conditions = $this->getConditions($conditions); - - $simpleSelect = clone $collection; - $conditions->collectValidatedAttributes($simpleSelect); - $this->sqlBuilder->attachConditionToCollection($simpleSelect, $conditions); - - $simpleSelect->addFieldToFilter('status', Status::STATUS_ENABLED); - $simpleSelect->getSelect() - ->reset(\Zend_Db_Select::COLUMNS) - ->columns(['e.entity_id']); - - $configurableProductCollection = $this->collectionFactory->create(); - $select = $configurableProductCollection->getConnection() - ->select() - ->distinct() - ->from(['l' => 'catalog_product_super_link'], 'l.product_id') - ->join( - ['k' => $configurableProductCollection->getTable('catalog_product_entity')], - `k.entity_id = l.parent_id` - ) - ->where($configurableProductCollection->getConnection()->prepareSqlCondition( - 'l.product_id', - ['in' => $simpleSelect->getSelect()] - )) - ->reset(\Zend_Db_Select::COLUMNS) - ->columns(['l.parent_id']); - - $unionCollection = $this->collectionFactory->create(); - $unionSelect = $unionCollection->getConnection() - ->select() - ->union([$simpleSelect->getSelect(), $select]); - - $collection->getSelect() - ->where($collection->getConnection()->prepareSqlCondition( - 'e.entity_id', - ['in' => $unionSelect] - )); - - return true; - } -} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConfigurableProductAttributeFilter.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConfigurableProductAttributeFilter.php deleted file mode 100644 index 16697a8..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConfigurableProductAttributeFilter.php +++ /dev/null @@ -1,113 +0,0 @@ - - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) - */ - -declare(strict_types=1); - -namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; - -use Magento\Catalog\Model\Product\Attribute\Source\Status; -use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; -use Magento\Framework\Data\Collection\AbstractDb; -use Magento\Framework\Api\Filter; -use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; -use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable; -use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CriteriaCheck; - -/** - * Category filter allows to filter products collection using custom defined filters from search criteria. - */ -class ConfigurableProductAttributeFilter implements CustomFilterInterface -{ - /** - * @var Configurable - */ - protected $configurable; - /** - * @var CollectionFactory - */ - protected $collectionFactory; - - /** - * ConfigurableProductAttributeFilter constructor. - * @param Configurable $configurable - * @param CollectionFactory $collectionFactory - */ - public function __construct( - Configurable $configurable, - CollectionFactory $collectionFactory - ) { - $this->configurable = $configurable; - $this->collectionFactory = $collectionFactory; - } - - protected function isJoinNeeded($filter): bool { - $type = $filter->getConditionType(); - $field = $filter->getField(); - - return ( - ($type === 'in' && in_array($field, ['id', 'entity_id', 'sku'])) - || ($type === 'eq' && in_array($field, ['url_key', 'id', 'entity_id', 'sku'])) - ); - } - - /** - * @param Filter $filter - * @param AbstractDb $collection - * @return bool - * @throws \Zend_Db_Select_Exception - */ - public function apply(Filter $filter, AbstractDb $collection) - { - $attributeName = $filter->getField(); - $attributeValue = $filter->getValue(); - $conditionType = $filter->getConditionType(); - - if ($this->isJoinNeeded($filter)) { - $collection->addFieldToFilter($attributeName, [$conditionType => $attributeValue]); - return true; - } - - $simpleSelect = $this->collectionFactory->create() - ->addAttributeToFilter($attributeName, [$conditionType => $attributeValue]) - ->addAttributeToFilter('status', Status::STATUS_ENABLED); - - $simpleSelect->getSelect() - ->reset(\Zend_Db_Select::COLUMNS) - ->columns(['e.entity_id']); - - $configurableProductCollection = $this->collectionFactory->create(); - $select = $configurableProductCollection->getConnection() - ->select() - ->distinct() - ->from(['l' => 'catalog_product_super_link'], 'l.product_id') - ->join( - ['k' => $configurableProductCollection->getTable('catalog_product_entity')], - 'k.entity_id = l.parent_id' - )->where($configurableProductCollection->getConnection()->prepareSqlCondition( - 'l.product_id', - ['in' => $simpleSelect->getSelect()] - )) - ->reset(\Zend_Db_Select::COLUMNS) - ->columns(['l.parent_id']); - - $unionCollection = $this->collectionFactory->create(); - $unionSelect = $unionCollection->getConnection() - ->select() - ->union([$simpleSelect->getSelect(), $select]); - - $collection->getSelect() - ->where($collection->getConnection()->prepareSqlCondition( - 'e.entity_id', - ['in' => $unionSelect] - )); - - return true; - } -} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CustomerGroupFilter.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CustomerGroupFilter.php deleted file mode 100644 index 2e45dc6..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/CustomerGroupFilter.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) - */ - -declare(strict_types=1); - -namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; - -use Magento\Catalog\Model\ResourceModel\Product\Collection; -use Magento\Framework\Api\Filter; -use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; -use Magento\Framework\Data\Collection\AbstractDb; -use Magento\Framework\Exception\LocalizedException; - -/** - * Class CustomerGroupFilter - * @package ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor - */ -class CustomerGroupFilter implements CustomFilterInterface -{ - const FLAG_CUSTOMER_GROUP_PRICE_ADDED = 'customer_group_price_data_added'; - - /** - * Apply filter by custom field to product collection. - * - * @param Filter $filter - * @param AbstractDb $collection - * @return bool Whether the filter is applied - * @throws LocalizedException - */ - public function apply(Filter $filter, AbstractDb $collection) - { - $conditionType = $filter->getConditionType(); - $rawFilterField = $filter->getField(); - - if ($conditionType !== 'eq') { - throw new LocalizedException(__($rawFilterField . " only supports 'eq' condition type.")); - } - - /** @var $collection Collection */ - $collection - ->addPriceData($filter->getValue()) - ->setFlag(self::FLAG_CUSTOMER_GROUP_PRICE_ADDED, true); - - return true; - } -} diff --git a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ProductIdFilter.php b/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ProductIdFilter.php deleted file mode 100644 index ec78054..0000000 --- a/src/Model/Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ProductIdFilter.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) - */ - -declare(strict_types=1); - -namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; - -use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; -use Magento\Framework\Data\Collection\AbstractDb; -use Magento\Framework\Api\Filter; - -/** - * Product filter allows to filter products collection using custom defined filters from search criteria. - */ -class ProductIdFilter implements CustomFilterInterface -{ - /** - * @param Filter $filter - * @param AbstractDb $collection - * @return bool - */ - public function apply(Filter $filter, AbstractDb $collection) - { - $attributeValue = $filter->getValue(); - $conditionType = $filter->getConditionType(); - - $collection->getSelect() - ->where($collection->getConnection()->prepareSqlCondition( - 'e.entity_id', - [$conditionType => $attributeValue] - )); - - return true; - } -} diff --git a/src/etc/graphql/di.xml b/src/etc/graphql/di.xml index 27f52ac..a72aacd 100644 --- a/src/etc/graphql/di.xml +++ b/src/etc/graphql/di.xml @@ -11,33 +11,14 @@ */ --> - - - - - - - - - - - - - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ConditionsFilter - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CustomerGroupFilter - - Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter - Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter - Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductWebsiteFilter - - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductIdFilter - - ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ConfigurableProductAttributeFilter - - + + + @@ -47,9 +28,6 @@ - -