Completed
Push — dev-master ( 89c50f...1a6699 )
by Derek Stephen
01:57
created

Paginator::renderBox()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
181
     * @return string
182
     */
183 6
    private function renderForward($icon = Icon::FORWARD, $fastForward = false)
184
    {
185 6
        $urlPageNo = $fastForward ? $this->pageCount : $this->currentPage + 1;
186 6
        $html = ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
187 6 View Code Duplication
        if ($this->currentPage >= $this->pageCount) {
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...
188 2
            $html .= '<a class="page-link" href="#">' . Icon::custom($icon, 'disabled') . '</a>';
189
        } else {
190 4
            $html .= '<a class="page-link"  href ="' . $this->url($urlPageNo) . '">' . $icon . '</i></a>';
191
        }
192 6
        $html .= '</li>';
193
194 6
        return $html;
195
    }
196
197
    /**
198
     * @param int $pageNo
199
     * @return string
200
     */
201 6
    private function renderBox(int $pageNo): string
202
    {
203 6
        $html = '<li class="page-item ';
204 6
        if ($this->currentPage === $pageNo) {
205 6
            $html .= ' active" aria-current="page';
206
        }
207 6
        $html .= '">';
208 6
        if ($this->currentPage === $pageNo) {
209 6
            $html .= '<a class="page-link" href="#">' . $pageNo . '</a>';
210
        } else {
211 5
            $html .= '<a class="page-link" href="' . $this->url($pageNo) . '">' . $pageNo . '</a>';
212
        }
213 6
        $html .= '</li>';
214
215 6
        return $html;
216
    }
217
}
218