Passed
Push — master ( 16f071...f55a40 )
by Timo
27:02
created

Faceting::getLimit()   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 0
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\Query;
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 AbstractDeactivatableParameterBuilder 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 177
    public function __construct($isEnabled, $sorting = '', $minCount = 1, $limit = 10, $fields = [], $additionalParameters = [])
76
    {
77 177
        $this->isEnabled = $isEnabled;
78 177
        $this->sorting = $sorting;
79 177
        $this->minCount = $minCount;
80 177
        $this->limit = $limit;
81 177
        $this->fields = $fields;
82 177
        $this->additionalParameters = $additionalParameters;
83 177
    }
84
85
    /**
86
     * @return string
87
     */
88 29
    public function getSorting()
89
    {
90 29
        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
    public function getMinCount()
105
    {
106
        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
    public function getLimit()
121
    {
122
        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
    public function getFields()
137
    {
138
        return $this->fields;
139
    }
140
141
    /**
142
     * @param array $fields
143
     */
144 33
    public function setFields(array $fields)
145
    {
146 33
        $this->fields = $fields;
147 33
    }
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
    public function getAdditionalParameters(): array
161
    {
162
        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 41
    public function addAdditionalParameter($key, $value)
177
    {
178 41
        $this->additionalParameters[$key] = $value;
179 41
    }
180
181
    /**
182
     * @param Query $query
183
     * @return Query
184
     */
185 123
    public function build(Query $query): Query
186
    {
187 123
        if (!$this->isEnabled) {
188 77
            $query->getQueryParametersContainer()->removeMany(['facet', 'json.facet']);
189 77
            $query->getQueryParametersContainer()->removeByPrefix('facet.');
190 77
            $query->getQueryParametersContainer()->removeByPrefix('f.');
191 77
            return $query;
192
        }
193
194 48
        $facetParameters = [];
195
196 48
        $facetParameters['facet'] = 'true';
197 48
        $facetParameters['facet.mincount'] = $this->minCount;
198 48
        $facetParameters['facet.limit'] = $this->limit;
199 48
        $facetParameters['facet.field'] = $this->fields;
200
201 48
        foreach ($this->additionalParameters as $additionalParameterKey => $additionalParameterValue) {
202 41
            $facetParameters[$additionalParameterKey] = $additionalParameterValue;
203
        }
204
205 48
        if ($facetParameters['json.facet']) {
206 40
            $facetParameters['json.facet'] = json_encode($facetParameters['json.facet']);
207
        }
208
209 48
        $facetParameters = $this->applySorting($facetParameters);
210 48
        $query->getQueryParametersContainer()->merge($facetParameters);
211
212 48
        return $query;
213
    }
214
215
    /**
216
     * Reads the facet sorting configuration and applies it to the queryParameters.
217
     *
218
     * @param array $facetParameters
219
     * @return array
220
     */
221 48
    protected function applySorting(array $facetParameters)
222
    {
223 48
        $sortingExpression = new SortingExpression();
224 48
        $globalSortingExpression = $sortingExpression->getForFacet($this->sorting);
225
226 48
        if (!empty($globalSortingExpression)) {
227 34
            $facetParameters['facet.sort'] = $globalSortingExpression;
228
        }
229
230 48
        return $facetParameters;
231
    }
232
233
    /**
234
     * @param TypoScriptConfiguration $solrConfiguration
235
     * @return Faceting
236
     */
237 141
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
238
    {
239 141
        $isEnabled = $solrConfiguration->getSearchFaceting();
240 141
        if (!$isEnabled) {
241 98
            return new Faceting(false);
242
        }
243
244 43
        $minCount = $solrConfiguration->getSearchFacetingMinimumCount();
245 43
        $limit = $solrConfiguration->getSearchFacetingFacetLimit();
246 43
        $sorting = $solrConfiguration->getSearchFacetingSortBy();
247
248 43
        return new Faceting($isEnabled, $sorting, $minCount, $limit);
249
    }
250
251
    /**
252
     * @return Faceting
253
     */
254
    public static function getEmpty()
255
    {
256
        return new Faceting(false);
257
    }
258
}
259