Completed
Push — master ( d24529...76d477 )
by BENOIT
05:43
created

DeltaPager   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 38
dl 0
loc 229
rs 8.3999
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIterator() 0 6 3
A isNextPage() 0 3 1
A setNumFound() 0 4 1
A getNumFound() 0 3 1
A getNextPage() 0 3 1
A asArray() 0 3 1
A getOffset() 0 3 1
A getPage() 0 3 1
A getFirstPage() 0 3 1
B isValid() 0 17 14
A setPerPage() 0 4 1
A isLastPage() 0 3 1
A getLastPage() 0 3 1
A isCurrentPage() 0 3 1
A getPageOffset() 0 3 1
A getCurrentPage() 0 3 1
A count() 0 3 1
A getPreviousPage() 0 3 1
A isFirstPage() 0 3 1
A isPreviousPage() 0 3 1
A getPerPage() 0 3 1
A setCurrentPageNumber() 0 4 1
1
<?php
2
3
namespace BenTools\Pager\Model;
4
5
use BenTools\Pager\Contract\PageInterface;
6
use BenTools\Pager\Contract\PagerInterface;
7
8
class DeltaPager implements PagerInterface
9
{
10
    /**
11
     * @var PagerInterface
12
     */
13
    private $pager;
14
15
    /**
16
     * @var int
17
     */
18
    private $delta;
19
20
    /**
21
     * @var bool
22
     */
23
    private $showFirstPage;
24
25
    /**
26
     * @var bool
27
     */
28
    private $showLastPage;
29
30
    /**
31
     * DeltaPager constructor.
32
     * @param PagerInterface $pager
33
     * @param int            $delta
34
     */
35
    public function __construct(PagerInterface $pager, int $delta = 0, bool $showFirstPage = true, bool $showLastPage = true)
36
    {
37
        $this->pager = $pager;
38
        $this->delta = $delta;
39
        $this->showFirstPage = $showFirstPage;
40
        $this->showLastPage = $showLastPage;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getIterator(): iterable
47
    {
48
        $pager = $this->pager;
49
        foreach ($pager as $page) {
50
            if ($this->isValid($page)) {
51
                yield $page;
52
            }
53
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return iterable. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
54
    }
55
56
    public function asArray(): array
57
    {
58
        return iterator_to_array($this);
59
    }
60
61
    /**
62
     * @param PageInterface $page
63
     * @return bool
64
     */
65
    private function isValid(PageInterface $page): bool
66
    {
67
        $pager = $this->pager;
68
        $currentPageNumber = $pager->getCurrentPage()->getPageNumber();
69
        $leftBorder = $pager->getFirstPage()->getPageNumber() + ($this->delta * 2);
70
        $rightBorder = $pager->getLastPage()->getPageNumber() - ($this->delta * 2);
71
        switch (true) {
72
            case true === $this->showFirstPage && $pager->isFirstPage($page):
73
            case true === $this->showLastPage && $pager->isLastPage($page):
74
            case true === $pager->isCurrentPage($page):
75
            case $page->getPageNumber() < $currentPageNumber && $page->getPageNumber() >= ($currentPageNumber - $this->delta):
76
            case $page->getPageNumber() > $currentPageNumber && $page->getPageNumber() <= ($currentPageNumber + $this->delta):
77
            case $currentPageNumber <= $leftBorder && $page->getPageNumber() <= $leftBorder:
78
            case $currentPageNumber >= $rightBorder && $page->getPageNumber() >= $rightBorder:
79
                return true;
80
        }
81
        return false;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function setPerPage(int $perPage): PagerInterface
88
    {
89
        $this->pager->setPerPage($perPage);
90
        return $this;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function setNumFound(int $numFound): PagerInterface
97
    {
98
        $this->pager->setNumFound($numFound);
99
        return $this;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function setCurrentPageNumber(int $currentPageNumber): PagerInterface
106
    {
107
        $this->pager->setCurrentPageNumber($currentPageNumber);
108
        return $this;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getPerPage(): int
115
    {
116
        return $this->pager->getPerPage();
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function getNumFound(): int
123
    {
124
        return $this->pager->getNumFound();
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getOffset(): int
131
    {
132
        return $this->pager->getOffset();
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function count(): int
139
    {
140
        return $this->pager->count();
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function getCurrentPage(): PageInterface
147
    {
148
        return $this->pager->getCurrentPage();
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function isCurrentPage(PageInterface $page): bool
155
    {
156
        return $this->pager->isCurrentPage($page);
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function getPreviousPage(): ?PageInterface
163
    {
164
        return $this->pager->getPreviousPage();
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170
    public function isPreviousPage(PageInterface $page): bool
171
    {
172
        return $this->pager->isPreviousPage($page);
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    public function getNextPage(): ?PageInterface
179
    {
180
        return $this->pager->getNextPage();
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function isNextPage(PageInterface $page): bool
187
    {
188
        return $this->pager->isNextPage($page);
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function getFirstPage(): PageInterface
195
    {
196
        return $this->pager->getFirstPage();
197
    }
198
199
    /**
200
     * @inheritDoc
201
     */
202
    public function isFirstPage(PageInterface $page): bool
203
    {
204
        return $this->pager->isFirstPage($page);
205
    }
206
207
    /**
208
     * @inheritDoc
209
     */
210
    public function getLastPage(): PageInterface
211
    {
212
        return $this->pager->getLastPage();
213
    }
214
215
    /**
216
     * @inheritDoc
217
     */
218
    public function isLastPage(PageInterface $page): bool
219
    {
220
        return $this->pager->isLastPage($page);
221
    }
222
223
    /**
224
     * @inheritDoc
225
     */
226
    public function getPage(int $pageNumber): PageInterface
227
    {
228
        return $this->pager->getPage($pageNumber);
229
    }
230
231
    /**
232
     * @inheritDoc
233
     */
234
    public function getPageOffset(PageInterface $page): int
235
    {
236
        return $this->pager->getPageOffset($page);
237
    }
238
}
239