Completed
Pull Request — master (#160)
by
unknown
08:03
created

ContainsNameQueryBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 37
rs 10
wmc 3
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder;
14
15
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
16
use Elastica\Query\AbstractQuery;
17
use Elastica\Query\Match;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_MATCH, expecting T_STRING or '{' on line 17 at column 19
Loading history...
18
use Sylius\Component\Locale\Context\LocaleContextInterface;
19
20
final class ContainsNameQueryBuilder implements QueryBuilderInterface
21
{
22
    /** @var LocaleContextInterface */
23
    private $localeContext;
24
25
    /** @var ConcatedNameResolverInterface */
26
    private $productNameNameResolver;
27
28
    /** @var string */
29
    private $namePropertyPrefix;
30
31
    public function __construct(
32
        LocaleContextInterface $localeContext,
33
        ConcatedNameResolverInterface $productNameNameResolver,
34
        string $namePropertyPrefix
35
    ) {
36
        $this->localeContext = $localeContext;
37
        $this->productNameNameResolver = $productNameNameResolver;
38
        $this->namePropertyPrefix = $namePropertyPrefix;
39
    }
40
41
    public function buildQuery(array $data): ?AbstractQuery
42
    {
43
        $localeCode = $this->localeContext->getLocaleCode();
44
        $propertyName = $this->productNameNameResolver->resolvePropertyName($localeCode);
45
46
        if (!$name = $data[$this->namePropertyPrefix]) {
47
            return null;
48
        }
49
50
        $nameQuery = new Match();
51
52
        $nameQuery->setFieldQuery($propertyName, $name);
53
        $nameQuery->setFieldFuzziness($propertyName, 2);
54
        $nameQuery->setFieldMinimumShouldMatch($propertyName, 2);
55
56
        return $nameQuery;
57
    }
58
}
59