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

Pager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 31
dl 0
loc 237
rs 9.8
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A isFirstPage() 0 3 1
A isNextPage() 0 3 1
A setCurrentPageNumber() 0 4 1
A asArray() 0 3 1
A setNumFound() 0 4 1
A getNumFound() 0 6 2
A getFirstPage() 0 3 1
A isLastPage() 0 3 1
A isCurrentPage() 0 3 1
A getNextPage() 0 7 2
A getOffset() 0 3 1
A getCurrentPage() 0 3 1
A __construct() 0 5 1
A getCurrentPageNumber() 0 6 2
A getPage() 0 12 3
A getPreviousPage() 0 6 2
A getIterator() 0 4 2
A isPreviousPage() 0 3 1
A count() 0 3 1
A getLastPage() 0 3 1
A getPageOffset() 0 3 1
A getPerPage() 0 6 2
A setPerPage() 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
use BenTools\Pager\Contract\PageUrlBuilderInterface;
8
use BenTools\Pager\Model\Exception\PagerException;
9
10
class Pager implements PagerInterface
11
{
12
    /**
13
     * @var int
14
     */
15
    private $perPage;
16
17
    /**
18
     * @var int
19
     */
20
    private $currentPageNumber;
21
22
    /**
23
     * @var int
24
     */
25
    private $numFound;
26
27
    /**
28
     * Pager constructor.
29
     * @param int|null                     $perPage
30
     * @param int|null                     $currentPageNumber
31
     * @param int|null                     $numFound
32
     * @throws \RuntimeException
33
     */
34
    public function __construct(int $perPage = null, int $currentPageNumber = null, int $numFound = null)
35
    {
36
        $this->perPage = $perPage;
37
        $this->currentPageNumber = $currentPageNumber;
38
        $this->numFound = $numFound;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function setCurrentPageNumber(int $currentPageNumber): PagerInterface
45
    {
46
        $this->currentPageNumber = $currentPageNumber;
47
        return $this;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    private function getCurrentPageNumber(): int
54
    {
55
        if (null === $this->currentPageNumber) {
56
            throw new PagerException(get_class($this) . '::currentPageNumber has not been set.');
57
        }
58
        return $this->currentPageNumber;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getNumFound(): int
65
    {
66
        if (null === $this->numFound) {
67
            throw new PagerException(get_class($this) . '::$numFound has not been set.');
68
        }
69
        return $this->numFound;
70
    }
71
72
    /**
73
     * @param int $numFound
74
     * @return Pager
75
     */
76
    public function setNumFound(int $numFound): PagerInterface
77
    {
78
        $this->numFound = $numFound;
79
        return $this;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function getPerPage(): int
86
    {
87
        if (null === $this->perPage) {
88
            throw new PagerException(get_class($this) . '::$perPage has not been set.');
89
        }
90
        return $this->perPage;
91
    }
92
93
    /**
94
     * @param int $perPage
95
     * @return Pager
96
     */
97
    public function setPerPage(int $perPage): PagerInterface
98
    {
99
        $this->perPage = $perPage;
100
        return $this;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function getOffset(): int
107
    {
108
        return ($this->getCurrentPageNumber() - 1) * $this->getPerPage();
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function count(): int
115
    {
116
        return (int) ceil($this->getNumFound() / $this->getPerPage());
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function getIterator(): iterable
123
    {
124
        for ($nbPages = count($this), $pageNumber = 1; $pageNumber <= $nbPages; $pageNumber++) {
125
            yield $this->getPage($pageNumber);
126
        }
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...
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function getPage(int $pageNumber): PageInterface
133
    {
134
        $nbPages = count($this);
135
136
        if ($pageNumber > $nbPages) {
137
            throw new PagerException(sprintf('Page number %d is invalid, the pager contains only %d pages.', $pageNumber, $nbPages));
138
        } elseif ($pageNumber === $nbPages) {
139
            $nbItems = ($this->getPerPage() - (($pageNumber * $this->getPerPage()) - $this->getNumFound()));
140
        } else {
141
            $nbItems = $this->getPerPage();
142
        }
143
        return new Page($pageNumber, $nbItems);
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function asArray(): array
150
    {
151
        return iterator_to_array($this);
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function getCurrentPage(): PageInterface
158
    {
159
        return $this->getPage($this->getCurrentPageNumber());
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function isCurrentPage(PageInterface $page): bool
166
    {
167
        return $page->getPageNumber() === $this->getCurrentPageNumber();
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function getPreviousPage(): ?PageInterface
174
    {
175
        if (1 === $this->getCurrentPageNumber()) {
176
            return null;
177
        }
178
        return $this->getPage($this->getCurrentPageNumber() - 1);
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184
    public function isPreviousPage(PageInterface $page): bool
185
    {
186
        return (-1 + $this->getCurrentPageNumber()) === $page->getPageNumber();
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192
    public function getNextPage(): ?PageInterface
193
    {
194
        $nbPages = count($this);
195
        if ($nbPages === $this->getCurrentPageNumber()) {
196
            return null;
197
        }
198
        return $this->getPage($this->getCurrentPageNumber() + 1);
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    public function isNextPage(PageInterface $page): bool
205
    {
206
        return (1 + $this->getCurrentPageNumber()) === $page->getPageNumber();
207
    }
208
209
    /**
210
     * @inheritDoc
211
     */
212
    public function getFirstPage(): PageInterface
213
    {
214
        return $this->getPage(1);
215
    }
216
217
    /**
218
     * @inheritDoc
219
     */
220
    public function isFirstPage(PageInterface $page): bool
221
    {
222
        return 1 === $page->getPageNumber();
223
    }
224
225
    /**
226
     * @inheritDoc
227
     */
228
    public function getLastPage(): PageInterface
229
    {
230
        return $this->getPage(count($this));
231
    }
232
233
    /**
234
     * @inheritDoc
235
     */
236
    public function isLastPage(PageInterface $page): bool
237
    {
238
        return count($this) === $page->getPageNumber();
239
    }
240
241
    /**
242
     * @inheritDoc
243
     */
244
    public function getPageOffset(PageInterface $page): int
245
    {
246
        return ($page->getPageNumber() - 1) * $this->getPerPage();
247
    }
248
}
249