Failed Conditions
Push — release-11.5.x ( 71e6eb...3bfdb1 )
by Markus
27:37
created

Faceting::getMinCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
19
20
use ApacheSolrForTypo3\Solr\Domain\Search\Query\AbstractQueryBuilder;
21
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
22
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\SortingExpression;
23
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
24
25
/**
26
 * The Faceting ParameterProvider is responsible to build the solr query parameters
27
 * that are needed for the highlighting.
28
 */
29
class Faceting extends AbstractDeactivatable implements ParameterBuilderInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected string $sorting = '';
35
36
    /**
37
     * @var int
38
     */
39
    protected int $minCount = 1;
40
41
    /**
42
     * @var int
43
     */
44
    protected int $limit = 10;
45
46
    /**
47
     * @var string[]
48
     */
49
    protected array $fields = [];
50
51
    /**
52
     * @var array
53
     */
54
    protected array $additionalParameters = [];
55
56
    /**
57
     * Faceting constructor.
58
     *
59
     * @param bool $isEnabled
60
     * @param string $sorting
61
     * @param int $minCount
62
     * @param int $limit
63
     * @param array $fields
64
     * @param array $additionalParameters
65
     */
66 70
    public function __construct(
67
        bool $isEnabled,
68
        string $sorting = '',
69
        int $minCount = 1,
70
        int $limit = 10,
71
        array $fields = [],
72
        array $additionalParameters = []
73
    ) {
74 70
        $this->isEnabled = $isEnabled;
75 70
        $this->sorting = $sorting;
76 70
        $this->minCount = $minCount;
77 70
        $this->limit = $limit;
78 70
        $this->fields = $fields;
79 70
        $this->additionalParameters = $additionalParameters;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getSorting(): string
86
    {
87
        return $this->sorting;
88
    }
89
90
    /**
91
     * @param string $sorting
92
     */
93
    public function setSorting(string $sorting)
94
    {
95
        $this->sorting = $sorting;
96
    }
97
98
    /**
99
     * @return int
100
     */
101 34
    public function getMinCount(): int
102
    {
103 34
        return $this->minCount;
104
    }
105
106
    /**
107
     * @param int $minCount
108
     */
109
    public function setMinCount(int $minCount)
110
    {
111
        $this->minCount = $minCount;
112
    }
113
114
    /**
115
     * @return int
116
     */
117 34
    public function getLimit(): int
118
    {
119 34
        return $this->limit;
120
    }
121
122
    /**
123
     * @param int $limit
124
     */
125
    public function setLimit(int $limit)
126
    {
127
        $this->limit = $limit;
128
    }
129
130
    /**
131
     * @return string[]
132
     */
133 34
    public function getFields(): array
134
    {
135 34
        return $this->fields;
136
    }
137
138
    /**
139
     * @param string[] $fields
140
     */
141 6
    public function setFields(array $fields)
142
    {
143 6
        $this->fields = $fields;
144
    }
145
146
    /**
147
     * @param string $fieldName
148
     */
149
    public function addField(string $fieldName)
150
    {
151
        $this->fields[] = $fieldName;
152
    }
153
154
    /**
155
     * @return array
156
     */
157 34
    public function getAdditionalParameters(): array
158
    {
159 34
        return $this->additionalParameters;
160
    }
161
162
    /**
163
     * @param array $additionalParameters
164
     */
165
    public function setAdditionalParameters(array $additionalParameters)
166
    {
167
        $this->additionalParameters = $additionalParameters;
168
    }
169
170
    /**
171
     * @param string $key
172
     * @param mixed $value
173
     */
174 34
    public function addAdditionalParameter(string $key, $value)
175
    {
176 34
        $this->additionalParameters[$key] = $value;
177
    }
178
179
    /**
180
     * Reads the facet sorting configuration and applies it to the queryParameters.
181
     *
182
     * @param array $facetParameters
183
     * @return array
184
     */
185 34
    protected function applySorting(array $facetParameters): array
186
    {
187 34
        $sortingExpression = new SortingExpression();
188 34
        $globalSortingExpression = $sortingExpression->getForFacet($this->sorting);
189
190 34
        if (!empty($globalSortingExpression)) {
191 34
            $facetParameters['facet.sort'] = $globalSortingExpression;
192
        }
193
194 34
        return $facetParameters;
195
    }
196
197
    /**
198
     * @param TypoScriptConfiguration $solrConfiguration
199
     * @return Faceting
200
     */
201 70
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration): Faceting
202
    {
203 70
        $isEnabled = $solrConfiguration->getSearchFaceting();
204 70
        if (!$isEnabled) {
205 36
            return new Faceting(false);
206
        }
207
208 34
        $minCount = $solrConfiguration->getSearchFacetingMinimumCount();
209 34
        $limit = $solrConfiguration->getSearchFacetingFacetLimit();
210 34
        $sorting = $solrConfiguration->getSearchFacetingSortBy();
211
212 34
        return new Faceting(true, $sorting, $minCount, $limit);
213
    }
214
215
    /**
216
     * @return Faceting
217
     */
218
    public static function getEmpty(): Faceting
219
    {
220
        return new Faceting(false);
221
    }
222
223
    /**
224
     * Retrieves all parameters that are required for faceting.
225
     *
226
     * @return array
227
     */
228 34
    protected function getFacetParameters(): array
229
    {
230 34
        $facetParameters = [];
231 34
        $facetParameters['facet'] = 'true';
232 34
        $facetParameters['facet.mincount'] = $this->getMinCount();
233 34
        $facetParameters['facet.limit'] = $this->getLimit();
234 34
        $facetParameters['facet.field'] = $this->getFields();
235
236 34
        foreach ($this->getAdditionalParameters() as $additionalParameterKey => $additionalParameterValue) {
237 34
            $facetParameters[$additionalParameterKey] = $additionalParameterValue;
238
        }
239
240 34
        if (isset($facetParameters['json.facet']) && $facetParameters['json.facet']) {
241 34
            $facetParameters['json.facet'] = json_encode($facetParameters['json.facet']);
242
        }
243
244 34
        return $this->applySorting($facetParameters);
245
    }
246
247
    /**
248
     * @param AbstractQueryBuilder $parentBuilder
249
     * @return QueryBuilder
250
     */
251 70
    public function build(AbstractQueryBuilder $parentBuilder): AbstractQueryBuilder
252
    {
253 70
        $query = $parentBuilder->getQuery();
254 70
        if (!$this->getIsEnabled()) {
255
            //@todo use unset functionality when present
256 36
            $query->addParam('facet', null);
257 36
            $query->addParam('lex', null);
258 36
            $query->addParam('json.mincount', null);
259 36
            $query->addParam('json.limit', null);
260 36
            $query->addParam('json.field', null);
261 36
            $query->addParam('facet.sort', null);
262
263 36
            $params = $query->getParams();
264 36
            foreach ($params as $key => $value) {
265 36
                if (strpos($key, 'f.') !== false) {
266
                    $query->addParam($key, null);
267
                }
268
            }
269
270 36
            return $parentBuilder;
271
        }
272
273
        //@todo check of $this->queryToBuilder->getFacetSet() can be used
274 34
        $facetingParameters = $this->getFacetParameters();
275 34
        foreach ($facetingParameters as $key => $value) {
276 34
            $query->addParam($key, $value);
277
        }
278
279 34
        return $parentBuilder;
280
    }
281
}
282