Completed
Push — dev-master ( 21f466...d07c25 )
by Derek Stephen
02:07
created

Paginator::ensurePageCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Bone\View\Helper;
4
5
use Bone\View\Helper\Exception\PaginatorException;
6
use Del\Icon;
7
8
class Paginator
9
{
10
    private $currentPage = 1;
11
    private $customNext;
12
    private $customPrev;
13
    private $pageCount;
14
    private $pagerSize = 5;
15
    private $url;
16
    private $urlPart = ':page';
17
18
    /**
19
     * @param int $pageNum
20
     * @return string
21
     */
22 1
    private function url(int $pageNum): string
23
    {
24 1
        return str_replace($this->urlPart, $pageNum, $this->url);
25
    }
26
27
    /**
28
     * @param $pageCount
29
     */
30 1
    public function setPageCount(int $pageCount): void
31
    {
32 1
        $this->pageCount = $pageCount;
33 1
    }
34
35
    /**
36
     * @param $pageCount
37
     */
38
    public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void
39
    {
40
        $this->pageCount = (int) ceil($rowCount / $numPerPage) ?: 1;
41
    }
42
43
    /**
44
     * @param $url
45
     */
46 1
    public function setUrl(string $url): void
47
    {
48 1
        $this->url = $url;
49 1
    }
50
51
    /**
52
     * @param $replace
53
     */
54 1
    public function setUrlPart(string $replace): void
55
    {
56 1
        $this->urlPart = $replace;
57 1
    }
58
59
    /**
60
     * @param int $page_no
61
     */
62 1
    public function setCurrentPage(int $page_no): void
63
    {
64 1
        $this->currentPage = $page_no;
65 1
    }
66
67
68
    /**
69
     * @param $url
70
     */
71
    public function setCustomPrev($url): void
72
    {
73
        $this->customPrev = '<a href="' . $url . '/"><i class="icon-backward"></i></a>';
74
    }
75
76
77
    /**
78
     * @param $url
79
     */
80
    public function setCustomNext(string $url): void
81
    {
82
        $this->customNext = '<a href="' . $url . '/"><i class="icon-forward"></i></a>';
83
    }
84
85
    /**
86
     * @param int $numBoxes an ODD number!
87
     */
88 1
    public function setPagerSize(int $numBoxes): void
89
    {
90 1
        if ($numBoxes % 2 === 0) {
91 1
            $numBoxes--;
92
        }
93 1
        $this->pagerSize = $numBoxes;
94 1
    }
95
96
    /**
97
     * @return int
98
     */
99 1
    public function getPagerSize(): int
100
    {
101 1
        if (!$this->pagerSize) {
102
            $this->pagerSize = 5;
103
        }
104 1
        return $this->pagerSize;
105
    }
106
107
    /**
108
     * @throws PaginatorException
109
     */
110 2
    private function ensurePageCount()
111
    {
112 2
        if (!$this->pageCount) {
113 1
            throw new PaginatorException(PaginatorException::NO_PAGE_COUNT);
114
        }
115 1
    }
116
117
    /**
118
     * @throws PaginatorException
119
     */
120 1
    private function ensureUrl()
121
    {
122 1
        if (!$this->url) {
123
            throw new PaginatorException(PaginatorException::NO_URL);
124
        }
125 1
    }
126
127
    /**
128
     * @throws PaginatorException
129
     */
130 1
    private function ensureCurrentPage()
131
    {
132 1
        if (!$this->url) {
133
            throw new PaginatorException(PaginatorException::NO_CURRENT_PAGE);
134
        }
135 1
    }
136
137
    /**
138
     * @return int
139
     */
140 1
    private function calculateStart(int $pages): int
141
    {
142 1
        $half = ($pages - 1) / 2;
143 1
        if ($this->currentPage < 3) {
144 1
            $start = 1;
145
        } elseif ($this->currentPage >= ($this->pageCount - $half)) {
146
            $start = $this->pageCount - ($this->getPagerSize() - 1);
147
        } else {
148
            $start = $this->currentPage - $half;
149
            if ($start < 1) {
150
                $start = 1;
151
            }
152
        }
153
154 1
        return $start;
155
    }
156
157
    /**
158
     * @return string
159
     * @throws PaginatorException
160
     */
161 2
    public function render(): string
162
    {
163 2
        $this->ensurePageCount();
164 1
        $this->ensureUrl();
165 1
        $this->ensureCurrentPage();
166
167 1
        $html = '<nav><ul class="pagination">';
168
169 1
        if ($this->pageCount > ($this->getPagerSize() - 1)) {
170 1
            $pages = $this->getPagerSize();
171 1
            $start = $this->calculateStart($pages);
172
        } else {
173
            $pages = $this->pageCount;
174
            $start = 1;
175
        }
176
177 1
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
178 1
        if (isset($this->customPrev)) {
179
            $html .= $this->customPrev;
180 1
        } elseif ($this->currentPage === 1) {
181
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>';
182
        } else {
183 1
            $html .= '<a class="page-link"  href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>';
184
        }
185 1
        $html .= '</li>';
186
187 1
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
188 1 View Code Duplication
        if (isset($this->customPrev)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            $html .= $this->customPrev;
190 1
        } elseif ($this->currentPage === 1) {
191
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>';
192
        } else {
193 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>';
194
        }
195 1
        $html .= '</li>';
196
197 1
        for ($x = $start; $x <= ($start + ($pages - 1)); $x++) {
198 1
            $html .= '<li class="page-item ';
199 1
            if ($this->currentPage === $x) {
200 1
                $html .= ' active" aria-current="page';
201
            }
202 1
            $html .= '">';
203 1
            if ($this->currentPage === $x) {
204 1
                $html .= '<a class="page-link" href="#">' . $x . '</a>';
205
            } else {
206 1
                $html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>';
207
            }
208 1
            $html .= '</li>';
209
        }
210
211 1
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">';
212 1 View Code Duplication
        if (isset($this->customNext)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
            $html .= $this->customNext;
214 1
        } elseif ($this->currentPage >= $this->pageCount) {
215
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>';
216
        } else {
217 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>';
218
        }
219 1
        $html .= '</li>';
220
221 1
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
222 1 View Code Duplication
        if (isset($this->customNext)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
            $html .= $this->customNext;
224 1
        } elseif ($this->currentPage >= $this->pageCount) {
225
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>';
226
        } else {
227 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>';
228
        }
229 1
        $html .= '</li>';
230 1
        $html .= '</ul></nav>';
231
232 1
        return $html;
233
    }
234
}
235