Passed
Push — upcoming-feature/Introduce_Rou... ( c2e062...738f7d )
by
unknown
35:02
created

Grouping::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 6
crap 1
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\System\Configuration\TypoScriptConfiguration;
29
30
/**
31
 * The Grouping ParameterProvider is responsible to build the solr query parameters
32
 * that are needed for the grouping.
33
 */
34
class Grouping extends AbstractDeactivatable implements ParameterBuilder
35
{
36
37
    /**
38
     * @var array
39
     */
40
    protected $fields = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $sortings = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $queries = [];
51
52
    /**
53
     * @var int
54
     */
55
    protected $numberOfGroups = 5;
56
57
    /**
58
     * @var int
59
     */
60
    protected $resultsPerGroup = 1;
61
62
    /**
63
     * Grouping constructor.
64
     *
65
     * @param bool $isEnabled
66
     * @param array $fields
67
     * @param array $sortings
68
     * @param array $queries
69
     * @param int $numberOfGroups
70
     * @param int $resultsPerGroup
71
     */
72 111
    public function __construct($isEnabled, array $fields = [], array $sortings = [], array $queries = [], $numberOfGroups = 5, $resultsPerGroup = 1)
73
    {
74 111
        $this->isEnabled = $isEnabled;
75 111
        $this->fields = $fields;
76 111
        $this->sortings = $sortings;
77 111
        $this->queries = $queries;
78 111
        $this->numberOfGroups = $numberOfGroups;
79 111
        $this->resultsPerGroup = $resultsPerGroup;
80 111
    }
81
82
    /**
83
     * @return array
84
     */
85 7
    public function getFields()
86
    {
87 7
        return $this->fields;
88
    }
89
90
    /**
91
     * @param array $fields
92
     */
93
    public function setFields(array $fields)
94
    {
95
        $this->fields = $fields;
96
    }
97
98
    /**
99
     * @param string $field
100
     */
101 1
    public function addField(string $field)
102
    {
103 1
        $this->fields[] = $field;
104 1
    }
105
106
    /**
107
     * @return array
108
     */
109 8
    public function getSortings()
110
    {
111 8
        return $this->sortings;
112
    }
113
114
    /**
115
     * @param string $sorting
116
     */
117 4
    public function addSorting($sorting)
118
    {
119 4
        $this->sortings[] = $sorting;
120 4
    }
121
122
    /**
123
     * @param array $sortings
124
     */
125
    public function setSortings(array $sortings)
126
    {
127
        $this->sortings = $sortings;
128
    }
129
130
    /**
131
     * @return array
132
     */
133 7
    public function getQueries(): array
134
    {
135 7
        return $this->queries;
136
    }
137
138
    /**
139
     * @param string $query
140
     */
141 1
    public function addQuery($query)
142
    {
143 1
        $this->queries[] = $query;
144 1
    }
145
146
    /**
147
     * @param array $queries
148
     */
149
    public function setQueries(array $queries)
150
    {
151
        $this->queries = $queries;
152
    }
153
154
    /**
155
     * @return int
156
     */
157 7
    public function getNumberOfGroups()
158
    {
159 7
        return $this->numberOfGroups;
160
    }
161
162
    /**
163
     * @param int $numberOfGroups
164
     */
165
    public function setNumberOfGroups($numberOfGroups)
166
    {
167
        $this->numberOfGroups = $numberOfGroups;
168
    }
169
170
    /**
171
     * @return int
172
     */
173 7
    public function getResultsPerGroup()
174
    {
175 7
        return $this->resultsPerGroup;
176
    }
177
178
    /**
179
     * @param int $resultsPerGroup
180
     */
181 1
    public function setResultsPerGroup($resultsPerGroup)
182
    {
183 1
        $resultsPerGroup = max(intval($resultsPerGroup), 0);
184 1
        $this->resultsPerGroup = $resultsPerGroup;
185 1
    }
186
187
    /**
188
     * @param TypoScriptConfiguration $solrConfiguration
189
     * @return Grouping
190
     */
191 110
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
192
    {
193 110
        $isEnabled = $solrConfiguration->getSearchGrouping();
194 110
        if (!$isEnabled) {
195 109
            return new Grouping(false);
196
        }
197
198 1
        $fields = [];
199 1
        $queries = [];
200 1
        $sortings = [];
201
202 1
        $resultsPerGroup = $solrConfiguration->getSearchGroupingHighestGroupResultsLimit();
203 1
        $configuredGroups = $solrConfiguration->getSearchGroupingGroupsConfiguration();
204 1
        $numberOfGroups = $solrConfiguration->getSearchGroupingNumberOfGroups();
205 1
        $sortBy = $solrConfiguration->getSearchGroupingSortBy();
206
207 1
        foreach ($configuredGroups as $groupName => $groupConfiguration) {
208
            if (isset($groupConfiguration['field'])) {
209
                $fields[] = $groupConfiguration['field'];
210
            } elseif (isset($groupConfiguration['query'])) {
211
                $queries[] = $groupConfiguration['query'];
212
            }
213
        }
214
215 1
        if (!empty(trim($sortBy))) {
216 1
            $sortings[] = $sortBy;
217
        }
218
219 1
        return new Grouping($isEnabled, $fields, $sortings, $queries, $numberOfGroups, $resultsPerGroup);
220
    }
221
222
    /**
223
     * @return Grouping
224
     */
225
    public static function getEmpty()
226
    {
227
        return new Grouping(false);
228
    }
229
230
    /**
231
     * @param AbstractQueryBuilder $parentBuilder
232
     * @return AbstractQueryBuilder
233
     */
234 110
    public function build(AbstractQueryBuilder $parentBuilder): AbstractQueryBuilder
235
    {
236 110
        $query = $parentBuilder->getQuery();
237 110
        if(!$this->getIsEnabled()) {
238 110
            $query->removeComponent($query->getGrouping());
239 110
            return $parentBuilder;
240
        }
241
242 7
        $query->getGrouping()->setFields($this->getFields());
243 7
        $query->getGrouping()->setLimit($this->getResultsPerGroup());
244 7
        $query->getGrouping()->setQueries($this->getQueries());
245 7
        $query->getGrouping()->setFormat('grouped');
246 7
        $query->getGrouping()->setNumberOfGroups(true);
247
248 7
        $query->setRows($this->getNumberOfGroups());
249
250 7
        $sorting = implode(' ', $this->getSortings());
251 7
        $query->getGrouping()->setSort($sorting);
252 7
        return $parentBuilder;
253
    }
254
}
255