Completed
Pull Request — master (#2)
by Kevin
01:29
created

Pager::haveToPaginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\Porpaginas;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
abstract class Pager implements \Countable, \IteratorAggregate
9
{
10 21
    final public function getNextPage(): ?int
11
    {
12 21
        $currentPage = $this->getCurrentPage();
13
14 21
        if ($currentPage === $this->getLastPage()) {
15 13
            return null;
16
        }
17
18 8
        return ++$currentPage;
19
    }
20
21 21
    final public function getPreviousPage(): ?int
22
    {
23 21
        $page = $this->getCurrentPage();
24
25 21
        if (1 === $page) {
26 12
            return null;
27
        }
28
29 9
        return --$page;
30
    }
31
32 21
    final public function getFirstPage(): int
33
    {
34 21
        return 1;
35
    }
36
37 22
    final public function getLastPage(): int
38
    {
39 22
        $totalCount = $this->totalCount();
40
41 22
        if (0 === $totalCount) {
42 5
            return 1;
43
        }
44
45 17
        return (int) \ceil($totalCount / $this->getLimit());
46
    }
47
48 20
    final public function pagesCount(): int
49
    {
50 20
        return $this->getLastPage();
51
    }
52
53 20
    final public function haveToPaginate(): bool
54
    {
55 20
        return $this->pagesCount() > 1;
56
    }
57
58
    abstract public function getCurrentPage(): int;
59
60
    abstract public function getLimit(): int;
61
62
    /**
63
     * The result count for the current page.
64
     */
65
    abstract public function count(): int;
66
67
    /**
68
     * The total result count.
69
     */
70
    abstract public function totalCount(): int;
71
72
    /**
73
     * Return an iterator over selected windows of results of the paginatable.
74
     */
75
    abstract public function getIterator(): \Traversable;
76
}
77