Passed
Push — master ( 42d2d3...8f9ec7 )
by Timo
69:32 queued 48:37
created

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