Passed
Push — master ( 5197c9...313b01 )
by Sébastien
04:05 queued 15s
created

AbstractPaginator::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Bdf\Prime\Query\Pagination;
4
5
use Bdf\Prime\Collection\CollectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\PrimeSerializable;
8
use Bdf\Prime\Query\QueryInterface;
9
10
/**
11
 * Abstract paginator
12
 * 
13
 * Provide method for pagination
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
14
 * 
15
 * @author  Seb
0 ignored issues
show
Coding Style Documentation introduced by
@author tag is not allowed in class comment
Loading history...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
introduced by
Tag value indented incorrectly; expected 1 space but found 2
Loading history...
16
 * @package Bdf\Prime\Query\Pagination
0 ignored issues
show
Coding Style Documentation introduced by
@package tag is not allowed in class comment
Loading history...
Coding Style introduced by
There must be a single blank line after a tag group
Loading history...
17
 * 
18
 * 
19
 * @todo retourner une nouvelle instance du paginator sur les methodes de collection ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style Documentation introduced by
@todo tag is not allowed in class comment
Loading history...
20
 */
21
abstract class AbstractPaginator extends PrimeSerializable
22
{
23
    const DEFAULT_PAGE  = 1;
24
    const DEFAULT_LIMIT = 20;
25
    
26
    /**
27
     * Current query
28
     * 
29
     * @var QueryInterface
30
     */
31
    protected $query;
32
    
33
    /**
34
     * Current collection
35
     * 
36
     * @var array|CollectionInterface
37
     */
38
    protected $collection = [];
39
    
40
    /**
41
     * Total size of the collection
42
     * 
43
     * @var int 
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
introduced by
Expected "int" but found "int " for @var tag in member variable comment
Loading history...
44
     */
45
    protected $size;
46
    
47
    /**
48
     * Current page
49
     * 
50
     * @var int
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
51
     */
52
    protected $page;
53
    
54
    /**
55
     * Number of entities loaded in the collection
56
     * 
57
     * @var int
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
58
     */
59
    protected $maxRows;
60
    
61
    
62
    /**
63
     * {@inheritdoc}
64
     */
65 7
    public function collection()
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
66
    {
67 7
        return $this->collection;
68
    }
69
    
70
    /**
71
     * Get the query
72
     * 
73
     * @return QueryInterface
74
     */
75 2
    public function query()
76
    {
77 2
        return $this->query;
78
    }
79
    
80
    /**
81
     * Load entities
82
     *
83
     * @throws PrimeException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
84
     */
85 25
    protected function loadCollection()
86
    {
87 25
        if ($this->maxRows > -1) {
88 25
            $this->query->limitPage($this->page, $this->maxRows);
0 ignored issues
show
Bug introduced by
The method limitPage() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

88
            $this->query->/** @scrutinizer ignore-call */ 
89
                          limitPage($this->page, $this->maxRows);
Loading history...
89
        }
90
        
91 25
        $this->collection = $this->query->all();
92 25
    }
93
    
94
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
95
     * {@inheritdoc}
96
     */
97 2
    public function order($attribute = null)
98
    {
99 2
        $orders = $this->query->getOrders();
0 ignored issues
show
Bug introduced by
The method getOrders() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

99
        /** @scrutinizer ignore-call */ 
100
        $orders = $this->query->getOrders();
Loading history...
100
        
101 2
        if ($attribute === null) {
102 2
            return $orders;
103
        }
104
        
105 2
        return isset($orders[$attribute]) ? $orders[$attribute] : null;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function limit()
112
    {
113 1
        return $this->query->getLimit();
0 ignored issues
show
Bug introduced by
The method getLimit() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

113
        return $this->query->/** @scrutinizer ignore-call */ getLimit();
Loading history...
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    public function offset()
120
    {
121 1
        return $this->query->getOffset();
0 ignored issues
show
Bug introduced by
The method getOffset() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

121
        return $this->query->/** @scrutinizer ignore-call */ getOffset();
Loading history...
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 4
    public function page()
128
    {
129 4
        return $this->query->getPage();
0 ignored issues
show
Bug introduced by
The method getPage() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

129
        return $this->query->/** @scrutinizer ignore-call */ getPage();
Loading history...
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 4
    public function pageMaxRows()
136
    {
137 4
        return $this->query->getLimit();
138
    }
139
    
140
    /**
141
     * {@inheritdoc}
142
     */
143 8
    public function size()
144
    {
145 8
        if ($this->size === null) {
146 8
            $this->buildSize();
147
        }
148
        
149 8
        return $this->size;
150
    }
151
    
152
    /**
153
     * Find size of the collection
154
     */
155 8
    protected function buildSize()
156
    {
157 8
        $currentSize = $this->count();
158
            
159 8
        if (!$this->query->isLimitQuery()) {
0 ignored issues
show
Bug introduced by
The method isLimitQuery() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

159
        if (!$this->query->/** @scrutinizer ignore-call */ isLimitQuery()) {
Loading history...
160 3
            $this->size = $currentSize;
161 5
        } elseif ($currentSize + $this->query->getOffset() < $this->query->getLimit()) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
Coding Style introduced by
Operation must be bracketed
Loading history...
162 1
            $this->size = $currentSize + $this->query->getOffset();
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
163
        } else {
164 4
            $this->size = $this->query->paginationCount();
0 ignored issues
show
Bug introduced by
The method paginationCount() does not exist on Bdf\Prime\Query\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\QueryInterface. ( Ignorable by Annotation )

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

164
            /** @scrutinizer ignore-call */ 
165
            $this->size = $this->query->paginationCount();
Loading history...
165
        }
166 8
    }
167
    
168
    /**
169
     * SPL - Countable
170
     *
171
     * {@inheritdoc}
172
     */
173 9
    public function count()
174
    {
175 9
        return count($this->collection);
176
    }
177
    
178
    //--------- collection interface
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// --------- collection interface" but found "//--------- collection interface"
Loading history...
179
    
180
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $items should have a doc-comment as per coding-style.
Loading history...
181
     * {@inheritdoc}
182
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
183
    public function pushAll(array $items)
184
    {
185
        if (!($this->collection instanceof CollectionInterface)) {
186
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
187
        }
188
        
189
        $this->collection->pushAll($items);
190
        
191
        return $this;
192
    }
193
    
194
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $item should have a doc-comment as per coding-style.
Loading history...
195
     * {@inheritdoc}
196
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
197
    public function push($item)
198
    {
199
        if (!($this->collection instanceof CollectionInterface)) {
200
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
201
        }
202
        
203
        $this->collection->push($item);
204
        
205
        return $this;
206
    }
207
    
208
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $item should have a doc-comment as per coding-style.
Loading history...
209
     * {@inheritdoc}
210
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
211
    public function put($key, $item)
212
    {
213
        if (!($this->collection instanceof CollectionInterface)) {
214
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
215
        }
216
        
217
        $this->collection->put($key, $item);
218
        
219
        return $this;
220
    }
221
    
222
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
223
     * SPL - ArrayAccess
224
     *
225
     * {@inheritdoc}
226
     */
227
    public function offsetSet($key, $value)
228
    {
229
        $this->collection[$key] = $value;
230
    }
231
    
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function all()
236
    {
237
        if (!($this->collection instanceof CollectionInterface)) {
238
            return $this->collection;
239
        }
240
        
241
        return $this->collection->all();
242
    }
243
    
244
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $default should have a doc-comment as per coding-style.
Loading history...
245
     * {@inheritdoc}
246
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
247 7
    public function get($key, $default = null)
248
    {
249 7
        if (!($this->collection instanceof CollectionInterface)) {
250
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
251
        }
252
        
253 7
        return $this->collection->get($key, $default);
254
    }
255
    
256
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
257
     * SPL - ArrayAccess
258
     *
259
     * {@inheritdoc}
260
     */
261
    public function offsetGet($key)
262
    {
263
        return $this->collection[$key];
264
    }
265
    
266
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
267
     * {@inheritdoc}
268
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
269
    public function has($key)
270
    {
271
        if (!($this->collection instanceof CollectionInterface)) {
272
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
273
        }
274
        
275
        return $this->collection->has($key);
276
    }
277
    
278
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
279
     * SPL - ArrayAccess
280
     *
281
     * {@inheritdoc}
282
     */
283
    public function offsetExists($key)
284
    {
285
        return isset($this->collection[$key]);
286
    }
287
    
288
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
289
     * {@inheritdoc}
290
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
291
    public function remove($key)
292
    {
293
        if (!($this->collection instanceof CollectionInterface)) {
294
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
295
        }
296
        
297
        $this->collection->remove($key);
298
        
299
        return $this;
300
    }
301
    
302
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
303
     * SPL - ArrayAccess
304
     *
305
     * {@inheritdoc}
306
     */
307
    public function offsetUnset($key)
308
    {
309
        unset($this->collection[$key]);
310
    }
311
    
312
    /**
313
     * {@inheritdoc}
314
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
315 1
    public function clear()
316
    {
317 1
        if (!($this->collection instanceof CollectionInterface)) {
318
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
319
        }
320
        
321 1
        $this->collection->clear();
322
        
323 1
        return $this;
324
    }
325
    
326
    /**
327
     * {@inheritdoc}
328
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
329
    public function keys()
330
    {
331
        if (!($this->collection instanceof CollectionInterface)) {
332
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
333
        }
334
        
335
        return $this->collection->keys();
336
    }
337
    
338
    /**
339
     * {@inheritdoc}
340
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
341 2
    public function isEmpty()
342
    {
343 2
        if (!($this->collection instanceof CollectionInterface)) {
344
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
345
        }
346
        
347 2
        return $this->collection->isEmpty();
348
    }
349
    
350
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
351
     * {@inheritdoc}
352
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
353 1
    public function map($callback)
354
    {
355 1
        if (!($this->collection instanceof CollectionInterface)) {
356
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
357
        }
358
        
359 1
        $this->collection = $this->collection->map($callback);
360
        
361 1
        return $this;
362
    }
363
    
364
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
365
     * {@inheritdoc}
366
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
367 1
    public function filter($callback = null)
368
    {
369 1
        if (!($this->collection instanceof CollectionInterface)) {
370
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
371
        }
372
        
373 1
        $this->collection = $this->collection->filter($callback);
374
        
375 1
        return $this;
376
    }
377
    
378
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $groupBy should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $mode should have a doc-comment as per coding-style.
Loading history...
379
     * {@inheritdoc}
380
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
381 1
    public function groupBy($groupBy, $mode = PaginatorInterface::GROUPBY)
382
    {
383 1
        if (!($this->collection instanceof CollectionInterface)) {
384
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
385
        }
386
        
387 1
        $this->collection = $this->collection->groupBy($groupBy, $mode);
388
        
389 1
        return $this;
390
    }
391
    
392
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
393
     * {@inheritdoc}
394
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
395
    public function contains($element)
396
    {
397
        if (!($this->collection instanceof CollectionInterface)) {
398
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
399
        }
400
        
401
        return $this->collection->contains($element);
402
    }
403
    
404
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $strict should have a doc-comment as per coding-style.
Loading history...
405
     * {@inheritdoc}
406
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
407
    public function indexOf($value, $strict = false)
408
    {
409
        if (!($this->collection instanceof CollectionInterface)) {
410
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
411
        }
412
        
413
        return $this->collection->indexOf($value, $strict);
414
    }
415
    
416
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $items should have a doc-comment as per coding-style.
Loading history...
417
     * {@inheritdoc}
418
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
419
    public function merge($items)
420
    {
421
        if (!($this->collection instanceof CollectionInterface)) {
422
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
423
        }
424
        
425
        $this->collection = $this->collection->merge($items);
426
        
427
        return $this;
428
    }
429
    
430
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
431
     * {@inheritdoc}
432
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
433
    public function sort(callable $callback = null)
434
    {
435
        if (!($this->collection instanceof CollectionInterface)) {
436
            throw new \LogicException('Collection is not an instance of CollectionInterface. Could not call method ' . __METHOD__);
437
        }
438
        
439
        $this->collection = $this->collection->sort($callback);
440
        
441
        return $this;
442
    }
443
}