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