Passed
Pull Request — master (#136)
by Matthieu
31:52 queued 22:36
created

ContainsNameQueryBuilder::buildQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
rs 9.9666
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\MatchQuery;
0 ignored issues
show
Bug introduced by
The type Elastica\Query\MatchQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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 MatchQuery();
51
52
        $nameQuery->setFieldQuery($propertyName, $name);
53
        $nameQuery->setFieldFuzziness($propertyName, 2);
54
        $nameQuery->setFieldMinimumShouldMatch($propertyName, 2);
55
56
        return $nameQuery;
57
    }
58
}
59