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.

Paginate   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 196
rs 10
c 0
b 0
f 0

14 Methods

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