Passed
Push — master ( 9edaa9...35a131 )
by Mikołaj
04:21
created

ShopProductsQueryBuilder::resolveAttributeQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 2
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 string
52
     */
53
    private $optionPropertyPrefix;
54
55
    /**
56
     * @var string
57
     */
58
    private $attributePropertyPrefix;
59
60
    /**
61
     * @param QueryBuilderInterface $isEnabledQueryBuilder
62
     * @param QueryBuilderInterface $hasChannelQueryBuilder
63
     * @param QueryBuilderInterface $containsNameQueryBuilder
64
     * @param QueryBuilderInterface $hasTaxonQueryBuilder
65
     * @param QueryBuilderInterface $hasOptionsQueryBuilder
66
     * @param QueryBuilderInterface $hasAttributesQueryBuilder
67
     * @param string $optionPropertyPrefix
68
     * @param string $attributePropertyPrefix
69
     */
70
    public function __construct(
71
        QueryBuilderInterface $isEnabledQueryBuilder,
72
        QueryBuilderInterface $hasChannelQueryBuilder,
73
        QueryBuilderInterface $containsNameQueryBuilder,
74
        QueryBuilderInterface $hasTaxonQueryBuilder,
75
        QueryBuilderInterface $hasOptionsQueryBuilder,
76
        QueryBuilderInterface $hasAttributesQueryBuilder,
77
        string $optionPropertyPrefix,
78
        string $attributePropertyPrefix
79
    )
80
    {
81
        $this->isEnabledQueryBuilder = $isEnabledQueryBuilder;
82
        $this->hasChannelQueryBuilder = $hasChannelQueryBuilder;
83
        $this->containsNameQueryBuilder = $containsNameQueryBuilder;
84
        $this->hasTaxonQueryBuilder = $hasTaxonQueryBuilder;
85
        $this->hasOptionsQueryBuilder = $hasOptionsQueryBuilder;
86
        $this->hasAttributesQueryBuilder = $hasAttributesQueryBuilder;
87
        $this->optionPropertyPrefix = $optionPropertyPrefix;
88
        $this->attributePropertyPrefix = $attributePropertyPrefix;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function buildQuery(array $data): AbstractQuery
95
    {
96
        $boolQuery = new BoolQuery();
97
98
        $boolQuery->addMust($this->isEnabledQueryBuilder->buildQuery($data));
99
        $boolQuery->addMust($this->hasChannelQueryBuilder->buildQuery($data));
100
101
        if ($nameQuery = $this->containsNameQueryBuilder->buildQuery($data)) {
102
            $boolQuery->addMust($nameQuery);
103
        }
104
105
        if ($taxonQuery = $this->hasTaxonQueryBuilder->buildQuery($data)) {
106
            $boolQuery->addMust($taxonQuery);
107
        }
108
109
        $this->resolveOptionQuery($boolQuery, $data);
110
        $this->resolveAttributeQuery($boolQuery, $data);
111
112
        return $boolQuery;
113
    }
114
115
    /**
116
     * @param BoolQuery $boolQuery
117
     * @param array $data
118
     */
119
    private function resolveOptionQuery(BoolQuery $boolQuery, array $data): void
120
    {
121
        foreach ($data as $key => $value) {
122
            if (0 === strpos($key, $this->optionPropertyPrefix) && 0 < count($value)) {
123
                $optionQuery = $this->hasOptionsQueryBuilder->buildQuery(['option' => $key, 'option_values' => $value]);
124
125
                $boolQuery->addMust($optionQuery);
126
            }
127
        }
128
    }
129
130
    /**
131
     * @param BoolQuery $boolQuery
132
     * @param array $data
133
     */
134
    private function resolveAttributeQuery(BoolQuery $boolQuery, array $data): void
135
    {
136
        foreach ($data as $key => $value) {
137
            if (0 === strpos($key, $this->attributePropertyPrefix) && 0 < count($value)) {
138
                $optionQuery = $this->hasAttributesQueryBuilder->buildQuery(['attribute' => $key, 'attribute_values' => $value]);
139
140
                $boolQuery->addMust($optionQuery);
141
            }
142
        }
143
    }
144
}
145