Passed
Push — master ( 982e46...a8ac91 )
by Timo
19:05
created

Faceting::getIsEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\System\Configuration\TypoScriptConfiguration;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
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
    private function __construct($isEnabled, $sorting = '', $minCount = 1, $limit = 10, $fields = [], $additionalParameters = [])
81
    {
82
        $this->isEnabled = $isEnabled;
83
        $this->sorting = $sorting;
84
        $this->minCount = $minCount;
85
        $this->limit = $limit;
86
        $this->fields = $fields;
87
        $this->additionalParameters = $additionalParameters;
88
    }
89
90
    /**
91
     * @return boolean
92
     */
93
    public function getIsEnabled()
94
    {
95
        return $this->isEnabled;
96
    }
97
98
    /**
99
     * @param boolean $isEnabled
100
     */
101
    public function setIsEnabled($isEnabled)
102
    {
103
        $this->isEnabled = $isEnabled;
104
    }
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
    public function setFields(array $fields)
166
    {
167
        $this->fields = $fields;
168
    }
169
170
    /**
171
     * @param string $fieldName
172
     */
173
    public function addField($fieldName)
174
    {
175
        $this->fields[] = $fieldName;
176
    }
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
    public function addAdditionalParameter($key, $value)
198
    {
199
        $this->additionalParameters[$key] = $value;
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public function build()
206
    {
207
        if (!$this->isEnabled) {
208
            return [];
209
        }
210
211
        $facetParameters = [];
212
213
        $facetParameters['facet'] = 'true';
214
        $facetParameters['facet.mincount'] = $this->minCount;
215
        $facetParameters['facet.limit'] = $this->limit;
216
        $facetParameters['facet.field'] = $this->fields;
217
218
        foreach ($this->additionalParameters as $additionalParameterKey => $additionalParameterValue) {
219
            $facetParameters[$additionalParameterKey] = $additionalParameterValue;
220
        }
221
222
        $facetParameters = $this->applySorting($facetParameters);
223
224
        return $facetParameters;
225
    }
226
227
    /**
228
     * Reads the facet sorting configuration and applies it to the queryParameters.
229
     *
230
     * @return array
231
     */
232
    protected function applySorting(array $facetParameters)
233
    {
234
        if (!GeneralUtility::inList('count,index,alpha,lex,1,0,true,false', $this->sorting)) {
235
            // when the sorting is not in the list of valid values we do not apply it.
236
            return $facetParameters;
237
        }
238
        $solrSorting = $this->sorting;
239
240
        // alpha and lex alias for index
241
        if ($this->sorting == 'alpha' || $this->sorting == 'lex') {
242
            $solrSorting = 'index';
243
        }
244
245
        $facetParameters['facet.sort'] = $solrSorting;
246
247
        return $facetParameters;
248
    }
249
250
    /**
251
     * @param TypoScriptConfiguration $solrConfiguration
252
     * @return Faceting
253
     */
254
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
255
    {
256
        $isEnabled = $solrConfiguration->getSearchFaceting();
257
        if (!$isEnabled) {
258
            return new Faceting(false);
259
        }
260
261
        $minCount = $solrConfiguration->getSearchFacetingMinimumCount();
262
        $limit = $solrConfiguration->getSearchFacetingFacetLimit();
263
        $sorting = $solrConfiguration->getSearchFacetingSortBy();
264
265
        return new Faceting($isEnabled, $sorting, $minCount, $limit);
266
    }
267
268
}