Skip to content

Commit

Permalink
5325 Improved code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
denisprotassoff committed Dec 1, 2022
1 parent aa0953c commit c90394d
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/DataProvider/Product/SearchCriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@ class SearchCriteriaBuilder extends MagentoSearchCriteriaBuilder
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
protected $scopeConfig;

/**
* @var FilterBuilder
*/
private $filterBuilder;
protected $filterBuilder;

/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
protected $filterGroupBuilder;

/**
* @var Builder
*/
private $builder;
protected $builder;
/**
* @var Visibility
*/
private $visibility;
protected $visibility;

/**
* @var SortOrderBuilder
*/
private $sortOrderBuilder;
protected $sortOrderBuilder;

/**
* @param Builder $builder
Expand Down Expand Up @@ -100,6 +100,7 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
} else {
$requestName = 'graphql_product_search';
}

$searchCriteria->setRequestName($requestName);

if ($isSearch) {
Expand All @@ -111,6 +112,7 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
}

$this->addEntityIdSort($searchCriteria, $args);
// Removed $isFilter parameter
$this->addVisibilityFilter($searchCriteria, $isSearch);

$searchCriteria->setCurrentPage($args['currentPage']);
Expand All @@ -127,8 +129,9 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
* @param bool $isSearch
* @param bool $isFilter
*/
private function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bool $isSearch): void
protected function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bool $isSearch): void
{
// Removed $isFilter parameter and related check
$visibilityIds = $isSearch
? $this->visibility->getVisibleInSearchIds()
: $this->visibility->getVisibleInCatalogIds();
Expand All @@ -142,7 +145,7 @@ private function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bo
* @param SearchCriteriaInterface $searchCriteria
* @param array $args
*/
private function addEntityIdSort(SearchCriteriaInterface $searchCriteria, array $args): void
protected function addEntityIdSort(SearchCriteriaInterface $searchCriteria, array $args): void
{
$sortOrder = !empty($args['sort']) ? reset($args['sort']) : SortOrder::SORT_DESC;
$sortOrderArray = $searchCriteria->getSortOrders();
Expand All @@ -159,12 +162,13 @@ private function addEntityIdSort(SearchCriteriaInterface $searchCriteria, array
* @param SearchCriteriaInterface $searchCriteria
* @return void
*/
private function preparePriceAggregation(SearchCriteriaInterface $searchCriteria): void
protected function preparePriceAggregation(SearchCriteriaInterface $searchCriteria): void
{
$priceRangeCalculation = $this->scopeConfig->getValue(
\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($priceRangeCalculation) {
$this->addFilter($searchCriteria, 'price_dynamic_algorithm', $priceRangeCalculation);
}
Expand All @@ -178,7 +182,7 @@ private function preparePriceAggregation(SearchCriteriaInterface $searchCriteria
* @param mixed $value
* @param string|null $condition
*/
private function addFilter(
protected function addFilter(
SearchCriteriaInterface $searchCriteria,
string $field,
$value,
Expand All @@ -203,16 +207,18 @@ private function addFilter(
* @param array $args
* @param bool $isSearch
*/
private function addDefaultSortOrder(SearchCriteriaInterface $searchCriteria, array $args, $isSearch = false): void
protected function addDefaultSortOrder(SearchCriteriaInterface $searchCriteria, array $args, $isSearch = false): void
{
$defaultSortOrder = [];

if ($isSearch) {
$defaultSortOrder[] = $this->sortOrderBuilder
->setField('relevance')
->setDirection(SortOrder::SORT_DESC)
->create();
} else {
$categoryIdFilter = isset($args['filter']['category_id']) ? $args['filter']['category_id'] : false;

if ($categoryIdFilter) {
if (!is_array($categoryIdFilter[array_key_first($categoryIdFilter)])
|| count($categoryIdFilter[array_key_first($categoryIdFilter)]) <= 1
Expand All @@ -235,11 +241,13 @@ private function addDefaultSortOrder(SearchCriteriaInterface $searchCriteria, ar
*
* @param SearchCriteriaInterface $searchCriteria
*/
private function updateRangeFilters(SearchCriteriaInterface $searchCriteria): void
protected function updateRangeFilters(SearchCriteriaInterface $searchCriteria): void
{
$filterGroups = $searchCriteria->getFilterGroups();

foreach ($filterGroups as $filterGroup) {
$filters = $filterGroup->getFilters();

foreach ($filters as $filter) {
if (in_array($filter->getConditionType(), ['from', 'to'])) {
$filter->setField($filter->getField() . '.' . $filter->getConditionType());
Expand Down

0 comments on commit c90394d

Please sign in to comment.