Completed
Pull Request — develop (#168)
by Daniel
24:26
created

SearchRequest::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Codappix\SearchCore\Domain\Model;
3
4
/*
5
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
 * 02110-1301, USA.
21
 */
22
23
use Codappix\SearchCore\Connection\ConnectionInterface;
24
use Codappix\SearchCore\Connection\FacetRequestInterface;
25
use Codappix\SearchCore\Connection\SearchRequestInterface;
26
use Codappix\SearchCore\Domain\Search\SearchService;
27
28
/**
29
 * Represents a search request used to process an actual search.
30
 */
31
class SearchRequest implements SearchRequestInterface
32
{
33
    use QueryResultInterfaceStub;
34
35
    /**
36
     * The search string provided by the user, the actual term to search for.
37
     *
38
     * @var string
39
     */
40
    protected $query = '';
41
42
    /**
43
     * @var array
44
     */
45
    protected $filter = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $facets = [];
51
52
    /**
53
     * @var int
54
     */
55
    protected $offset = 0;
56
57
    /**
58
     * @var int
59
     */
60
    protected $limit = 10;
61
62
    /**
63
     * Used for QueryInterface implementation to allow execute method to work.
64
     *
65
     * @var ConnectionInterface
66
     */
67
    protected $connection = null;
68
69
    /**
70
     * @var SearchService
71
     */
72
    protected $searchService = null;
73
74
    /**
75 4
     * @param string $query
76
     */
77 4
    public function __construct(string $query = '')
78 4
    {
79
        $this->query = $query;
80
    }
81
82
    public function getQuery() : string
83
    {
84
        return $this->query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query returns the type string which is incompatible with the return type mandated by TYPO3\CMS\Extbase\Persis...ltInterface::getQuery() of TYPO3\CMS\Extbase\Persistence\QueryInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
85 4
    }
86
87 4
    public function getSearchTerm() : string
88
    {
89
        return $this->query;
90
    }
91
92
    /**
93 2
     * @param array $filter
94
     */
95 2
    public function setFilter(array $filter)
96 2
    {
97 2
        $filter = \TYPO3\CMS\Core\Utility\ArrayUtility::removeArrayEntryByValue($filter, '');
98
        $this->filter = \TYPO3\CMS\Core\Utility\ArrayUtility::filterRecursive($filter, function ($value) {
99 4
            return (!is_array($value) && trim($value) !== '')
100
                || is_array($value) && count($value) !== 0;
101 4
        });
102
    }
103
104 4
    public function hasFilter() : bool
105
    {
106 4
        return count($this->filter) > 0;
107
    }
108
109
    public function getFilter() : array
110
    {
111
        return $this->filter;
112 2
    }
113
114 2
    /**
115 2
     * Add a facet to gather in this search request.
116
     */
117
    public function addFacet(FacetRequestInterface $facet)
118
    {
119
        $this->facets[$facet->getIdentifier()] = $facet;
120 4
    }
121
122 4
    /**
123
     * Returns all configured facets to fetch in this search request.
124
     */
125
    public function getFacets() : array
126
    {
127
        return $this->facets;
128
    }
129 4
130
    /**
131 4
     * Define connection to use for this request.
132 4
     * Necessary to allow implementation of execute for interface.
133
     */
134 4
    public function setConnection(ConnectionInterface $connection)
135
    {
136 4
        $this->connection = $connection;
137 4
    }
138
139
    public function setSearchService(SearchService $searchService)
140
    {
141
        $this->searchService = $searchService;
142
    }
143
144
    // Extbase QueryInterface
145
    // Current implementation covers only paginate widget support.
146
    public function execute($returnRawQueryResult = false)
0 ignored issues
show
Unused Code introduced by
The parameter $returnRawQueryResult is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

146
    public function execute(/** @scrutinizer ignore-unused */ $returnRawQueryResult = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        if (! ($this->connection instanceof ConnectionInterface)) {
0 ignored issues
show
introduced by
$this->connection is always a sub-type of Codappix\SearchCore\Connection\ConnectionInterface.
Loading history...
149
            throw new \InvalidArgumentException(
150
                'Connection was not set before, therefore execute can not work. Use `setConnection` before.',
151
                1502197732
152
            );
153
        }
154
        if (! ($this->searchService instanceof SearchService)) {
0 ignored issues
show
introduced by
$this->searchService is always a sub-type of Codappix\SearchCore\Domain\Search\SearchService.
Loading history...
155
            throw new \InvalidArgumentException(
156
                'SearchService was not set before, therefore execute can not work. Use `setSearchService` before.',
157
                1520325175
158
            );
159 4
        }
160
161 4
        return $this->searchService->processResult($this->connection->search($this));
162
    }
163 4
164
    public function setLimit($limit)
165
    {
166
        $this->limit = (int) $limit;
167
168
        return $this;
169
    }
170
171
    public function setOffset($offset)
172
    {
173 4
        $this->offset = (int) $offset;
174
175 4
        return $this;
176
    }
177
178 4
    public function getLimit(): int
179
    {
180 4
        return $this->limit;
181
    }
182
183
    public function getOffset(): int
184
    {
185
        return $this->offset;
186
    }
187
188
    public function getSource()
189
    {
190
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196146);
191
    }
192
193
    public function setOrderings(array $orderings)
0 ignored issues
show
Unused Code introduced by
The parameter $orderings is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function setOrderings(/** @scrutinizer ignore-unused */ array $orderings)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196163);
196
    }
