Passed
Push — master ( 2cf8ca...97e960 )
by Timo
08:49
created

Spellchecking   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A build() 0 15 2
A fromTypoScriptConfiguration() 0 11 2
A getEmpty() 0 4 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\Query;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
30
/**
31
 * The Spellchecking ParameterProvider is responsible to build the solr query parameters
32
 * that are needed for the spellchecking.
33
 *
34
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
35
 */
36
class Spellchecking extends AbstractDeactivatableParameterBuilder implements ParameterBuilder
37
{
38
39
    /**
40
     * @var int
41
     */
42
    protected $maxCollationTries = 0;
43
44
    /**
45
     * Spellchecking constructor.
46
     *
47
     * @param bool $isEnabled
48
     * @param int $maxCollationTries
49
     */
50
    public function __construct($isEnabled = false, int $maxCollationTries = 0)
51
    {
52
        $this->isEnabled = $isEnabled;
53
        $this->maxCollationTries = $maxCollationTries;
54
    }
55
56
    /**
57
     * @param Query $query
58
     * @return Query
59
     */
60
    public function build(Query $query): Query
61
    {
62
        if (!$this->isEnabled) {
63
            $query->getQueryParametersContainer()->removeMany(['spellcheck', 'spellcheck.collate', 'spellcheck.maxCollationTries']);
64
            return $query;
65
        }
66
67
        $spellcheckingParameters = [];
68
        $spellcheckingParameters['spellcheck'] = 'true';
69
        $spellcheckingParameters['spellcheck.collate'] = 'true';
70
        $spellcheckingParameters['spellcheck.maxCollationTries'] = $this->maxCollationTries;
71
72
        $query->getQueryParametersContainer()->merge($spellcheckingParameters);
73
        return $query;
74
    }
75
76
    /**
77
     * @param TypoScriptConfiguration $solrConfiguration
78
     * @return Spellchecking
79
     */
80
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
81
    {
82
        $isEnabled = $solrConfiguration->getSearchSpellchecking();
83
        if (!$isEnabled) {
84
            return new Spellchecking(false);
85
        }
86
87
        $maxCollationTries = $solrConfiguration->getSearchSpellcheckingNumberOfSuggestionsToTry();
88
89
        return new Spellchecking($isEnabled, $maxCollationTries);
90
    }
91
92
    /**
93
     * @return Spellchecking
94
     */
95
    public static function getEmpty()
96
    {
97
        return new Spellchecking(false);
98
    }
99
}