Passed
Pull Request — develop (#167)
by Daniel
04:49
created

SearchRequest::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Codappix\SearchCore\Domain\Model;
4
5
/*
6
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
 * 02110-1301, USA.
22
 */
23
24
use Codappix\SearchCore\Connection\ConnectionInterface;
25
use Codappix\SearchCore\Connection\FacetRequestInterface;
26
use Codappix\SearchCore\Connection\SearchRequestInterface;
27
use Codappix\SearchCore\Domain\Search\SearchServiceInterface;
28
use Codappix\SearchCore\Utility\ArrayUtility;
29
30
/**
31
 * Represents a search request used to process an actual search.
32
 */
33
class SearchRequest implements SearchRequestInterface
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;
68
69
    /**
70
     * @var SearchServiceInterface
71
     */
72
    protected $searchService;
73
74
    /**
75
     * @param string $query
76
     */
77 4
    public function __construct(string $query = '')
78
    {
79 4
        $this->query = $query;
80 4
    }
81
82
    public function getQuery(): string
83
    {
84
        return $this->query;
85
    }
86
87 4
    public function getSearchTerm(): string
88
    {
89 4
        return $this->query;
90
    }
91
92
    /**
93
     * Type hint necessary for extbase!
94
     *
95
     * @param array $filter
96
     */
97 2
    public function setFilter(array $filter)
98
    {
99 2
        $filter = ArrayUtility::removeArrayEntryByValue($filter, '');
100 2
        $this->filter = ArrayUtility::removeEmptyElementsRecursively($filter);
101 2
    }
102
103 4
    public function hasFilter(): bool
104
    {
105 4
        return count($this->filter) > 0;
106
    }
107
108 4
    public function getFilter(): array
109
    {
110 4
        return $this->filter;
111
    }
112
113
    /**
114
     * Add a facet to gather in this search request.
115
     */
116 2
    public function addFacet(FacetRequestInterface $facet)
117
    {
118 2
        $this->facets[$facet->getIdentifier()] = $facet;
119 2
    }
120
121
    /**
122
     * Returns all configured facets to fetch in this search request.
123
     */
124 4
    public function getFacets(): array
125
    {
126 4
        return $this->facets;
127
    }
128
129
    /**
130
     * Define connection to use for this request.
131
     * Necessary to allow implementation of execute for interface.
132
     */
133 4
    public function setConnection(ConnectionInterface $connection)
134
    {
135 4
        $this->connection = $connection;
136 4
    }
137
138 4
    public function setSearchService(SearchServiceInterface $searchService)
139
    {
140 4
        $this->searchService = $searchService;
141 4
    }
142
143
    // Extbase QueryInterface
144
    // Current implementation covers only paginate widget support.
145
146
    /**
147
     * @throws \InvalidArgumentException
148
     */
149
    public function execute($returnRawQueryResult = false)
150
    {
151
        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...
152
            throw new \InvalidArgumentException(
153
                'Connection was not set before, therefore execute can not work. Use `setConnection` before.',
154
                1502197732
155
            );
156
        }
157
        if (!($this->searchService instanceof SearchServiceInterface)) {
0 ignored issues
show
introduced by
$this->searchService is always a sub-type of Codappix\SearchCore\Doma...\SearchServiceInterface.
Loading history...
158
            throw new \InvalidArgumentException(
159
                'SearchService was not set before, therefore execute can not work. Use `setSearchService` before.',
160
                1520325175
161
            );
162
        }
163
164
        return $this->searchService->processResult($this->connection->search($this));
165
    }
166
167 4
    public function setLimit($limit)
168
    {
169 4
        $this->limit = (int)$limit;
170
171 4
        return $this;
172
    }
173
174
    public function setOffset($offset)
175
    {
176
        $this->offset = (int)$offset;
177
178
        return $this;
179
    }
180
181 4
    public function getLimit()
182
    {
183 4
        return $this->limit;
184
    }
185
186 4
    public function getOffset()
187
    {
188 4
        return $this->offset;
189
    }
