Passed
Push — master ( c5feea...1cc810 )
by Mikołaj
11:24
created

ShopProductsQueryBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 134
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B buildQuery() 0 23 4
A __construct() 0 20 1
A resolveOptionQuery() 0 7 4
A resolveAttributeQuery() 0 7 4
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
18
final class ShopProductsQueryBuilder implements QueryBuilderInterface
19
{
20
    /**
21
     * @var QueryBuilderInterface
22
     */
23
    private $isEnabledQueryBuilder;
24
25
    /**
26
     * @var QueryBuilderInterface
27
     */
28
    private $hasChannelQueryBuilder;
29
30
    /**
31
     * @var QueryBuilderInterface
32
     */
33
    private $containsNameQueryBuilder;
34
35
    /**
36
     * @var QueryBuilderInterface
37
     */
38
    private $hasTaxonQueryBuilder;
39
40
    /**
41
     * @var QueryBuilderInterface
42
     */
43
    private $hasOptionsQueryBuilder;
44
45
    /**
46
     * @var QueryBuilderInterface
47
     */
48
    private $hasAttributesQueryBuilder;
49
50
    /**
51
     * @var QueryBuilderInterface
52
     */
53
    private $hasPriceBetweenQueryBuilder;
54
55
    /**
56
     * @var string
57
     */
58
    private $optionPropertyPrefix;
59
60
    /**
61
     * @var string
62
     */
63
    private $attributePropertyPrefix;
64
65
    /**
66
     * @param QueryBuilderInterface $isEnabledQueryBuilder
67
     * @param QueryBuilderInterface $hasChannelQueryBuilder
68
     * @param QueryBuilderInterface $containsNameQueryBuilder
69
     * @param QueryBuilderInterface $hasTaxonQueryBuilder
70
     * @param QueryBuilderInterface $hasOptionsQueryBuilder
71
     * @param QueryBuilderInterface $hasAttributesQueryBuilder
72
     * @param QueryBuilderInterface $hasPriceBetweenQueryBuilder
73
     * @param string $optionPropertyPrefix
74
     * @param string $attributePropertyPrefix
75
     */
76
    public function __construct(
77
        QueryBuilderInterface $isEnabledQueryBuilder,
78
        QueryBuilderInterface $hasChannelQueryBuilder,
79
        QueryBuilderInterface $containsNameQueryBuilder,
80
        QueryBuilderInterface $hasTaxonQueryBuilder,
81
        QueryBuilderInterface $hasOptionsQueryBuilder,
82
        QueryBuilderInterface $hasAttributesQueryBuilder,
83
        QueryBuilderInterface $hasPriceBetweenQueryBuilder,
84
        string $optionPropertyPrefix,
85
        string $attributePropertyPrefix
86
    ) {
87
        $this->isEnabledQueryBuilder = $isEnabledQueryBuilder;
88
        $this->hasChannelQueryBuilder = $hasChannelQueryBuilder;
89
        $this->containsNameQueryBuilder = $containsNameQueryBuilder;
90
        $this->hasTaxonQueryBuilder = $hasTaxonQueryBuilder;
91
        $this->hasOptionsQueryBuilder = $hasOptionsQueryBuilder;
92
        $this->hasAttributesQueryBuilder = $hasAttributesQueryBuilder;
93
        $this->hasPriceBetweenQueryBuilder = $hasPriceBetweenQueryBuilder;
94
        $this->optionPropertyPrefix = $optionPropertyPrefix;
95
        $this->attributePropertyPrefix = $attributePropertyPrefix;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function buildQuery(array $data): AbstractQuery
102
    {
103
        $boolQuery = new BoolQuery();
104
105
        $boolQuery->addMust($this->isEnabledQueryBuilder->buildQuery($data));
106
        $boolQuery->addMust($this->hasChannelQueryBuilder->buildQuery($data));
107
108
        if ($nameQuery = $this->containsNameQueryBuilder->buildQuery($data)) {
109
            $boolQuery->addMust($nameQuery);
110
        }
111
112
        if ($taxonQuery = $this->hasTaxonQueryBuilder->buildQuery($data)) {
113
            $boolQuery->addMust($taxonQuery);
114
        }
115
116
        if ($priceQuery = $this->hasPriceBetweenQueryBuilder->buildQuery($data)) {
117
            $boolQuery->addMust($priceQuery);
118
        }
119
120
        $this->resolveOptionQuery($boolQuery, $data);
121
        $this->resolveAttributeQuery($boolQuery, $data);
122
123
        return $boolQuery;
124
    }
125
126
    /**
127
     * @param BoolQuery $boolQuery
128
     * @param array $data
129
     */
130
    private function resolveOptionQuery(BoolQuery $boolQuery, array $data): void
131
    {
132
        foreach ($data as $key => $value) {
133
            if (0 === strpos($key, $this->optionPropertyPrefix) && 0 < count($value)) {
134
                $optionQuery = $this->hasOptionsQueryBuilder->buildQuery(['option' => $key, 'option_values' => $value]);
135
136
                $boolQuery->addMust($optionQuery);
137
            }
138
        }
139
    }
140
141
    /**
142
     * @param BoolQuery $boolQuery
143
     * @param array $data
144
     */
145
    private function resolveAttributeQuery(BoolQuery $boolQuery, array $data): void
146
    {
147
        foreach ($data as $key => $value) {
148
            if (0 === strpos($key, $this->attributePropertyPrefix) && 0 < count($value)) {
149
                $optionQuery = $this->hasAttributesQueryBuilder->buildQuery(['attribute' => $key, 'attribute_values' => $value]);
150
151
                $boolQuery->addMust($optionQuery);
152
            }
153
        }
154
    }
155
}
156