ShopProductsQueryBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 97
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildQuery() 0 20 1
A __construct() 0 20 1
A resolveOptionQuery() 0 6 4
A resolveAttributeQuery() 0 6 4
A addMustIfNotNull() 0 4 2
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
11
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder;
12
13
use Elastica\Query\AbstractQuery;
14
use Elastica\Query\BoolQuery;
15
16
final class ShopProductsQueryBuilder implements QueryBuilderInterface
17
{
18
    /** @var QueryBuilderInterface */
19
    private $isEnabledQueryBuilder;
20
21
    /** @var QueryBuilderInterface */
22
    private $hasChannelQueryBuilder;
23
24
    /** @var QueryBuilderInterface */
25
    private $containsNameQueryBuilder;
26
27
    /** @var QueryBuilderInterface */
28
    private $hasTaxonQueryBuilder;
29
30
    /** @var QueryBuilderInterface */
31
    private $hasOptionsQueryBuilder;
32
33
    /** @var QueryBuilderInterface */
34
    private $hasAttributesQueryBuilder;
35
36
    /** @var QueryBuilderInterface */
37
    private $hasPriceBetweenQueryBuilder;
38
39
    /** @var string */
40
    private $optionPropertyPrefix;
41
42
    /** @var string */
43
    private $attributePropertyPrefix;
44
45
    public function __construct(
46
        QueryBuilderInterface $isEnabledQueryBuilder,
47
        QueryBuilderInterface $hasChannelQueryBuilder,
48
        QueryBuilderInterface $containsNameQueryBuilder,
49
        QueryBuilderInterface $hasTaxonQueryBuilder,
50
        QueryBuilderInterface $hasOptionsQueryBuilder,
51
        QueryBuilderInterface $hasAttributesQueryBuilder,
52
        QueryBuilderInterface $hasPriceBetweenQueryBuilder,
53
        string $optionPropertyPrefix,
54
        string $attributePropertyPrefix
55
    ) {
56
        $this->isEnabledQueryBuilder = $isEnabledQueryBuilder;
57
        $this->hasChannelQueryBuilder = $hasChannelQueryBuilder;
58
        $this->containsNameQueryBuilder = $containsNameQueryBuilder;
59
        $this->hasTaxonQueryBuilder = $hasTaxonQueryBuilder;
60
        $this->hasOptionsQueryBuilder = $hasOptionsQueryBuilder;
61
        $this->hasAttributesQueryBuilder = $hasAttributesQueryBuilder;
62
        $this->hasPriceBetweenQueryBuilder = $hasPriceBetweenQueryBuilder;
63
        $this->optionPropertyPrefix = $optionPropertyPrefix;
64
        $this->attributePropertyPrefix = $attributePropertyPrefix;
65
    }
66
67
    public function buildQuery(array $data): AbstractQuery
68
    {
69
        $boolQuery = new BoolQuery();
70
71
        $boolQuery->addMust($this->isEnabledQueryBuilder->buildQuery($data));
72
        $boolQuery->addMust($this->hasChannelQueryBuilder->buildQuery($data));
73
74
        $nameQuery = $this->containsNameQueryBuilder->buildQuery($data);
75
        $this->addMustIfNotNull($nameQuery, $boolQuery);
76
77
        $taxonQuery = $this->hasTaxonQueryBuilder->buildQuery($data);
78
        $this->addMustIfNotNull($taxonQuery, $boolQuery);
79
80
        $priceQuery = $this->hasPriceBetweenQueryBuilder->buildQuery($data);
81
        $this->addMustIfNotNull($priceQuery, $boolQuery);
82
83
        $this->resolveOptionQuery($boolQuery, $data);
84
        $this->resolveAttributeQuery($boolQuery, $data);
85
86
        return $boolQuery;
87
    }
88
89
    private function resolveOptionQuery(BoolQuery $boolQuery, array $data): void
90
    {
91
        foreach ($data as $key => $value) {
92
            if (0 === strpos($key, $this->optionPropertyPrefix) && 0 < count($value)) {
93
                $optionQuery = $this->hasOptionsQueryBuilder->buildQuery(['option' => $key, 'option_values' => $value]);
94
                $boolQuery->addMust($optionQuery);
95
            }
96
        }
97
    }
98
99
    private function resolveAttributeQuery(BoolQuery $boolQuery, array $data): void
100
    {
101
        foreach ($data as $key => $value) {
102
            if (0 === strpos($key, $this->attributePropertyPrefix) && 0 < count($value)) {
103
                $optionQuery = $this->hasAttributesQueryBuilder->buildQuery(['attribute' => $key, 'attribute_values' => $value]);
104
                $boolQuery->addMust($optionQuery);
105
            }
106
        }
107
    }
108
109
    private function addMustIfNotNull(?AbstractQuery $query, BoolQuery $boolQuery): void
110
    {
111
        if (null !== $query) {
112
            $boolQuery->addMust($query);
113
        }
114
    }
115
}
116