Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

NumberedPagination   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 129
rs 10
c 1
b 0
f 0
wmc 25

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getMaximumNumberOfLinks() 0 3 1
A getHasMorePages() 0 3 1
A calculateDisplayRange() 0 22 5
A getLastPageNumber() 0 3 1
A getNextPageNumber() 0 7 2
A getDisplayRangeStart() 0 3 1
A getPreviousPageNumber() 0 11 3
A getEndRecordNumber() 0 7 2
A __construct() 0 7 2
A getStartRecordNumber() 0 7 2
A getHasLessPages() 0 3 1
A getAllPageNumbers() 0 3 1
A getDisplayRangeEnd() 0 3 1
A getPaginator() 0 3 1
A getFirstPageNumber() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Pagination;
13
14
use TYPO3\CMS\Core\Pagination\PaginationInterface;
15
use TYPO3\CMS\Core\Pagination\PaginatorInterface;
16
17
final class NumberedPagination implements PaginationInterface
18
{
19
    protected PaginatorInterface $paginator;
20
21
    protected int $maximumNumberOfLinks = 10;
22
    protected int $displayRangeStart = 0;
23
    protected int $displayRangeEnd = 0;
24
    protected bool $hasLessPages = false;
25
    protected bool $hasMorePages = false;
26
27
    public function __construct(PaginatorInterface $paginator, int $maximumNumberOfLinks = 0)
28
    {
29
        $this->paginator = $paginator;
30
        if ($maximumNumberOfLinks > 0) {
31
            $this->maximumNumberOfLinks = $maximumNumberOfLinks;
32
        }
33
        $this->calculateDisplayRange();
34
    }
35
36
    public function getPreviousPageNumber(): ?int
37
    {
38
        $previousPage = $this->paginator->getCurrentPageNumber() - 1;
39
40
        if ($previousPage > $this->paginator->getNumberOfPages()) {
41
            return null;
42
        }
43
44
        return $previousPage >= $this->getFirstPageNumber()
45
            ? $previousPage
46
            : null;
47
    }
48
49
    public function getNextPageNumber(): ?int
50
    {
51
        $nextPage = $this->paginator->getCurrentPageNumber() + 1;
52
53
        return $nextPage <= $this->paginator->getNumberOfPages()
54
            ? $nextPage
55
            : null;
56
    }
57
58
    public function getFirstPageNumber(): int
59
    {
60
        return 1;
61
    }
62
63
    public function getLastPageNumber(): int
64
    {
65
        return $this->paginator->getNumberOfPages();
66
    }
67
68
    public function getStartRecordNumber(): int
69
    {
70
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
71
            return 0;
72
        }
73
74
        return $this->paginator->getKeyOfFirstPaginatedItem() + 1;
75
    }
76
77
    public function getEndRecordNumber(): int
78
    {
79
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
80
            return 0;
81
        }
82
83
        return $this->paginator->getKeyOfLastPaginatedItem() + 1;
84
    }
85
86
    /**
87
     * @return int[]
88
     */
89
    public function getAllPageNumbers(): array
90
    {
91
        return range($this->displayRangeStart, $this->displayRangeEnd);
92
    }
93
94
    public function getHasLessPages(): bool
95
    {
96
        return $this->hasLessPages;
97
    }
98
99
    public function getHasMorePages(): bool
100
    {
101
        return $this->hasMorePages;
102
    }
103
104
    public function getMaximumNumberOfLinks(): int
105
    {
106
        return $this->maximumNumberOfLinks;
107
    }
108
109
    public function getDisplayRangeStart(): int
110
    {
111
        return $this->displayRangeStart;
112
    }
113
114
    public function getDisplayRangeEnd(): int
115
    {
116
        return $this->displayRangeEnd;
117
    }
118
119
    protected function calculateDisplayRange(): void
120
    {
121
        $numberOfPages = $this->paginator->getNumberOfPages();
122
        $currentPage = $this->paginator->getCurrentPageNumber();
123
124
        $maximumNumberOfLinks = $this->maximumNumberOfLinks;
125
        if ($maximumNumberOfLinks > $numberOfPages) {
126
            $maximumNumberOfLinks = $numberOfPages;
127
        }
128
        $delta = floor($maximumNumberOfLinks / 2);
129
        $this->displayRangeStart = (int)($currentPage - $delta);
130
        $this->displayRangeEnd = (int)($currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0));
131
        if ($this->displayRangeStart < 1) {
132
            $this->displayRangeEnd -= $this->displayRangeStart - 1;
133
        }
134
        if ($this->displayRangeEnd > $numberOfPages) {
135
            $this->displayRangeStart -= $this->displayRangeEnd - $numberOfPages;
136
        }
137
        $this->displayRangeStart = (integer)max($this->displayRangeStart, 1);
138
        $this->displayRangeEnd = (integer)min($this->displayRangeEnd, $numberOfPages);
139
        $this->hasLessPages = $this->displayRangeStart > 2;
140
        $this->hasMorePages = $this->displayRangeEnd + 1 < $this->paginator->getNumberOfPages();
141
    }
142
143
    public function getPaginator(): PaginatorInterface
144
    {
145
        return $this->paginator;
146
    }
147
}
148