Passed
Push — master ( cc4e78...1262d4 )
by Damian
04:08
created

SearchProductsQueryBuilder::buildQuery()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 35
rs 9.2088
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder;
6
7
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\SearchPropertyNameResolverRegistryInterface;
8
use Elastica\Query\AbstractQuery;
9
use Elastica\Query\BoolQuery;
10
use Elastica\Query\MultiMatch;
11
use Sylius\Component\Locale\Context\LocaleContextInterface;
12
13
final class SearchProductsQueryBuilder implements QueryBuilderInterface
14
{
15
    public const QUERY_KEY = 'query';
16
17
    /** @var SearchPropertyNameResolverRegistryInterface */
18
    private $searchProperyNameResolverRegistry;
19
20
    /** @var LocaleContextInterface */
21
    private $localeContext;
22
23
    /** @var QueryBuilderInterface */
24
    private $isEnabledQueryBuilder;
25
26
    /** @var QueryBuilderInterface */
27
    private $hasChannelQueryBuilder;
28
29
    public function __construct(
30
        SearchPropertyNameResolverRegistryInterface $searchProperyNameResolverRegistry,
31
        LocaleContextInterface $localeContext,
32
        QueryBuilderInterface $isEnabledQueryBuilder,
33
        QueryBuilderInterface $hasChannelQueryBuilder
34
    ) {
35
        $this->searchProperyNameResolverRegistry = $searchProperyNameResolverRegistry;
36
        $this->localeContext = $localeContext;
37
        $this->isEnabledQueryBuilder = $isEnabledQueryBuilder;
38
        $this->hasChannelQueryBuilder = $hasChannelQueryBuilder;
39
    }
40
41
    public function buildQuery(array $data): ?AbstractQuery
42
    {
43
        if (!array_key_exists(self::QUERY_KEY, $data)) {
44
            throw new \RuntimeException(
45
                sprintf(
46
                    'Could not build search products query because there\'s no "query" key in provided data. ' .
47
                    'Got the following keys: %s',
48
                    implode(', ', array_keys($data))
49
                )
50
            );
51
        }
52
        $query = $data[self::QUERY_KEY];
53
        if (!is_string($query)) {
54
            throw new \RuntimeException(
55
                sprintf(
56
                    'Could not build search products query because the provided "query" is expected to be a string ' .
57
                    'but "%s" is given.',
58
                    is_object($query) ? get_class($query) : gettype($query)
59
                )
60
            );
61
        }
62
63
        $multiMatch = new MultiMatch();
64
        $multiMatch->setQuery($query);
65
        $multiMatch->setFuzziness('AUTO');
66
        $fields = [];
67
        foreach ($this->searchProperyNameResolverRegistry->getPropertyNameResolvers() as $propertyNameResolver) {
68
            $fields[] = $propertyNameResolver->resolvePropertyName($this->localeContext->getLocaleCode());
69
        }
70
        $multiMatch->setFields($fields);
71
        $bool = new BoolQuery();
72
        $bool->addMust($multiMatch);
73
        $bool->addFilter($this->isEnabledQueryBuilder->buildQuery([]));
0 ignored issues
show
Bug introduced by
It seems like $this->isEnabledQueryBuilder->buildQuery(array()) can also be of type null; however, parameter $filter of Elastica\Query\BoolQuery::addFilter() does only seem to accept Elastica\Query\AbstractQuery, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $bool->addFilter(/** @scrutinizer ignore-type */ $this->isEnabledQueryBuilder->buildQuery([]));
Loading history...
74
        $bool->addFilter($this->hasChannelQueryBuilder->buildQuery([]));
75
        return $bool;
76
    }
77
}
78