Completed
Push — dev-master ( 7df918...6b99dc )
by Derek Stephen
02:11
created

Paginator::setPageCountByTotalRecords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 $pageCount;
12
    private $pagerSize = 5;
13
    private $url;
14
    private $urlPart = ':page';
15
16
    /**
17
     * @param int $pageNum
18
     * @return string
19
     */
20 2
    private function url(int $pageNum): string
21
    {
22 2
        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 2
    public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void
37
    {
38 2
        $this->pageCount = (int) ceil($rowCount / $numPerPage) ?: 1;
39 2
    }
40
41
    /**
42
     * @param $url
43
     */
44 2
    public function setUrl(string $url): void
45
    {
46 2
        $this->url = $url;
47 2
    }
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 3
    public function getPagerSize(): int
80
    {
81 3
        return $this->pagerSize;
82
    }
83
84
    /**
85
     * @throws PaginatorException
86
     */
87 4
    private function ensurePageCount()
88
    {
89 4
        if (!$this->pageCount) {
90 1
            throw new PaginatorException(PaginatorException::NO_PAGE_COUNT);
91
        }
92 3
    }
93
94
    /**
95
     * @throws PaginatorException
96
     */
97 3
    private function ensureUrl()
98
    {
99 3
        if (!$this->url) {
100 1
            throw new PaginatorException(PaginatorException::NO_URL);
101
        }
102 2
    }
103
104
    /**
105
     * @throws PaginatorException
106
     */
107 2
    private function ensureCurrentPage()
108
    {
109 2
        if (!$this->currentPage) {
110
            throw new PaginatorException(PaginatorException::NO_CURRENT_PAGE);
111
        }
112 2
    }
113
114
    /**
115
     * @return int
116
     */
117 2
    private function calculateStart(int $pages): int
118
    {
119 2
        $half = ($pages - 1) / 2;
120 2
        if ($this->currentPage < 3) {
121 2
            $start = 1;
122
        } elseif ($this->currentPage >= ($this->pageCount - $half)) {
123
            $start = $this->pageCount - ($this->getPagerSize() - 1);
124
        } else {
125
            $start = $this->currentPage - $half;
126
            if ($start < 1) {
127
                $start = 1;
128
            }
129
        }
130
131 2
        return $start;
132
    }
133
134
    /**
135
     * @return string
136
     * @throws PaginatorException
137
     */
138 4
    public function render(): string
139
    {
140 4
        $this->ensurePageCount();
141 3
        $this->ensureUrl();
142 2
        $this->ensureCurrentPage();
143
144 2
        $html = '<nav><ul class="pagination">';
145
146 2
        if ($this->pageCount > ($this->getPagerSize() - 1)) {
147 2
            $pages = $this->getPagerSize();
148 2
            $start = $this->calculateStart($pages);
149
        } else {
150
            $pages = $this->pageCount;
151
            $start = 1;
152
        }
153
154 2
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
155 2
        if ($this->currentPage === 1) {
156 1
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::FAST_BACKWARD, 'disabled') . '</a>';
157
        } else {
158 1
            $html .= '<a class="page-link"  href ="' . $this->url(1) . '">' . Icon::FAST_BACKWARD . '</a>';
159
        }
160 2
        $html .= '</li>';
161
162 2
        $html .= ($this->currentPage === 1) ? '<li class="page-item disabled">' :'<li class="page-item">';
163 2 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...
164 1
            $html .= '<a class="page-link"  href ="#">' . Icon::custom(Icon::BACKWARD, 'disabled') . '</a>';
165
        } else {
166 1
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage - 1) . '">' . Icon::BACKWARD . '</a>';
167
        }
168 2
        $html .= '</li>';
169
170 2
        for ($x = $start; $x <= ($start + ($pages - 1)); $x++) {
171 2
            $html .= '<li class="page-item ';
172 2
            if ($this->currentPage === $x) {
173 2
                $html .= ' active" aria-current="page';
174
            }
175 2
            $html .= '">';
176 2
            if ($this->currentPage === $x) {
177 2
                $html .= '<a class="page-link" href="#">' . $x . '</a>';
178
            } else {
179 2
                $html .= '<a class="page-link" href="' . $this->url($x) .'">' . $x . '</a>';
180
            }
181 2
            $html .= '</li>';
182
        }
183
184 2
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' :'<li class="page-item">';
185 2 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...
186
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FORWARD, 'disabled') . '</a>';
187
        } else {
188 2
            $html .= '<a class="page-link"  href ="' . $this->url($this->currentPage + 1) . '">' . Icon::FORWARD . '</i></a>';
189
        }
190 2
        $html .= '</li>';
191
192 2
        $html .= ($this->currentPage >= $this->pageCount) ? '<li class="page-item disabled">' : '<li class="page-item">';
193 2 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...
194
            $html .= '<a class="page-link" href="#">' . Icon::custom(Icon::FAST_FORWARD, 'disabled') . '</a>';
195
        } else {
196 2
            $html .= '<a class="page-link"  href ="' . $this->url($this->pageCount) . '">' . Icon::FAST_FORWARD . '</i></a>';
197
        }
198 2
        $html .= '</li>';
199 2
        $html .= '</ul></nav>';
200
201 2
        return $html;
202
    }
203
}
204