PageInformation   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 98
ccs 34
cts 38
cp 0.8947
rs 10
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A currentPage() 0 3 1
A fromMessage() 0 10 2
A __construct() 0 14 1
A pageLength() 0 3 1
A hasMorePages() 0 3 1
A firstRecord() 0 3 1
A lastRecord() 0 3 1
A totalRecords() 0 3 1
A fromValues() 0 10 2
A empty() 0 3 1
A totalPages() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services\Registration;
6
7
class PageInformation
8
{
9
    /** @var int */
10
    private $firstRecord;
11
12
    /** @var int */
13
    private $lastRecord;
14
15
    /** @var int */
16
    private $totalRecords;
17
18
    /** @var int */
19
    private $currentPage;
20
21
    /** @var int */
22
    private $totalPages;
23
24
    /** @var int */
25
    private $pageLength;
26
27 4
    public function __construct(
28
        int $firstRecord,
29
        int $lastRecord,
30
        int $totalRecords,
31
        int $currentPage,
32
        int $totalPages,
33
        int $pageLength
34
    ) {
35 4
        $this->firstRecord = $firstRecord;
36 4
        $this->lastRecord = $lastRecord;
37 4
        $this->totalRecords = $totalRecords;
38 4
        $this->currentPage = $currentPage;
39 4
        $this->totalPages = $totalPages;
40 4
        $this->pageLength = $pageLength;
41
    }
42
43
    public static function empty(): self
44
    {
45
        return new self(1, 50, 0, 1, 1, 50);
46
    }
47
48 4
    public static function fromMessage(string $message): self
49
    {
50 4
        $found = preg_match('/^Showing (?<first>\d+) to (?<last>\d+) of (?<records>\d+) entries$/', $message, $matches);
51 4
        if (1 !== $found) {
52
            return self::empty();
53
        }
54 4
        $first = intval($matches['first']);
55 4
        $last = intval($matches['last']);
56 4
        $records = intval($matches['records']);
57 4
        return self::fromValues($first, $last, $records);
58
    }
59
60 4
    public static function fromValues(int $first, int $last, int $records): self
61
    {
62 4
        $pageLength = $last - $first + 1;
63 4
        if (0 === $pageLength) {
64
            return new self($first, $last, $records, 1, 1, $pageLength);
65
        }
66 4
        $pages = intval(ceil($records / $pageLength));
67 4
        $page = intval(round($last / $pageLength, 0));
68
69 4
        return new self($first, $last, $records, $page, $pages, $pageLength);
70
    }
71
72 1
    public function firstRecord(): int
73
    {
74 1
        return $this->firstRecord;
75
    }
76
77 1
    public function lastRecord(): int
78
    {
79 1
        return $this->lastRecord;
80
    }
81
82 1
    public function totalRecords(): int
83
    {
84 1
        return $this->totalRecords;
85
    }
86
87 1
    public function currentPage(): int
88
    {
89 1
        return $this->currentPage;
90
    }
91
92 1
    public function totalPages(): int
93
    {
94 1
        return $this->totalPages;
95
    }
96
97 1
    public function pageLength(): int
98
    {
99 1
        return $this->pageLength;
100
    }
101
102 1
    public function hasMorePages(): bool
103
    {
104 1
        return $this->currentPage < $this->totalPages;
105
    }
106
}
107