197
198
    public function matching($constraint)
0 ignored issues
show
Unused Code introduced by
The parameter $constraint is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

198
    public function matching(/** @scrutinizer ignore-unused */ $constraint)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
199
    {
200
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196197);
201
    }
202
203
    public function logicalAnd($constraint1)
0 ignored issues
show
Unused Code introduced by
The parameter $constraint1 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

203
    public function logicalAnd(/** @scrutinizer ignore-unused */ $constraint1)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
204
    {
205
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196166);
206
    }
207
208
    public function logicalOr($constraint1)
0 ignored issues
show
Unused Code introduced by
The parameter $constraint1 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

208
    public function logicalOr(/** @scrutinizer ignore-unused */ $constraint1)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196198);
211
    }
212
213
    public function logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
0 ignored issues
show
Unused Code introduced by
The parameter $constraint is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

213
    public function logicalNot(/** @scrutinizer ignore-unused */ \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196166);
216
    }
217
218
    public function equals($propertyName, $operand, $caseSensitive = true)
0 ignored issues
show
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    public function equals($propertyName, /** @scrutinizer ignore-unused */ $operand, $caseSensitive = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $caseSensitive is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    public function equals($propertyName, $operand, /** @scrutinizer ignore-unused */ $caseSensitive = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    public function equals(/** @scrutinizer ignore-unused */ $propertyName, $operand, $caseSensitive = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
    {
220
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196199);
221
    }
222
223
    public function contains($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

223
    public function contains(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

223
    public function contains($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
    {
225
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196200);
226
    }
227
228
    public function in($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

228
    public function in(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

228
    public function in($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
229
    {
230
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196167);
231
    }
232
233
    public function lessThan($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

233
    public function lessThan($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

233
    public function lessThan(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
    {
235
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196201);
236
    }
237
238
    public function lessThanOrEqual($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

238
    public function lessThanOrEqual($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

238
    public function lessThanOrEqual(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
239
    {
240
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
241
    }
242
243
    public function greaterThan($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

243
    public function greaterThan($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

243
    public function greaterThan(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
244
    {
245
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196202);
246
    }
247
248
    public function greaterThanOrEqual($propertyName, $operand)
0 ignored issues
show
Unused Code introduced by
The parameter $operand is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

248
    public function greaterThanOrEqual($propertyName, /** @scrutinizer ignore-unused */ $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

248
    public function greaterThanOrEqual(/** @scrutinizer ignore-unused */ $propertyName, $operand)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
249
    {
250
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
251
    }
252
253
    public function getType()
254
    {
255
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196203);
256
    }
257
258
    public function setQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings)
0 ignored issues
show
Unused Code introduced by
The parameter $querySettings is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

258
    public function setQuerySettings(/** @scrutinizer ignore-unused */ \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
259
    {
260
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
261
    }
262
263
    public function getQuerySettings()
264
    {
265
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196205);
266
    }
267
268
    public function count()
269
    {
270
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196169);
271
    }
272
273
    public function getOrderings()
274
    {
275
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196206);
276
    }
277
278
    public function getConstraint()
279
    {
280
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196171);
281
    }
282
283
    public function isEmpty($propertyName)
0 ignored issues
show
Unused Code introduced by
The parameter $propertyName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

283
    public function isEmpty(/** @scrutinizer ignore-unused */ $propertyName)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
    {
285
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196207);
286
    }
287
288
    public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

288
    public function setSource(/** @scrutinizer ignore-unused */ \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
289
    {
290
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196172);
291
    }
292
293
    public function getStatement()
294
    {
295
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
296
    }
297
298
    public function current()
299
    {
300
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
301
    }
302
303
    public function next()
304
    {
305
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
306
    }
307
308
    public function key()
309
    {
310
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
311
    }
312
313
    public function valid()
314
    {
315
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
316
    }
317
318
    public function rewind()
319
    {
320
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
321
    }
322
}
323