Paginator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPageSize() 0 4 1
A getCurrentPage() 0 4 1
A getNumberOfRecords() 0 4 1
A getNumberOfPages() 0 11 2
A isLastPage() 0 8 2
A getUrlParametersForPage() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\View;
6
7
use Psi\Component\Grid\GridContext;
8
9
class Paginator
10
{
11
    private $options;
12
    private $numberOfRecords;
13
    private $numberOfRecordsOnPage;
14
15
    public function __construct(GridContext $options, int $numberOfRecordsOnPage, int $numberOfRecords = null)
16
    {
17
        $this->options = $options;
18
        $this->numberOfRecords = $numberOfRecords;
19
        $this->numberOfRecordsOnPage = $numberOfRecordsOnPage;
20
    }
21
22
    public function getPageSize(): int
23
    {
24
        return $this->options->getPageSize();
25
    }
26
27
    public function getCurrentPage(): int
28
    {
29
        return $this->options->getCurrentPage();
30
    }
31
32
    public function getNumberOfRecords()
33
    {
34
        return $this->numberOfRecords;
35
    }
36
37
    public function getNumberOfPages(): int
38
    {
39
        if (null === $this->numberOfRecords) {
40
            throw new \RuntimeException(
41
                'Cannot determine the last page when the total number of ' .
42
                'records has not been provided.'
43
            );
44
        }
45
46
        return (int) ceil($this->getNumberOfRecords() / $this->getPageSize());
47
    }
48
49
    public function isLastPage(): bool
50
    {
51
        if (null === $this->numberOfRecords) {
52
            return $this->numberOfRecordsOnPage < $this->getPageSize();
53
        }
54
55
        return $this->getCurrentPage() == $this->getNumberOfPages();
56
    }
57
58
    public function getUrlParametersForPage($page = null)
59
    {
60
        return array_merge($this->options->getUrlParameters(), [
61
            'page' => $page ?: $this->options->getCurrentPage(),
62
        ]);
63
    }
64
}
65