Passed
Push — main ( 90e997...fc6c20 )
by Breno
02:07
created

Pagination   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 0 3 1
A page() 0 3 1
A __construct() 0 12 3
A withMaxLimit() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus\Query;
5
6
use InvalidArgumentException;
7
8
class Pagination
9
{
10
    const MIN_PAGE_NUMBER = 1;
11
    const MIN_PAGE_LIMIT = 1;
12
13
    /**
14
     * @var int
15
     */
16
    protected $page;
17
18
    /**
19
     * @var int
20
     */
21
    protected $limit;
22
23
    public function __construct(int $page, int $limit)
24
    {
25
        if ($page < self::MIN_PAGE_NUMBER) {
26
            throw new InvalidArgumentException(sprintf("Invalid page number (%s).", $page));
27
        }
28
29
        if ($limit < self::MIN_PAGE_LIMIT) {
30
            throw new InvalidArgumentException(sprintf("Invalid page limit (%s).", $limit));
31
        }
32
33
        $this->page = $page;
34
        $this->limit = $limit;
35
    }
36
37
    public function page(): int
38
    {
39
        return $this->page;
40
    }
41
42
    public function limit(): int
43
    {
44
        return $this->limit;
45
    }
46
47
    public function withMaxLimit(int $max): self
48
    {
49
        return $this->limit > $max ? new self($this->page, $max) : $this;
50
    }
51
}
52