Completed
Push — dev-master ( 3d2c20...b7e24f )
by Derek Stephen
08:18 queued 03:13
created

Paginator::render()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 93

Duplication

Lines 21
Ratio 22.58 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
dl 21
loc 93
ccs 0
cts 83
cp 0
rs 0
c 0
b 0
f 0
cc 24
nc 38883
nop 0
crap 600

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    private function url(int $pageNum): string
23
    {
24
        return str_replace($this->urlPart, $pageNum, $this->url);
25
    }
26
27
    /**
28
     * @param $pageCount
29
     */
30
    public function setPageCount(int $pageCount): void
31
    {
32
        $this->pageCount = $pageCount;
33
    }
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
    public function setUrl(string $url): void
47
    {
48
        $this->url = $url;
49
    }
50
51
    /**
52
     * @param $replace
53
     */
54
    public function setUrlPart(string $replace): void
55
    {
56
        $this->urlPart = $replace;
57
    }
58
59
    /**
60
     * @param int $page_no
61
     */
62
    public function setCurrentPage(int $page_no): void
63
    {
64
        $this->currentPage = $page_no;
65
    }
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)
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
    public function setPagerSize(int $numBoxes): void
89
    {
90
        if ($numBoxes % 2 === 0) {
91
            $numBoxes--;
92
        }
93
        $this->pagerSize = $numBoxes;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getPagerSize(): int
100
    {
101
        if (!$this->pagerSize) {
102
            $this->pagerSize = 5;
103
        }
104
        return $this->pagerSize;
105
    }
106
107
    /**
108
     * @return string
109
     * @throws PaginatorException
110
     */
111
    public function render(): string
112
    {
113
        if (!$this->pageCount) {
114
            throw new PaginatorException(PaginatorException::NO_PAGE_COUNT);
115
        }
116
117
        if (!$this->url) {
118
            throw new PaginatorException(PaginatorException::NO_URL);
119
        }
120
121
        if (!$this->currentPage) {
122
            throw new PaginatorException(PaginatorException::NO_CURRENT_PAGE);
123
        }
124
125
        $html = '<nav><ul class="pagination">';
126
127
        if ($this->pageCount > ($this->getPagerSize() - 1)) {
128
            $pages = $this->getPagerSize();
129
            $half = ($pages - 1) / 2;
130
            if ($this->currentPage === 1) {
131
                $start = 1;
132
            } elseif ($this->currentPage === 2) {
133
                $start = 1;
134
            } elseif ($this->currentPage >= ($this->pageCount - $half)) {
135
                $start = $this->pageCount - ($this->getPagerSize() - 1);
136
            } else {
137
                $start = $this->currentPage - $half;
138
                if ($start < 1) {
139
                    $start = 1;
140
                }
141
            }
142
        } else {
143
            $pages = $this->pageCount;
144
            $start = 1;
145
        }
146
147
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
148
        if (isset($this->customPrev)) {
149
            $html .= $this->customPrev;
150
        } elseif ($this->currentPage === 1) {
151
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>';
152
        } else {
153
            $html .= '<a class="page-link"  href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>';
154
        }
155
        $html .= '</li>';
156
157
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
158 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...
159
            $html .= $this->customPrev;
160
        } elseif ($this->currentPage === 1) {
161
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>';
162
        } else {
163
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>';
164
        }
165
        $html .= '</li>';
166
167
        for ($x = $start; $x <= ($start + ($pages - 1)); $x++) {
168
            $html .= '<li class="page-item ';
169
            if ($this->currentPage === $x) {
170
                $html .= ' active" aria-current="page';
171
            }
172
            $html .= '">';
173
            if ($this->currentPage === $x) {
174
                $html .= '<a class="page-link" href="#">' . $x . '</a>';
175
            } else {
176
                $html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>';
177
            }
178
            $html .= '</li>';
179
        }
180
181
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">';
182 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...
183
            $html .= $this->customNext;
184
        } elseif ($this->currentPage >= $this->pageCount) {
185
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>';
186
        } else {
187
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>';
188
        }
189
        $html .= '</li>';
190
191
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
192 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...
193
            $html .= $this->customNext;
194
        } elseif ($this->currentPage >= $this->pageCount) {
195
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>';
196
        } else {
197
            $html .= '<a class="page-link"  href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>';
198
        }
199
        $html .= '</li>';
200
        $html .= '</ul></nav>';
201
202
        return $html;
203
    }
204
}
205