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

Highlighting::setFragmentSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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\QueryBuilder;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * The Highlighting ParameterProvider is responsible to build the solr query parameters
33
 * that are needed for the highlighting.
34
 *
35
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
36
 */
37
class Highlighting extends AbstractDeactivatable implements ParameterBuilder
38
{
39
    /**
40
     * @var int
41
     */
42
    protected $fragmentSize = 200;
43
44
    /**
45
     * @var string
46
     */
47
    protected $highlightingFieldList = '';
48
49
    /**
50
     * @var string
51
     */
52
    protected $prefix = '';
53
54
    /**
55
     * @var string
56
     */
57
    protected $postfix = '';
58
59
    /**
60
     * Highlighting constructor.
61
     *
62
     * @param bool $isEnabled
63
     * @param int $fragmentSize
64
     * @param string $highlightingFieldList
65
     * @param string $prefix
66
     * @param string $postfix
67
     */
68 136
    public function __construct($isEnabled = false, $fragmentSize = 200, $highlightingFieldList = '', $prefix = '', $postfix = '')
69
    {
70 136
        $this->isEnabled = $isEnabled;
71 136
        $this->fragmentSize = $fragmentSize;
72 136
        $this->highlightingFieldList = $highlightingFieldList;
73 136
        $this->prefix = $prefix;
74 136
        $this->postfix = $postfix;
75 136
    }
76
77
    /**
78
     * @return int
79
     */
80 45
    public function getFragmentSize(): int
81
    {
82 45
        return $this->fragmentSize;
83
    }
84
85
    /**
86
     * @param int $fragmentSize
87
     */
88 3
    public function setFragmentSize(int $fragmentSize)
89
    {
90 3
        $this->fragmentSize = $fragmentSize;
91 3
    }
92
93
    /**
94
     * @return string
95
     */
96 45
    public function getHighlightingFieldList(): string
97
    {
98 45
        return $this->highlightingFieldList;
99
    }
100
101
    /**
102
     * @param string $highlightingFieldList
103
     */
104
    public function setHighlightingFieldList(string $highlightingFieldList)
105
    {
106
        $this->highlightingFieldList = $highlightingFieldList;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 45
    public function getPrefix(): string
113
    {
114 45
        return $this->prefix;
115
    }
116
117
    /**
118
     * @param string $prefix
119
     */
120
    public function setPrefix(string $prefix)
121
    {
122
        $this->prefix = $prefix;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 44
    public function getPostfix(): string
129
    {
130 44
        return $this->postfix;
131
    }
132
133
    /**
134
     * @param string $postfix
135
     */
136
    public function setPostfix(string $postfix)
137
    {
138
        $this->postfix = $postfix;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144 45
    public function getUseFastVectorHighlighter()
145
    {
146 45
        return ($this->fragmentSize >= 18);
147
    }
148
149
150
    /**
151
     * @param TypoScriptConfiguration $solrConfiguration
152
     * @return Highlighting
153
     */
154 136
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
155
    {
156 136
        $isEnabled = $solrConfiguration->getSearchResultsHighlighting();
157 136
        if (!$isEnabled) {
158 95
            return new Highlighting(false);
159
        }
160
161 41
        $fragmentSize = $solrConfiguration->getSearchResultsHighlightingFragmentSize();
162 41
        $highlightingFields = $solrConfiguration->getSearchResultsHighlightingFields();
163 41
        $wrap = explode('|', $solrConfiguration->getSearchResultsHighlightingWrap());
164 41
        $prefix = isset($wrap[0]) ? $wrap[0] : '';
165 41
        $postfix = isset($wrap[1]) ? $wrap[1] : '';
166
167
168 41
        return new Highlighting($isEnabled, $fragmentSize, $highlightingFields, $prefix, $postfix);
169
    }
170
171
    /**
172
     * @return Highlighting
173
     */
174
    public static function getEmpty()
175
    {
176
        return new Highlighting(false);
177
    }
178
179
180
    /**
181
     * @param QueryBuilder $parentBuilder
182
     * @return QueryBuilder
183
     */
184 136
    public function build(QueryBuilder $parentBuilder): QueryBuilder
185
    {
186 136
        $query = $parentBuilder->getQuery();
187 136
        if(!$this->getIsEnabled()) {
188 95
            $query->removeComponent($query->getHighlighting());
189 95
            return $parentBuilder;
190
        }
191
192 45
        $query->getHighlighting()->setFragSize($this->getFragmentSize());
193 45
        $query->getHighlighting()->setFields(GeneralUtility::trimExplode(",", $this->getHighlightingFieldList()));
194
195 45
        if ($this->getUseFastVectorHighlighter()) {
196 44
            $query->getHighlighting()->setUseFastVectorHighlighter(true);
197 44
            $query->getHighlighting()->setTagPrefix($this->getPrefix());
198 44
            $query->getHighlighting()->setTagPostfix($this->getPostfix());
199
        } else {
200 2
            $query->getHighlighting()->setUseFastVectorHighlighter(false);
201 2
            $query->getHighlighting()->setTagPrefix(null);
202 2
            $query->getHighlighting()->setTagPostfix(null);
203
        }
204
205 45
        if ($this->getPrefix() !== '' && $this->getPostfix() !== '') {
206 40
            $query->getHighlighting()->setSimplePrefix($this->getPrefix());
207 40
            $query->getHighlighting()->setSimplePostfix($this->getPostfix());
208
        }
209
210 45
        return $parentBuilder;
211
212
    }
213
}