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

Operator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 82
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setOperator() 0 8 2
A getEmpty() 0 4 1
A getAnd() 0 4 1
A getOr() 0 4 1
A build() 0 10 2
A fromString() 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 Doctrine\Common\Proxy\Exception\InvalidArgumentException;
29
30
/**
31
 * The Operator ParameterProvider is responsible to build the solr query parameters
32
 * that are needed for the operator q.op.
33
 *
34
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
35
 */
36
class Operator extends AbstractDeactivatableParameterBuilder implements ParameterBuilder
37
{
38
    const OPERATOR_AND = 'AND';
39
    const OPERATOR_OR = 'OR';
40
41
    /**
42
     * @var string
43
     */
44
    protected $operator = 'AND';
45
46
    /**
47
     * Faceting constructor.
48
     *
49
     * @param bool $isEnabled
50
     * @param string $operator
51
     */
52
    public function __construct($isEnabled, $operator = Operator::OPERATOR_AND)
53
    {
54
        $this->isEnabled = $isEnabled;
55
        $this->setOperator($operator);
56
    }
57
58
    /**
59
     * @param string $operator
60
     */
61
    public function setOperator($operator)
62
    {
63
        if (!in_array($operator, [self::OPERATOR_AND, self::OPERATOR_OR])) {
64
            throw new InvalidArgumentException("Invalid operator");
65
        }
66
67
        $this->operator = $operator;
68
    }
69
70
    /**
71
     * @return Operator
72
     */
73
    public static function getEmpty(): Operator
74
    {
75
        return new Operator(false);
76
    }
77
78
    /**
79
     * @return Operator
80
     */
81
    public static function getAnd(): Operator
82
    {
83
        return new Operator(true, static::OPERATOR_AND);
84
    }
85
86
    /**
87
     * @return Operator
88
     */
89
    public static function getOr(): Operator
90
    {
91
        return new Operator(true, static::OPERATOR_OR);
92
    }
93
94
    /**
95
     * @param Query $query
96
     * @return Query
97
     */
98
    public function build(Query $query): Query
99
    {
100
        if (!$this->isEnabled) {
101
            $query->getQueryParametersContainer()->remove('q.op');
102
            return $query;
103
        }
104
105
        $query->getQueryParametersContainer()->set('q.op', $this->operator);
106
        return $query;
107
    }
108
109
    /**
110
     * @param string $operator
111
     * @return Operator
112
     */
113
    public static function fromString($operator)
114
    {
115
        return new Operator(true, $operator);
116
    }
117
}