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 ( 4a2061...30729a )
by Asao
01:24
created

Paginate::setToHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 ToHtmlInterface $toHtml
78
     */
79
    public function setToHtml($toHtml)
80
    {
81
        $this->toHtml = $toHtml;
82
    }
83
84
    /**
85
     * @param Inputs $inputs
86
     * @return Paginate
87
     */
88
    public function setInputs(Inputs $inputs)
89
    {
90
        $this->currPage  = $inputs->getPage();
91
        $this->firstPage = $inputs->calcFirstPage();
92
        $this->lastPage  = $inputs->calcLastPage();
93
        $this->pagerKey  = $inputs->pagerKey;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return Page[]|\Iterator
100
     */
101
    public function getIterator()
102
    {
103
        $pages = $this->calcPageList();
104
        foreach($pages as $p) {
105
            yield $p;
106
        }
107
    }
108
109
    /**
110
     * @return Page[]
111
     */
112
    private function calcPageList()
113
    {
114
        $start    = $this->calcStart();
115
        $last     = $this->calcLast();
116
117
        $pages = [];
118
        if ($p = $this->getExtraStart()) {
119
            $pages[] = $p;
120
        }
121
        for ($p = $start; $p <= $last; $p++) {
122
            $pages[$p] = $this->forgePage($p);
123
        }
124
        if ($p = $this->getExtraLast()) {
125
            $pages[] = $p;
126
        }
127
        return $pages;
128
    }
129
130
    /**
131
     * @return int
132
     */
133
    private function calcStart()
134
    {
135
        $half = (int) ($this->numLinks / 2);
136
        $start = max($this->currPage - $half, $this->firstPage + 1);
137
        $maybe = max($this->lastPage - 1 - $this->numLinks, $this->firstPage + 1);
138
        $start = min($start, $maybe);
139
        return $start;
140
    }
141
142
    /**
143
     * @return bool|Page
144
     */    
145
    private function getExtraStart()
146
    {
147
        $start = $this->calcStart();
148
        if ($start < $this->firstPage + 2) {
149
            return false;
150
        }
151
        if ($start === $this->firstPage + 2) {
152
            return $this->forgePage(2);
153
        }
154
        return $this->forgePage(null);
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    private function calcLast()
161
    {
162
        $numLinks = $this->numLinks - 1 + ($this->getExtraStart() ? 0: 1);
163
        $last = min($this->calcStart() + $numLinks, $this->lastPage - 1);
164
        return $last;
165
    }
166
167
    private function getExtraLast()
168
    {
169
        $last = $this->calcLast();
170
        if ($last > $this->lastPage - 2) {
171
            return false;
172
        }
173
        if ($last === $this->lastPage - 2) {
174
            return $this->forgePage($this->lastPage - 1);
175
        }
176
        return $this->forgePage(null);
177
    }
178
    
179
    /**
180
     * @param int $page
181
     * @return Page
182
     */
183
    private function forgePage($page)
184
    {
185
        return new Page($this->pagerKey, $this->currPage, $page);
186
    }
187
188
    /**
189
     * @return Page
190
     */
191
    public function getFirstPage()
192
    {
193
        return $this->forgePage($this->firstPage);
194
    }
195
196
    /**
197
     * @return Page
198
     */
199
    public function getLastPage()
200
    {
201
        return $this->forgePage($this->lastPage);
202
    }
203
204
    /**
205
     * @return Page
206
     */    
207
    public function getNextPage()
208
    {
209
        $page = min($this->currPage + 1, $this->lastPage);
210
        return $this->forgePage($page);
211
    }
212
213
    /**
214
     * @return Page
215
     */
216
    public function getPrevPage()
217
    {
218
        $page = max($this->currPage - 1, $this->firstPage);
219
        return $this->forgePage($page);
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function __toString()
226
    {
227
        $toHtml = $this->toHtml ? $this->toHtml->setPaginate($this): new ToBootstrap3($this);
228
        return $toHtml->__toString();
229
    }
230
}