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

Pagination   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPagination() 0 19 3
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
}