Completed
Push — dev-master ( 6b99dc...0f2d28 )
by Derek Stephen
02:19
created

Paginator   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 186
Duplicated Lines 8.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.75%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 2
dl 15
loc 186
ccs 72
cts 83
cp 0.8675
rs 9.92
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 4 1
A setPageCount() 0 4 1
A setPageCountByTotalRecords() 0 4 2
A setUrl() 0 4 1
A setUrlPart() 0 4 1
A setCurrentPage() 0 4 1
A setPagerSize() 0 7 2
A getPagerSize() 0 4 1
A ensurePageCount() 0 6 2
A ensureUrl() 0 7 2
A calculateStart() 0 16 4
F render() 15 64 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 1
    private function url(int $pageNum): string
21
    {
22 1
        return str_replace($this->urlPart, $pageNum, $this->url);
23
    }
24
25
    /**
26
     * @param $pageCount
27
     */
28 1
    public function setPageCount(int $pageCount): void
29
    {
30 1
        $this->pageCount = $pageCount;
31 1
    }
32
33
    /**
34
     * @param $pageCount
35
     */
36 1
    public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void
37
    {
38 1
        $this->pageCount = (int) ceil($rowCount / $numPerPage) ?: 1;
39 1
    }
40
41
    /**
42
     * @param $url
43
     */
44 1
    public function setUrl(string $url): void
45
    {
46 1
        $this->url = $url;
47 1
    }
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 1
    public function setCurrentPage(int $page_no): void
61
    {
62 1
        $this->currentPage = $page_no;
63 1
    }
64
65
    /**
66
     * @param int $numBoxes an ODD number!
67
     */
68 2
    public function setPagerSize(int $numBoxes): void
69
    {
70 2
        if ($numBoxes % 2 === 0) {
71 2
            $numBoxes--;
72
        }
73 2
        $this->pagerSize = $numBoxes;
74 2
    }
75
76
    /**
77
     * @return int
78
     */
79 2
    public function getPagerSize(): int
80
    {
81 2
        return $this->pagerSize;
82
    }
83
84
    /**
85
     * @throws PaginatorException
86
     */
87 3
    private function ensurePageCount()
88
    {
89 3
        if (null === $this->pageCount) {
90 1
            throw new PaginatorException(PaginatorException::NO_PAGE_COUNT);
91
        }
92 2
    }
93
94
    /**
95
     * @throws PaginatorException
96
     */
97 2
    private function ensureUrl()
98
    {
99 2
        codecept_debug($this->url);
100 2
        if (null === $this->url) {
101 1
            throw new PaginatorException(PaginatorException::NO_URL);
102
        }
103 1
    }
104
105
    /**
106
     * @return int
107
     */
108 1
    private function calculateStart(int $pages): int
109
    {
110 1
        $half = ($pages - 1) / 2;
111 1
        if ($this->currentPage < 3) {
112 1
            $start = 1;
113
        } elseif ($this->currentPage >= ($this->pageCount - $half)) {
114
            $start = $this->pageCount - ($this->getPagerSize() - 1);
115
        } else {
116
            $start = $this->currentPage - $half;
117
            if ($start < 1) {
118
                $start = 1;
119
            }
120
        }
121
122 1
        return $start;
123
    }
124
125
    /**
126
     * @return string
127
     * @throws PaginatorException
128
     */
129 3
    public function render(): string
130
    {
131 3
        $this->ensurePageCount();
132 2
        $this->ensureUrl();
133
134 1
        $html = '<nav><ul class="pagination">';
135
136 1
        if ($this->pageCount > ($this->getPagerSize() - 1)) {
137 1
            $pages = $this->getPagerSize();
138 1
            $start = $this->calculateStart($pages);
139
        } else {
140
            $pages = $this->pageCount;
141
            $start = 1;
142
        }
143
144 1
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
145 1
        if ($this->currentPage === 1) {
146
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>';
147
        } else {
148 1
            $html .= '<a class="page-link"  href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>';
149
        }
150 1
        $html .= '</li>';
151
152 1
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
153 1 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...
154
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>';
155
        } else {
156 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>';
157
        }
158 1
        $html .= '</li>';
159
160 1
        for ($x = $start; $x <= ($start + ($pages - 1)); $x++) {
161 1
            $html .= '<li class="page-item ';
162 1
            if ($this->currentPage === $x) {
163 1
                $html .= ' active" aria-current="page';
164
            }
165 1
            $html .= '">';
166 1
            if ($this->currentPage === $x) {
167 1
                $html .= '<a class="page-link" href="#">' . $x . '</a>';
168
            } else {
169 1
                $html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>';
170
            }
171 1
            $html .= '</li>';
172
        }
173
174 1
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">';
175 1 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...
176
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>';
177
        } else {
178 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>';
179
        }
180 1
        $html .= '</li>';
181
182 1
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
183 1 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...
184
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>';
185
        } else {
186 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>';
187
        }
188 1
        $html .= '</li>';
189 1
        $html .= '</ul></nav>';
190
191 1
        return $html;
192
    }
193
}
194