PaginationCriteria::getPage()   A
last analyzed

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
declare(strict_types=1);
3
4
namespace KGzocha\Searcher\Criteria;
5
6
/**
7
 * @author Krzysztof Gzocha <[email protected]>
8
 */
9
class PaginationCriteria implements PaginationCriteriaInterface
10
{
11
    /**
12
     * @var int|null
13
     */
14
    private $page;
15
16
    /**
17
     * @var int|null
18
     */
19
    private $itemsPerPage;
20
21
    /**
22
     * @param int $page
23
     * @param int $itemsPerPage
24
     */
25 21
    public function __construct(
26
        int $page = null,
27
        int $itemsPerPage = null
28
    ) {
29 21
        $this->setPage($page);
30 21
        $this->setItemsPerPage($itemsPerPage);
31 21
    }
32
33
    /**
34
     * @return int|null
35
     */
36 6
    public function getPage()
37
    {
38 6
        return $this->page;
39
    }
40
41
    /**
42
     * @param int $page
43
     */
44 21
    public function setPage(int $page = null)
45
    {
46 21
        $this->page = $this->convert($page);
47 21
    }
48
49
    /**
50
     * @return int|null
51
     */
52 7
    public function getItemsPerPage()
53
    {
54 7
        return $this->itemsPerPage;
55
    }
56
57
    /**
58
     * @param int $itemsPerPage
59
     *
60
     * @throws \BadMethodCallException
61
     */
62 21
    public function setItemsPerPage(int $itemsPerPage = null)
63
    {
64 21
        $this->itemsPerPage = $this->convert($itemsPerPage);
65 21
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 12
    public function shouldBeApplied(): bool
71
    {
72 12
        return $this->page !== 0 && $this->itemsPerPage !== 0;
73
    }
74
75
    /**
76
     * @param int $number
77
     *
78
     * @return int
79
     */
80 21
    private function convert(int $number = null): int
81
    {
82 21
        return (int) abs($number);
83
    }
84
}
85