Passed
Push — master ( aefee4...1dc983 )
by Rafael
36:25
created

Faceting::build()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

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