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

Slops::fromTypoScriptConfiguration()   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 1
crap 1
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\AbstractQueryBuilder;
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
class Slops implements ParameterBuilder
36
{
37
    const NO_SLOP = null;
38
39
    /**
40
     * The qs parameter
41
     *
42
     * @var int
43
     */
44
    protected $querySlop = self::NO_SLOP;
45
46
    /**
47
     * @var int
48
     */
49
    protected $phraseSlop = self::NO_SLOP;
50
51
    /**
52
     * @var int
53
     */
54
    protected $bigramPhraseSlop = self::NO_SLOP;
55
56
    /**
57
     * @var int
58
     */
59
    protected $trigramPhraseSlop = self::NO_SLOP;
60
61
    /**
62
     * Slops constructor.
63
     * @param int|null $querySlop
64
     * @param int|null $phraseSlop
65
     * @param int|null $bigramPhraseSlop
66
     * @param int|null $trigramPhraseSlop
67
     */
68 22
    public function __construct($querySlop = self::NO_SLOP, $phraseSlop = self::NO_SLOP, $bigramPhraseSlop = self::NO_SLOP, $trigramPhraseSlop = self::NO_SLOP)
69
    {
70 22
        $this->querySlop = $querySlop;
71 22
        $this->phraseSlop = $phraseSlop;
72 22
        $this->bigramPhraseSlop = $bigramPhraseSlop;
73 22
        $this->trigramPhraseSlop = $trigramPhraseSlop;
74 22
    }
75
76
    /**
77
     * @return boolean
78
     */
79 22
    public function getHasQuerySlop()
80
    {
81 22
        return $this->querySlop !== null;
82
    }
83
84
    /**
85
     * @return int|null
86
     */
87 2
    public function getQuerySlop()
88
    {
89 2
        return $this->querySlop;
90
    }
91
92
    /**
93
     * @param int $querySlop
94
     */
95 1
    public function setQuerySlop(int $querySlop)
96
    {
97 1
        $this->querySlop = $querySlop;
98 1
    }
99
100
    /**
101
     * @return boolean
102
     */
103 22
    public function getHasPhraseSlop()
104
    {
105 22
        return $this->phraseSlop !== null;
106
    }
107
108
109
    /**
110
     * @return int|null
111
     */
112 2
    public function getPhraseSlop()
113
    {
114 2
        return $this->phraseSlop;
115
    }
116
117
    /**
118
     * @param int $phraseSlop
119
     */
120 1
    public function setPhraseSlop(int $phraseSlop)
121
    {
122 1
        $this->phraseSlop = $phraseSlop;
123 1
    }
124
125
    /**
126
     * @return boolean
127
     */
128 22
    public function getHasBigramPhraseSlop()
129
    {
130 22
        return $this->bigramPhraseSlop !== null;
131
    }
132
133
    /**
134
     * @return int|null
135
     */
136 2
    public function getBigramPhraseSlop()
137
    {
138 2
        return $this->bigramPhraseSlop;
139
    }
140
141
    /**
142
     * @param int $bigramPhraseSlop
143
     */
144 1
    public function setBigramPhraseSlop(int $bigramPhraseSlop)
145
    {
146 1
        $this->bigramPhraseSlop = $bigramPhraseSlop;
147 1
    }
148
149
    /**
150
     * @return boolean
151
     */
152 22
    public function getHasTrigramPhraseSlop()
153
    {
154 22
        return $this->trigramPhraseSlop !== null;
155
    }
156
157
    /**
158
     * @return int|null
159
     */
160 2
    public function getTrigramPhraseSlop()
161
    {
162 2
        return $this->trigramPhraseSlop;
163
    }
164
165
    /**
166
     * @param int $trigramPhraseSlop
167
     */
168 1
    public function setTrigramPhraseSlop(int $trigramPhraseSlop)
169
    {
170 1
        $this->trigramPhraseSlop = $trigramPhraseSlop;
171 1
    }
172
173
    /**
174
     * @param TypoScriptConfiguration $solrConfiguration
175
     * @return Slops
176
     */
177 18
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
178
    {
179 18
        $searchConfiguration = $solrConfiguration->getSearchConfiguration();
180 18
        $querySlop = static::getQuerySlopFromConfiguration($searchConfiguration);
181 18
        $phraseSlop = static::getPhraseSlopFromConfiguration($searchConfiguration);
182 18
        $bigramPhraseSlop = static::getBigramPhraseSlopFromConfiguration($searchConfiguration);
183 18
        $trigramPhraseSlop = static::getTrigramPhraseSlopFromConfiguration($searchConfiguration);
184 18
        return new Slops($querySlop, $phraseSlop, $bigramPhraseSlop, $trigramPhraseSlop);
185
    }
186
187
    /**
188
     * @param array $searchConfiguration
189
     * @return int|null
190
     */
191 18
    protected static function getPhraseSlopFromConfiguration($searchConfiguration)
192
    {
193 18
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
194 18
        $phraseSlopConfigured = !empty($searchConfiguration['query.']['phrase.']['slop']);
195 18
        return  ($phraseEnabled && $phraseSlopConfigured) ? $searchConfiguration['query.']['phrase.']['slop'] : self::NO_SLOP;
196
    }
197
198
    /**
199
     * @param array $searchConfiguration
200
     * @return int|null
201
     */
202 18
    protected static function getQuerySlopFromConfiguration($searchConfiguration)
203
    {
204 18
        $phraseEnabled = !(empty($searchConfiguration['query.']['phrase']) || $searchConfiguration['query.']['phrase'] !== 1);
205 18
        $querySlopConfigured = !empty($searchConfiguration['query.']['phrase.']['querySlop']);
206 18
        return ($phraseEnabled && $querySlopConfigured) ? $searchConfiguration['query.']['phrase.']['querySlop'] : self::NO_SLOP;
207
    }
208
209
    /**
210
     * @param array $searchConfiguration
211
     * @return int|null
212
     */
213 18
    protected static function getBigramPhraseSlopFromConfiguration($searchConfiguration)
214
    {
215 18
        $bigramPhraseEnabled = !empty($searchConfiguration['query.']['bigramPhrase']) && $searchConfiguration['query.']['bigramPhrase'] === 1;
216 18
        $bigramSlopConfigured = !empty($searchConfiguration['query.']['bigramPhrase.']['slop']);
217 18
        return ($bigramPhraseEnabled && $bigramSlopConfigured) ? $searchConfiguration['query.']['bigramPhrase.']['slop'] : self::NO_SLOP;
218
    }
219
220
    /**
221
     * @param array $searchConfiguration
222
     * @return int|null
223
     */
224 18
    protected static function getTrigramPhraseSlopFromConfiguration($searchConfiguration)
225
    {
226 18
        $trigramPhraseEnabled = !empty($searchConfiguration['query.']['trigramPhrase']) && $searchConfiguration['query.']['trigramPhrase'] === 1;
227 18
        $trigramSlopConfigured = !empty($searchConfiguration['query.']['trigramPhrase.']['slop']);
228 18
        return ($trigramPhraseEnabled && $trigramSlopConfigured) ? $searchConfiguration['query.']['trigramPhrase.']['slop'] : self::NO_SLOP;
229
    }
230
231
    /**
232
     * @param AbstractQueryBuilder $parentBuilder
233
     * @return AbstractQueryBuilder
234
     */
235 22
    public function build(AbstractQueryBuilder $parentBuilder): AbstractQueryBuilder
236
    {
237 22
        $query = $parentBuilder->getQuery();
238
239 22
        if ($this->getHasPhraseSlop()) {
240 2
            $query->getEDisMax()->setPhraseSlop($this->getPhraseSlop());
241
        }
242
243 22
        if ($this->getHasBigramPhraseSlop()) {
244 2
            $query->getEDisMax()->setPhraseBigramSlop($this->getBigramPhraseSlop());
245
        }
246
247 22
        if ($this->getHasTrigramPhraseSlop()) {
248 2
            $query->getEDisMax()->setPhraseTrigramSlop($this->getTrigramPhraseSlop());
249
        }
250
251 22
        if ($this->getHasQuerySlop()) {
252 2
            $query->getEDisMax()->setQueryPhraseSlop($this->getQuerySlop());
253
        }
254
255 22
        return $parentBuilder;
256
    }
257
}
258