Passed
Push — Showing-Posts ( b1a8d3...c3e4ac )
by Stone
02:25
created

Pagination::getPagination()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace App\Modules;
3
4
use Core\Constant;
5
use Core\Modules\Module;
6
use Core\Traits\StringFunctions;
7
8
class Pagination extends Module{
9
10
    use StringFunctions;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Modules\Pagination.
Loading history...
11
12
    public function getPagination(string $page, int $totalPosts):array
13
    {
14
        if(!$this->startsWith($page, "page-"))
15
        {
16
            throw new \Exception("Pagination Error", "404");
17
        }
18
        $pageNo = $this->removeFromBeginning($page, "page-");
19
        $offset = ($pageNo-1) * Constant::POSTS_PER_PAGE;
20
        $totalPages = ceil($totalPosts / Constant::POSTS_PER_PAGE);
21
22
        if($pageNo > $totalPages)
23
        {
24
            throw new \Error("Pagination Number not found", "404");
25
        }
26
27
        return array(
28
          "pageNo" => $pageNo,
29
          "offset" => $offset,
30
          "totalPages" => $totalPages
31
        );
32
33
    }
34
}