Failed Conditions
Push — master ( a052b2...ee9c7d )
by Rafael
18:58
created

Faceting::getSorting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 2 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\ResultSet\Facets\SortingExpression;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
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 implements ParameterBuilder
38
{
39
    /**
40
     * @var bool
41
     */
42
    protected $isEnabled = false;
43
44
    /**
45
     * @var string
46
     */
47
    protected $sorting = '';
48
49
    /**
50
     * @var int
51
     */
52
    protected $minCount = 1;
53
54
    /**
55
     * @var
56
     */
57
    protected $limit = 10;
58
59
    /**
60
     * @var array
61
     */
62
    protected $fields = [];
63
64
    /**
65
     * @var array
66
     */
67
    protected $additionalParameters = [];
68
69
    /**
70
     * Faceting constructor.
71
     *
72
     * private constructor should only be created with the from* methods
73
     *
74
     * @param bool $isEnabled
75
     * @param string $sorting
76
     * @param int $minCount
77
     * @param int $limit
78
     * @param array $fields
79
     * @param array $additionalParameters
80
     */
81 120
    private function __construct($isEnabled, $sorting = '', $minCount = 1, $limit = 10, $fields = [], $additionalParameters = [])
82
    {
83 120
        $this->isEnabled = $isEnabled;
84 120
        $this->sorting = $sorting;
85 120
        $this->minCount = $minCount;
86 120
        $this->limit = $limit;
87 120
        $this->fields = $fields;
88 120
        $this->additionalParameters = $additionalParameters;
89 120
    }
90
91
    /**
92
     * @return boolean
93
     */
94
    public function getIsEnabled()
95
    {
96
        return $this->isEnabled;
97
    }
98
99
    /**
100
     * @param boolean $isEnabled
101
     */
102 43
    public function setIsEnabled($isEnabled)
103
    {
104 43
        $this->isEnabled = $isEnabled;
105 43
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getSorting()
111
    {
112
        return $this->sorting;
113
    }
114
115
    /**
116
     * @param string $sorting
117
     */
118
    public function setSorting($sorting)
119
    {
120
        $this->sorting = $sorting;
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    public function getMinCount()
127
    {
128
        return $this->minCount;
129
    }
130
131
    /**
132
     * @param int $minCount
133
     */
134
    public function setMinCount($minCount)
135
    {
136
        $this->minCount = $minCount;
137
    }
138
139
    /**
140
     * @return mixed
141
     */
142
    public function getLimit()
143
    {
144
        return $this->limit;
145
    }
146
147
    /**
148
     * @param mixed $limit
149
     */
150
    public function setLimit($limit)
151
    {
152
        $this->limit = $limit;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getFields()
159
    {
160
        return $this->fields;
161
    }
162
163
    /**
164
     * @param array $fields
165
     */
166 37
    public function setFields(array $fields)
167
    {
168 37
        $this->fields = $fields;
169 37
    }
170
171
    /**
172
     * @param string $fieldName
173
     */
174 2
    public function addField($fieldName)
175
    {
176 2
        $this->fields[] = $fieldName;
177 2
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function getAdditionalParameters(): array
183
    {
184
        return $this->additionalParameters;
185
    }
186
187
    /**
188
     * @param array $additionalParameters
189
     */
190
    public function setAdditionalParameters(array $additionalParameters)
191
    {
192
        $this->additionalParameters = $additionalParameters;
193
    }
194
195
    /**
196
     * @param array $value
197
     */
198 30
    public function addAdditionalParameter($key, $value)
199
    {
200 30
        $this->additionalParameters[$key] = $value;
201 30
    }
202
203
    /**
204
     * @return array
205
     */
206 80
    public function build()
207
    {
208 80
        if (!$this->isEnabled) {
209 37
            return [];
210
        }
211
212 45
        $facetParameters = [];
213
214 45
        $facetParameters['facet'] = 'true';
215 45
        $facetParameters['facet.mincount'] = $this->minCount;
216 45
        $facetParameters['facet.limit'] = $this->limit;
217 45
        $facetParameters['facet.field'] = $this->fields;
218
219 45
        foreach ($this->additionalParameters as $additionalParameterKey => $additionalParameterValue) {
220 30
            $facetParameters[$additionalParameterKey] = $additionalParameterValue;
221
        }
222
223 45
        if ($facetParameters['json.facet']) {
224
            $facetParameters['json.facet'] = json_encode($facetParameters['json.facet']);
225
        }
226
227 45
        $facetParameters = $this->applySorting($facetParameters);
228
229 45
        return $facetParameters;
230
    }
231
232
    /**
233
     * Reads the facet sorting configuration and applies it to the queryParameters.
234
     *
235
     * @return array
236
     */
237 45
    protected function applySorting(array $facetParameters)
238
    {
239 45
        $sortingExpression = new SortingExpression();
240 45
        $globalSortingExpression = $sortingExpression->getForFacet($this->sorting);
241 45
        if (!empty($globalSortingExpression)) {
242 31
            $facetParameters['facet.sort'] = $globalSortingExpression;
243
        }
244
245
246 45
        return $facetParameters;
247
    }
248
249
    /**
250
     * @param TypoScriptConfiguration $solrConfiguration
251
     * @return Faceting
252
     */
253 120
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
254
    {
255 120
        $isEnabled = $solrConfiguration->getSearchFaceting();
256 120
        if (!$isEnabled) {
257 80
            return new Faceting(false);
258
        }
259
260 40
        $minCount = $solrConfiguration->getSearchFacetingMinimumCount();
261 40
        $limit = $solrConfiguration->getSearchFacetingFacetLimit();
262 40
        $sorting = $solrConfiguration->getSearchFacetingSortBy();
263
264 40
        return new Faceting($isEnabled, $sorting, $minCount, $limit);
265
    }
266
267
}
268