Passed
Push — master ( ff806f...2e578a )
by
unknown
12:53 queued 11s
created

ContainsNameQueryBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildQuery() 0 16 2
A __construct() 0 8 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder;
12
13
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
14
use Elastica\Query\AbstractQuery;
15
use Elastica\Query\MatchQuery;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
18
final class ContainsNameQueryBuilder implements QueryBuilderInterface
19
{
20
    /** @var LocaleContextInterface */
21
    private $localeContext;
22
23
    /** @var ConcatedNameResolverInterface */
24
    private $productNameNameResolver;
25
26
    /** @var string */
27
    private $namePropertyPrefix;
28
29
    public function __construct(
30
        LocaleContextInterface $localeContext,
31
        ConcatedNameResolverInterface $productNameNameResolver,
32
        string $namePropertyPrefix
33
    ) {
34
        $this->localeContext = $localeContext;
35
        $this->productNameNameResolver = $productNameNameResolver;
36
        $this->namePropertyPrefix = $namePropertyPrefix;
37
    }
38
39
    public function buildQuery(array $data): ?AbstractQuery
40
    {
41
        $localeCode = $this->localeContext->getLocaleCode();
42
        $propertyName = $this->productNameNameResolver->resolvePropertyName($localeCode);
43
44
        if (!$name = $data[$this->namePropertyPrefix]) {
45
            return null;
46
        }
47
48
        $nameQuery = new MatchQuery();
49
50
        $nameQuery->setFieldQuery($propertyName, $name);
51
        $nameQuery->setFieldFuzziness($propertyName, 2);
52
        $nameQuery->setFieldMinimumShouldMatch($propertyName, 2);
53
54
        return $nameQuery;
55
    }
56
}
57