GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c21a18...869bac )
by Asao
01:28
created

Paginate::getPrevPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Tuum\Pagination\Paginate;
3
4
use Tuum\Pagination\Inputs;
5
use Tuum\Pagination\ToHtml\ToBootstrap3;
6
use Tuum\Pagination\ToHtml\ToHtmlInterface;
7
8
class Paginate implements \IteratorAggregate, PaginateInterface
9
{
10
    /**
11
     * @var ToHtmlInterface
12
     */
13
    private $toHtml;
14
    
15
    /**
16
     * @var int
17
     */
18
    protected $currPage;
19
20
    /**
21
     * @var int
22
     */
23
    public $num_links = 5;
24
25
    /**
26
     * @var int
27
     */
28
    private $firstPage;
29
30
    /**
31
     * @var int
32
     */
33
    private $lastPage;
34
35
    /**
36
     * @var string
37
     */
38
    private $pagerKey = '_page';
39
40
    /**
41
     * @var int
42
     */
43
    private $numLinks = 5;
44
45
    /**
46
     * Paginate constructor.
47
     *
48
     * @param int    $currPage
49
     * @param int    $lastPage
50
     * @param string $pagerKey
51
     * @param int    $firstPage
52
     */
53
    public function __construct($currPage, $lastPage, $pagerKey = '', $firstPage = 1)
54
    {
55
        $this->currPage  = $currPage;
56
        $this->firstPage = $firstPage;
57
        $this->lastPage  = $lastPage;
58
        $this->pagerKey  = $pagerKey ?: $this->pagerKey;
59
    }
60
61
    /**
62
     * @param Inputs $inputs
63
     * @return Paginate
64
     */
65
    public static function forge(Inputs $inputs)
66
    {
67
        $self = new self(
68
            $inputs->getPage(),
69
            $inputs->calcLastPage(),
70
            $inputs->pagerKey,
71
            $inputs->calcFirstPage()
72
            );
73
        return $self;
74
    }
75
76
    /**
77
     * @param Inputs $inputs
78
     * @return Paginate
79
     */
80
    public function setInputs(Inputs $inputs)
81
    {
82
        $this->currPage  = $inputs->getPage();
83
        $this->firstPage = $inputs->calcFirstPage();
84
        $this->lastPage  = $inputs->calcLastPage();
85
        $this->pagerKey  = $inputs->pagerKey;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return Page[]|\Iterator
92
     */
93
    public function getIterator()
94
    {
95
        $pages = $this->calcPageList();
96
        foreach($pages as $p) {
97
            yield $p;
98
        }
99
    }
100
101
    /**
102
     * @return Page[]
103
     */
104
    private function calcPageList()
105
    {
106
        $start    = $this->calcStart();
107
        $last     = $this->calcLast();
108
109
        $pages = [];
110
        if ($p = $this->getExtraStart()) {
111
            $pages[] = $p;
112
        }
113
        for ($p = $start; $p <= $last; $p++) {
114
            $pages[$p] = $this->forgePage($p);
115
        }
116
        if ($p = $this->getExtraLast()) {
117
            $pages[] = $p;
118
        }
119
        return $pages;
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    private function calcStart()
126
    {
127
        $half = (int) ($this->numLinks / 2);
128
        $start = max($this->currPage - $half, $this->firstPage + 1);
129
        $maybe = max($this->lastPage - 1 - $this->numLinks, $this->firstPage + 1);
130
        $start = min($start, $maybe);
131
        return $start;
132
    }
133
134
    /**
135
     * @return bool|Page
136
     */    
137
    private function getExtraStart()
138
    {
139
        $start = $this->calcStart();
140
        if ($start < $this->firstPage + 2) {
141
            return false;
142
        }
143
        if ($start === $this->firstPage + 2) {
144
            return $this->forgePage(2);
145
        }
146
        return $this->forgePage(null);
147
    }
148
149
    /**
150
     * @return int
151
     */
152
    private function calcLast()
153
    {
154
        $numLinks = $this->numLinks - 1 + ($this->getExtraStart() ? 0: 1);
155
        $last = min($this->calcStart() + $numLinks, $this->lastPage - 1);
156
        return $last;
157
    }
158
159
    private function getExtraLast()
160
    {
161
        $last = $this->calcLast();
162
        if ($last > $this->lastPage - 2) {
163
            return false;
164
        }
165
        if ($last === $this->lastPage - 2) {
166
            return $this->forgePage($this->lastPage - 1);
167
        }
168
        return $this->forgePage(null);
169
    }
170
    
171
    /**
172
     * @param int $page
173
     * @return Page
174
     */
175
    private function forgePage($page)
176
    {
177
        return new Page($this->pagerKey, $this->currPage, $page);
178
    }
179
180
    /**
181
     * @return Page
182
     */
183
    public function getFirstPage()
184
    {
185
        return $this->forgePage($this->firstPage);
186
    }
187
188
    /**
189
     * @return Page
190
     */
191
    public function getLastPage()
192
    {
193
        return $this->forgePage($this->lastPage);
194
    }
195
196
    /**
197
     * @return Page
198
     */    
199
    public function getNextPage()
200
    {
201
        $page = min($this->currPage + 1, $this->lastPage);
202
        return $this->forgePage($page);
203
    }
204
205
    /**
206
     * @return Page
207
     */
208
    public function getPrevPage()
209
    {
210
        $page = max($this->currPage - 1, $this->firstPage);
211
        return $this->forgePage($page);
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function __toString()
218
    {
219
        $toHtml = $this->toHtml ? $this->toHtml->setPaginate($this): new ToBootstrap3($this);
220
        return $toHtml->__toString();
221
    }
222
}