190
191
    /**
192
     * Used, e.g. by caching to determine identifier.
193
     */
194 4
    public function __sleep()
195
    {
196
        return [
197 4
            'query',
198
            'filter',
199
            'facets',
200
            'offset',
201
            'limit',
202
        ];
203
    }
204
205
    /**
206
     * @throws \BadMethodCallException
207
     */
208
    public function getSource()
209
    {
210
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196146);
211
    }
212
213
    /**
214
     * @throws \BadMethodCallException
215
     */
216
    public function setOrderings(array $orderings)
217
    {
218
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196163);
219
    }
220
221
    /**
222
     * @throws \BadMethodCallException
223
     */
224
    public function matching($constraint)
225
    {
226
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196197);
227
    }
228
229
    /**
230
     * @throws \BadMethodCallException
231
     */
232
    public function logicalAnd($constraint1)
233
    {
234
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196166);
235
    }
236
237
    /**
238
     * @throws \BadMethodCallException
239
     */
240
    public function logicalOr($constraint1)
241
    {
242
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196198);
243
    }
244
245
    /**
246
     * @throws \BadMethodCallException
247
     */
248
    public function logicalNot(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
249
    {
250
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196166);
251
    }
252
253
    /**
254
     * @throws \BadMethodCallException
255
     */
256
    public function equals($propertyName, $operand, $caseSensitive = true)
257
    {
258
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196199);
259
    }
260
261
    /**
262
     * @throws \BadMethodCallException
263
     */
264
    public function like($propertyName, $operand, $caseSensitive = true)
265
    {
266
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196167);
267
    }
268
269
    /**
270
     * @throws \BadMethodCallException
271
     */
272
    public function contains($propertyName, $operand)
273
    {
274
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196200);
275
    }
276
277
    /**
278
     * @throws \BadMethodCallException
279
     */
280
    public function in($propertyName, $operand)
281
    {
282
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196167);
283
    }
284
285
    /**
286
     * @throws \BadMethodCallException
287
     */
288
    public function lessThan($propertyName, $operand)
289
    {
290
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196201);
291
    }
292
293
    /**
294
     * @throws \BadMethodCallException
295
     */
296
    public function lessThanOrEqual($propertyName, $operand)
297
    {
298
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
299
    }
300
301
    /**
302
     * @throws \BadMethodCallException
303
     */
304
    public function greaterThan($propertyName, $operand)
305
    {
306
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196202);
307
    }
308
309
    /**
310
     * @throws \BadMethodCallException
311
     */
312
    public function greaterThanOrEqual($propertyName, $operand)
313
    {
314
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
315
    }
316
317
    /**
318
     * @throws \BadMethodCallException
319
     */
320
    public function getType()
321
    {
322
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196203);
323
    }
324
325
    /**
326
     * @throws \BadMethodCallException
327
     */
328
    public function setQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings)
329
    {
330
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196168);
331
    }
332
333
    /**
334
     * @throws \BadMethodCallException
335
     */
336
    public function getQuerySettings()
337
    {
338
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196205);
339
    }
340
341
    /**
342
     * @throws \BadMethodCallException
343
     */
344
    public function count()
345
    {
346
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196169);
347
    }
348
349
    /**
350
     * @throws \BadMethodCallException
351
     */
352
    public function getOrderings()
353
    {
354
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196206);
355
    }
356
357
    /**
358
     * @throws \BadMethodCallException
359
     */
360
    public function getConstraint()
361
    {
362
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196171);
363
    }
364
365
    /**
366
     * @throws \BadMethodCallException
367
     */
368
    public function isEmpty($propertyName)
369
    {
370
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196207);
371
    }
372
373
    /**
374
     * @throws \BadMethodCallException
375
     */
376
    public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source)
377
    {
378
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196172);
379
    }
380
381
    /**
382
     * @throws \BadMethodCallException
383
     */
384
    public function getStatement()
385
    {
386
        throw new \BadMethodCallException('Method is not implemented yet.', 1502196208);
387
    }
388
}
389