Passed
Push — release-11.5.x ( eab588...506b54 )
by Rafael
49:07 queued 25:53
created

ResultsPagination::getPageRangeFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Pagination;
19
20
use TYPO3\CMS\Core\Pagination\PaginationInterface;
21
use TYPO3\CMS\Core\Pagination\PaginatorInterface;
22
23
/**
24
 * Class ResultsPagination
25
 *
26
 * @author Rudy Gnodde <[email protected]>
27
 */
28
class ResultsPagination implements PaginationInterface
29
{
30
    /**
31
     * @var PaginatorInterface
32
     */
33
    protected PaginatorInterface $paginator;
34
35
    /**
36
     * @var int
37
     */
38
    protected int $maxPageNumbers = 0;
39
40
    /**
41
     * @var bool
42
     */
43
    protected bool $hasMorePages = false;
44
45
    /**
46
     * @var bool
47
     */
48
    protected bool $hasLessPages = false;
49
50
    /**
51
     * @var int
52
     */
53
    protected int $pageRangeFirst = 1;
54
55
    /**
56
     * @var int
57
     */
58
    protected int $pageRangeLast = 1;
59
60
    /**
61
     * @param PaginatorInterface $paginator
62
     */
63 33
    public function __construct(PaginatorInterface $paginator)
64
    {
65 33
        $this->paginator = $paginator;
66 33
        $this->calculatePageRange();
67
    }
68
69
    /**
70
     * Get maximum page numbers
71
     *
72
     * @return int
73
     */
74 33
    public function getMaxPageNumbers(): int
75
    {
76 33
        return $this->maxPageNumbers;
77
    }
78
79
    /**
80
     * Set maximum page numbers
81
     *
82
     * @param int $maxPageNumbers
83
     */
84 33
    public function setMaxPageNumbers(int $maxPageNumbers): void
85
    {
86 33
        $this->maxPageNumbers = $maxPageNumbers;
87 33
        $this->calculatePageRange();
88
    }
89
90
    /**
91
     * Get has more pages
92
     *
93
     * @return bool
94
     */
95 26
    public function getHasMorePages(): bool
96
    {
97 26
        return $this->hasMorePages;
98
    }
99
100
    /**
101
     * Get has less pages
102
     *
103
     * @return bool
104
     */
105 26
    public function getHasLessPages(): bool
106
    {
107 26
        return $this->hasLessPages;
108
    }
109
110
    /**
111
     * Get page range first
112
     *
113
     * @return int
114
     */
115
    public function getPageRangeFirst(): int
116
    {
117
        return $this->pageRangeFirst;
118
    }
119
120
    /**
121
     * Get page range last
122
     *
123
     * @return int
124
     */
125
    public function getPageRangeLast(): int
126
    {
127
        return $this->pageRangeLast;
128
    }
129
130
    /**
131
     * Get previous page number
132
     *
133
     * @return int|null
134
     */
135 26
    public function getPreviousPageNumber(): ?int
136
    {
137 26
        $previousPage = $this->paginator->getCurrentPageNumber() - 1;
138
139 26
        if ($previousPage > $this->paginator->getNumberOfPages()) {
140
            return null;
141
        }
142
143 26
        return $previousPage >= $this->getFirstPageNumber() ? $previousPage : null;
144
    }
145
146
    /**
147
     * Get next page number
148
     *
149
     * @return int|null
150
     */
151 26
    public function getNextPageNumber(): ?int
152
    {
153 26
        $nextPage = $this->paginator->getCurrentPageNumber() + 1;
154
155 26
        return $nextPage <= $this->paginator->getNumberOfPages()
156 2
            ? $nextPage
157 26
            : null;
158
    }
159
160
    /**
161
     * Get first page number
162
     *
163
     * @return int
164
     */
165 26
    public function getFirstPageNumber(): int
166
    {
167 26
        return 1;
168
    }
169
170
    /**
171
     * Get last page number
172
     *
173
     * @return int
174
     */
175 33
    public function getLastPageNumber(): int
176
    {
177 33
        return $this->paginator->getNumberOfPages();
178
    }
179
180
    /**
181
     * Get start record number
182
     *
183
     * @return int
184
     */
185 26
    public function getStartRecordNumber(): int
186
    {
187 26
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
188
            return 0;
189
        }
190
191 26
        return $this->paginator->getKeyOfFirstPaginatedItem() + 1;
192
    }
193
194
    /**
195
     * Get end record number
196
     *
197
     * @return int
198
     */
199
    public function getEndRecordNumber(): int
200
    {
201
        if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) {
202
            return 0;
203
        }
204
205
        return $this->paginator->getKeyOfLastPaginatedItem() + 1;
206
    }
207
208
    /**
209
     * Get all page numbers
210
     *
211
     * @return int[]
212
     */
213 26
    public function getAllPageNumbers(): array
214
    {
215 26
        return range($this->pageRangeFirst, $this->pageRangeLast);
216
    }
217
218
    /**
219
     * Calculate page range
220
     */
221 33
    protected function calculatePageRange(): void
222
    {
223 33
        $this->pageRangeFirst = 1;
224 33
        $this->pageRangeLast = $this->getLastPageNumber();
225 33
        $this->hasLessPages = false;
226 33
        $this->hasMorePages = false;
227
228 33
        $maxNumberOfLinks = $this->getMaxPageNumbers();
229 33
        if ($maxNumberOfLinks > 0) {
230
            $numberOfPages = $this->paginator->getNumberOfPages();
231
232
            if ($numberOfPages > $maxNumberOfLinks) {
233
                $currentPage = $this->paginator->getCurrentPageNumber();
234
                $pagesBeforeAndAfter = ($maxNumberOfLinks - 1) / 2;
235
                $this->pageRangeFirst = (int)($currentPage - floor($pagesBeforeAndAfter));
236
                $this->pageRangeLast = (int)($currentPage + ceil($pagesBeforeAndAfter));
237
238
                if ($this->pageRangeFirst < 1) {
239
                    $this->pageRangeLast -= $this->pageRangeFirst - 1;
240
                }
241
                if ($this->pageRangeLast > $numberOfPages) {
242
                    $this->pageRangeFirst -= $this->pageRangeLast - $numberOfPages;
243
                }
244
                $this->pageRangeFirst = max($this->pageRangeFirst, 1);
245
                $this->pageRangeLast = min($this->pageRangeLast, $numberOfPages);
246
                $this->hasLessPages = $this->pageRangeFirst > 1;
247
                $this->hasMorePages = $this->pageRangeLast < $numberOfPages;
248
            }
249
        }
250
    }
251
}
252