Passed
Pull Request — master (#187)
by Przemysław eRIZ
05:39
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
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder;
11
12
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
13
use Elastica\Query\AbstractQuery;
14
use Elastica\Query\MatchQuery;
15
use Sylius\Component\Locale\Context\LocaleContextInterface;
16
17
final class ContainsNameQueryBuilder implements QueryBuilderInterface
18
{
19
    /** @var LocaleContextInterface */
20
    private $localeContext;
21
22
    /** @var ConcatedNameResolverInterface */
23
    private $productNameNameResolver;
24
25
    /** @var string */
26
    private $namePropertyPrefix;
27
28
    public function __construct(
29
        LocaleContextInterface $localeContext,
30
        ConcatedNameResolverInterface $productNameNameResolver,
31
        string $namePropertyPrefix
32
    ) {
33
        $this->localeContext = $localeContext;
34
        $this->productNameNameResolver = $productNameNameResolver;
35
        $this->namePropertyPrefix = $namePropertyPrefix;
36
    }
37
38
    public function buildQuery(array $data): ?AbstractQuery
39
    {
40
        $localeCode = $this->localeContext->getLocaleCode();
41
        $propertyName = $this->productNameNameResolver->resolvePropertyName($localeCode);
42
43
        if (!$name = $data[$this->namePropertyPrefix]) {
44
            return null;
45
        }
46
47
        $nameQuery = new MatchQuery();
48
49
        $nameQuery->setFieldQuery($propertyName, $name);
50
        $nameQuery->setFieldFuzziness($propertyName, 2);
51
        $nameQuery->setFieldMinimumShouldMatch($propertyName, 2);
52
53
        return $nameQuery;
54
    }
55
}
56