Passed
Push — master ( fe103b...7bb24b )
by Timo
23:28
created

Faceting::setMinCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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