Completed
Push — master ( 706b8b...2af00e )
by Derek Stephen
01:55
created

PaginationFilter::getTotalPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Del\Filter\Filter;
4
5
use ArrayIterator;
6
use LogicException;
7
8
class PaginationFilter implements FilterInterface
9
{
10
    /** @var int $page */
11
    private $page;
12
13
    /** @var int $numPerPage */
14
    private $numPerPage;
15
16
    /** @var int $totalRecords */
17
    private $totalRecords;
18
19
    /** @var ArrayIterator $collection */
20
    private $collection;
21
22
    /** @var int $resultsOffset */
23
    private $resultsOffset;
24
25
    private $resultsEndOffset;
26
27
    /**
28
     * @param ArrayIterator $collection
29
     * @return ArrayIterator
30
     */
31 3
    public function filter(ArrayIterator $collection): ArrayIterator
32
    {
33
        // If pagination wasnt set, dont use it!
34 3
        if (!$this->page || !$this->numPerPage) {
35 1
            return $collection;
36
        }
37
38 2
        $this->collection = $collection;
39 2
        $this->totalRecords = $collection->count();
40
41 2
        $this->resultsOffset = ($this->page * $this->numPerPage) - $this->numPerPage;
42 2
        $this->resultsEndOffset = $this->resultsOffset + $this->numPerPage;
43
44 2
        if ($this->resultsOffset > $this->totalRecords) {
45 1
            throw new LogicException('There aren\'t that many pages for this result set.');
46
        }
47
48 2
        $results = $this->getResults();
49
50 2
        return $results;
51
    }
52
53 2
    private function getResults()
54
    {
55 2
        $results = new ArrayIterator();
56
57 2
        $this->collection->rewind();
58
59 2
        for ($x = 0; $x < $this->totalRecords; $x ++) {
60 2
            $this->handleRow($x, $results);
61
        }
62 2
        return $results;
63
    }
64
65 2
    private function handleRow(int $x, ArrayIterator $results)
66
    {
67 2
        if ($this->collection->valid()) {
68 2
            $row = $this->collection->current();
69 2
            if ($x >= $this->resultsOffset && $x < $this->resultsEndOffset) {
70 2
                $results->append($row);
71
            }
72 2
            $this->collection->next();
73
        }
74 2
    }
75
76
77
    /**
78
     * @return int
79
     */
80 2
    public function getPage(): int
81
    {
82 2
        return $this->page;
83
    }
84
85
    /**
86
     * @param int $page
87
     * @return PaginationFilter
88
     */
89 3
    public function setPage(int $page): PaginationFilter
90
    {
91 3
        $this->page = $page;
92 3
        return $this;
93
    }
94
95
    /**
96
     * @return int
97
     */
98 2
    public function getNumPerPage(): int
99
    {
100 2
        return $this->numPerPage;
101
    }
102
103
    /**
104
     * @param int $numPerPage
105
     * @return PaginationFilter
106
     */
107 3
    public function setNumPerPage(int $numPerPage): PaginationFilter
108
    {
109 3
        $this->numPerPage = $numPerPage;
110 3
        return $this;
111
    }
112
113
    /**
114
     * @return int
115
     */
116 2
    public function getTotalPages() : int
117
    {
118 2
        if (!$this->collection instanceof ArrayIterator) {
119 1
            throw new LogicException('You must first pass your collection in to filter');
120
        }
121 1
        return ceil(($this->totalRecords / $this->numPerPage));
122
    }
123
}