Passed
Push — master ( b1f912...39c5fb )
by Damian
08:50
created

HasAttributesQueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 Elastica\Query\AbstractQuery;
16
use Elastica\Query\BoolQuery;
17
use Elastica\Query\Term;
18
use Sylius\Component\Locale\Context\LocaleContextInterface;
19
20
final class HasAttributesQueryBuilder implements QueryBuilderInterface
21
{
22
    protected $localeContext;
23
24
    public function __construct(LocaleContextInterface $localeContext)
25
    {
26
        $this->localeContext = $localeContext;
27
    }
28
29
    public function buildQuery(array $data): ?AbstractQuery
30
    {
31
        $attributeQuery = new BoolQuery();
32
33
        foreach ((array) $data['attribute_values'] as $attributeValue) {
34
            $termQuery = new Term();
35
            $attribute = \sprintf('%s_%s', $data['attribute'], $this->localeContext->getLocaleCode());
36
            $termQuery->setTerm(
37
                strtolower($attribute),
38
                $attributeValue
39
            );
40
41
            $attributeQuery->addShould($termQuery);
42
        }
43
44
        return $attributeQuery;
45
    }
46
}
47