Passed
Push — master ( fe103b...7bb24b )
by Timo
23:28
created

Slops::getPhraseSlopFromConfiguration()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
31
/**
32
 * The Slops ParameterProvider is responsible to build the solr query parameters
33
 * that are needed for the several slop arguments.
34
 *
35
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
36
 */
37
class Slops implements ParameterBuilder
38
{
39
    const NO_SLOP = null;
40
41
    /**
42
     * The qs parameter
43
     *
44
     * @var int
45
     */
46
    protected $querySlop = self::NO_SLOP;
47
48
    /**
49
     * @var int
50
     */
51
    protected $phraseSlop = self::NO_SLOP;
52
53
    /**
54
     * @var int
55
     */
56
    protected $bigramPhraseSlop = self::NO_SLOP;
57
58
    /**
59
     * @var int
60
     */
61
    protected $trigramPhraseSlop = self::NO_SLOP;
62
63
    /**
64
     * Slops constructor.
65
     * @param int|null $querySlop
66
     * @param int|null $phraseSlop
67
     * @param int|null $bigramPhraseSlop
68
     * @param int|null $trigramPhraseSlop
69
     */
70 55
    public function __construct($querySlop = self::NO_SLOP, $phraseSlop = self::NO_SLOP, $bigramPhraseSlop = self::NO_SLOP, $trigramPhraseSlop = self::NO_SLOP)
71
    {
72 55
        $this->querySlop = $querySlop;
73 55
        $this->phraseSlop = $phraseSlop;
74 55
        $this->bigramPhraseSlop = $bigramPhraseSlop;
75 55
        $this->trigramPhraseSlop = $trigramPhraseSlop;
76 55
    }
77
78
    /**
79
     * @return boolean
80
     */
81 55
    public function getHasQuerySlop()
82
    {
83 55
        return $this->querySlop !== null;
84
    }
85
86
    /**
87
     * @return int|null
88
     */
89 2
    public function getQuerySlop()
90
    {
91 2
        return $this->querySlop;
92
    }
93
94
    /**
95
     * @param int $querySlop
96
     */
97 1
    public function setQuerySlop(int $querySlop)
98
    {
99 1
        $this->querySlop = $querySlop;
100 1
    }
101
102
    /**
103
     * @return boolean
104
     */
105 55
    public function getHasPhraseSlop()
106
    {
107 55
        return $this->phraseSlop !== null;
108
    }
109
110
111
    /**
112
     * @return int|null
113
     */
114 2
    public function getPhraseSlop()
115
    {
116 2
        return $this->phraseSlop;
117
    }
118
119
    /**
120
     * @param int $phraseSlop
121
     */
122 1
    public function setPhraseSlop(int $phraseSlop)
123
    {
124 1
        $this->phraseSlop = $phraseSlop;
125 1
    }
126
127
    /**
128
     * @return boolean
129
     */
130 55
    public function getHasBigramPhraseSlop()
131
    {
132 55
        return $this->bigramPhraseSlop !== null;
133
    }
134
135
    /**
136
     * @return int|null
137
     */
138 2
    public function getBigramPhraseSlop()
139
    {
140 2
        return $this->bigramPhraseSlop;
141
    }
142
143
    /**
144
     * @param int $bigramPhraseSlop
145
     */
146 1
    public function setBigramPhraseSlop(int $bigramPhraseSlop)
147
    {
148 1
        $this->bigramPhraseSlop = $bigramPhraseSlop;
149 1
    }
150
151
    /**
152
     * @return boolean
153
     */
154 55
    public function getHasTrigramPhraseSlop()
155
    {
156 55
        return $this->trigramPhraseSlop !== null;
157
    }
158
159
    /**
160
     * @return int|null
161
     */
162 2
    public function getTrigramPhraseSlop()
163
    {
164 2
        return $this->trigramPhraseSlop;
165
    }
166
167
    /**
168
     * @param int $trigramPhraseSlop
169
     */
170 1
    public function setTrigramPhraseSlop(int $trigramPhraseSlop)
171
    {
172 1
        $this->trigramPhraseSlop = $trigramPhraseSlop;
173 1
    }
174
175
    /**
176
     * @param TypoScriptConfiguration $solrConfiguration
177
     * @return Slops
178
     */
179 51
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
180
    {
181 51
        $searchConfiguration = $solrConfiguration->getSearchConfiguration();
182 51
        $querySlop = static::getQuerySlopFromConfiguration($searchConfiguration);
183 51
        $phraseSlop = static::getPhraseSlopFromConfiguration($searchConfiguration);
184 51
        $bigramPhraseSlop = static::getBigramPhraseSlopFromConfiguration($searchConfiguration);
185 51
        $trigramPhraseSlop = static::getTrigramPhraseSlopFromConfiguration($searchConfiguration);
186 51
        return new Slops($querySlop, $phraseSlop, $bigramPhraseSlop, $trigramPhraseSlop);
187
    }
188
189
    /**
190
     * @param array $searchConfiguration
191
     * @return int|null
192
     */
193 51
    protected static function getPhraseSlopFromConfiguration($searchConfiguration)
194
    {
195 51
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
196 51
        $phraseSlopConfigured = !empty($searchConfiguration['query.']['phrase.']['slop']);
197 51
        return  ($phraseEnabled && $phraseSlopConfigured) ? $searchConfiguration['query.']['phrase.']['slop'] : self::NO_SLOP;
198
    }
199
200
    /**
201
     * @param array $searchConfiguration
202
     * @return int|null
203
     */
204 51
    protected static function getQuerySlopFromConfiguration($searchConfiguration)
205
    {
206 51
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
207 51
        $querySlopConfigured = !empty($searchConfiguration['query.']['phrase.']['querySlop']);
208 51
        return ($phraseEnabled && $querySlopConfigured) ? $searchConfiguration['query.']['phrase.']['querySlop'] : self::NO_SLOP;
209
    }
210
211
    /**
212
     * @param array $searchConfiguration
213
     * @return int|null
214
     */
215 51
    protected static function getBigramPhraseSlopFromConfiguration($searchConfiguration)
216
    {
217 51
        $bigramPhraseEnabled = !empty($searchConfiguration['query.']['bigramPhrase']) && $searchConfiguration['query.']['bigramPhrase'] === 1;
218 51
        $bigramSlopConfigured = !empty($searchConfiguration['query.']['bigramPhrase.']['slop']);
219 51
        return ($bigramPhraseEnabled && $bigramSlopConfigured) ? $searchConfiguration['query.']['bigramPhrase.']['slop'] : self::NO_SLOP;
220
    }
221
222
    /**
223
     * @param array $searchConfiguration
224
     * @return int|null
225
     */
226 51
    protected static function getTrigramPhraseSlopFromConfiguration($searchConfiguration)
227
    {
228 51
        $trigramPhraseEnabled = !empty($searchConfiguration['query.']['trigramPhrase']) && $searchConfiguration['query.']['trigramPhrase'] === 1;
229 51
        $trigramSlopConfigured = !empty($searchConfiguration['query.']['trigramPhrase.']['slop']);
230 51
        return ($trigramPhraseEnabled && $trigramSlopConfigured) ? $searchConfiguration['query.']['trigramPhrase.']['slop'] : self::NO_SLOP;
231
    }
232
233
    /**
234
     * @param QueryBuilder $parentBuilder
235
     * @return QueryBuilder
236
     */
237 55
    public function build(QueryBuilder $parentBuilder): QueryBuilder
238
    {
239 55
        $query = $parentBuilder->getQuery();
240
241 55
        if ($this->getHasPhraseSlop()) {
242 2
            $query->getEDisMax()->setPhraseSlop($this->getPhraseSlop());
243
        }
244
245 55
        if ($this->getHasBigramPhraseSlop()) {
246 2
            $query->getEDisMax()->setPhraseBigramSlop($this->getBigramPhraseSlop());
247
        }
248
249 55
        if ($this->getHasTrigramPhraseSlop()) {
250 2
            $query->getEDisMax()->setPhraseTrigramSlop($this->getTrigramPhraseSlop());
251
        }
252
253 55
        if ($this->getHasQuerySlop()) {
254 2
            $query->getEDisMax()->setQueryPhraseSlop($this->getQuerySlop());
255
        }
256
257 55
        return $parentBuilder;
258
    }
259
}