Completed
Push — master ( 6ee2a7...5f60a5 )
by Rafael
04:29
created

Elevation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 106
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIsForced() 0 4 1
A setIsForced() 0 4 1
A getMarkElevatedResults() 0 4 1
A setMarkElevatedResults() 0 4 1
A build() 0 20 4
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) 2018 <[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 Elevation ParameterProvider is responsible to build the solr query parameters
32
 * that are needed for the elevation.
33
 *
34
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
35
 */
36
class Elevation extends AbstractDeactivatableParameterBuilder  implements ParameterBuilder
37
{
38
    /**
39
     * @var bool
40
     */
41
    protected $isForced = true;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $markElevatedResults = true;
47
48
    /**
49
     * Elevation constructor.
50
     * @param boolean $isEnabled
51
     * @param boolean $isForced
52
     * @param boolean $markElevatedResults
53
     */
54
    public function __construct($isEnabled = false, $isForced = true, $markElevatedResults = true)
55
    {
56
        $this->isEnabled = $isEnabled;
57
        $this->isForced = $isForced;
58
        $this->markElevatedResults = $markElevatedResults;
59
    }
60
61
    /**
62
     * @return boolean
63
     */
64
    public function getIsForced(): bool
65
    {
66
        return $this->isForced;
67
    }
68
69
    /**
70
     * @param boolean $isForced
71
     */
72
    public function setIsForced(bool $isForced)
73
    {
74
        $this->isForced = $isForced;
75
    }
76
77
    /**
78
     * @return boolean
79
     */
80
    public function getMarkElevatedResults(): bool
81
    {
82
        return $this->markElevatedResults;
83
    }
84
85
    /**
86
     * @param boolean $markElevatedResults
87
     */
88
    public function setMarkElevatedResults(bool $markElevatedResults)
89
    {
90
        $this->markElevatedResults = $markElevatedResults;
91
    }
92
93
    /**
94
     * @param Query $query
95
     * @return Query
96
     */
97
    public function build(Query $query): Query
98
    {
99
        if (!$this->isEnabled) {
100
            $query->getQueryParametersContainer()->remove('enableElevation');
101
            $query->getQueryParametersContainer()->remove('forceElevation');
102
            $query->getReturnFields()->remove('isElevated:[elevated]');
103
            $query->getReturnFields()->remove('[elevated]'); // fallback
104
105
            return $query;
106
        }
107
108
        $query->getQueryParametersContainer()->set('enableElevation', 'true');
109
        $forceElevationString = $this->isForced ? 'true' : 'false';
110
        $query->getQueryParametersContainer()->set('forceElevation', $forceElevationString);
111
        if ($this->markElevatedResults) {
112
            $query->getReturnFields()->add('isElevated:[elevated]');
113
        }
114
115
        return $query;
116
    }
117
118
    /**
119
     * @param TypoScriptConfiguration $solrConfiguration
120
     * @return Elevation
121
     */
122
    public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration)
123
    {
124
        $isEnabled = $solrConfiguration->getSearchElevation();
125
        if (!$isEnabled) {
126
            return new Elevation(false);
127
        }
128
129
        $force = $solrConfiguration->getSearchElevationForceElevation();
130
        $markResults = $solrConfiguration->getSearchElevationMarkElevatedResults();
131
        return new Elevation(true, $force, $markResults);
132
    }
133
134
    /**
135
     * @return Elevation
136
     */
137
    public static function getEmpty()
138
    {
139
        return new Elevation(false);
140
    }
141
}