Passed
Pull Request — master (#159)
by
unknown
08:56
created

ContainsNameQueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
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