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

QueryStringContainer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 133
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getQueryString() 0 8 2
A setQueryString() 0 4 1
A buildQueryString() 0 5 1
A useRawQueryString() 0 4 1
A getKeywords() 0 4 1
A setKeywords() 0 5 1
A getKeywordsCleaned() 0 4 1
A getKeywordsRaw() 0 4 1
A __toString() 0 4 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\Helper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[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
class QueryStringContainer {
28
29
    /**
30
     * @var string
31
     */
32
    protected $keywords;
33
34
    /**
35
     * @var string
36
     */
37
    protected $keywordsRaw;
38
39
    /**
40
     * @var
41
     */
42
    protected $queryString;
43
44
    /**
45
     * @var bool
46
     */
47
    private $rawQueryString = false;
48
49
    /**
50
     * QueryStrings constructor.
51
     * @param string $keywords
52
     */
53
    public function __construct($keywords)
54
    {
55
        $this->setKeywords($keywords);
56
    }
57
58
    /**
59
     * Builds the query string which is then used for Solr's q parameters
60
     *
61
     * @return string Solr query string
62
     */
63
    public function getQueryString()
64
    {
65
        if (!$this->rawQueryString) {
66
            $this->buildQueryString();
67
        }
68
69
        return $this->queryString;
70
    }
71
72
    /**
73
     * Sets the query string without any escaping.
74
     *
75
     * Be cautious with this function!
76
     * TODO remove this method as it basically just sets the q parameter / keywords
77
     *
78
     * @param string $queryString The raw query string.
79
     */
80
    public function setQueryString($queryString)
81
    {
82
        $this->queryString = $queryString;
83
    }
84
85
    /**
86
     * Creates the string that is later used as the q parameter in the solr query
87
     *
88
     * @return void
89
     */
90
    protected function buildQueryString()
91
    {
92
        // very simple for now
93
        $this->queryString = $this->keywords;
94
    }
95
96
    /**
97
     * Sets whether a raw query sting should be used, that is, whether the query
98
     * string should be escaped or not.
99
     *
100
     * @param bool $useRawQueryString TRUE to use raw queries (like Lucene Query Language) or FALSE for regular, escaped queries
101
     */
102
    public function useRawQueryString($useRawQueryString)
103
    {
104
        $this->rawQueryString = (boolean)$useRawQueryString;
105
    }
106
107
    /**
108
     * Get the query keywords, keywords are escaped.
109
     *
110
     * @return string query keywords
111
     */
112
    public function getKeywords()
113
    {
114
        return $this->keywords;
115
    }
116
117
    /**
118
     * Sets the query keywords, escapes them as needed for Solr/Lucene.
119
     *
120
     * @param string $keywords user search terms/keywords
121
     */
122
    public function setKeywords($keywords)
123
    {
124
        $this->keywords = EscapeService::escape($keywords);
0 ignored issues
show
Documentation Bug introduced by
It seems like \ApacheSolrForTypo3\Solr...vice::escape($keywords) can also be of type integer or double. However, the property $keywords is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
125
        $this->keywordsRaw = $keywords;
126
    }
127
128
    /**
129
     * Gets the cleaned keywords so that it can be used in templates f.e.
130
     *
131
     * @return string The cleaned keywords.
132
     */
133
    public function getKeywordsCleaned()
134
    {
135
        return EscapeService::clean($this->keywordsRaw);
136
    }
137
138
    /**
139
     * Gets the raw, unescaped, unencoded keywords.
140
     *
141
     * USE WITH CAUTION!
142
     *
143
     * @return string raw keywords
144
     */
145
    public function getKeywordsRaw()
146
    {
147
        return $this->keywordsRaw;
148
    }
149
150
    /**
151
     * returns a string representation of the query
152
     *
153
     * @return string the string representation of the query
154
     */
155
    public function __toString()
156
    {
157
        return $this->getQueryString();
158
    }
159
}