Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
38.46% |
15 / 39 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Pagination | |
38.46% |
15 / 39 |
|
40.00% |
2 / 5 |
27.88 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setRouteName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getPagination | |
57.89% |
11 / 19 |
|
0.00% |
0 / 1 |
2.30 | |||
| linkToRoute | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| sortParameters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Helper; |
| 10 | |
| 11 | use OCA\Libresign\Db\PagerFantaQueryAdapter; |
| 12 | use OCA\Libresign\Vendor\Pagerfanta\Pagerfanta; |
| 13 | use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | use OCP\IURLGenerator; |
| 15 | |
| 16 | /** |
| 17 | * @psalm-import-type LibresignPagination from \OCA\Libresign\ResponseDefinitions |
| 18 | * @extends Pagerfanta<array<string, mixed>> |
| 19 | */ |
| 20 | class Pagination extends Pagerfanta { |
| 21 | private string $routeName; |
| 22 | public function __construct( |
| 23 | IQueryBuilder $queryBuilder, |
| 24 | private IURLGenerator $urlGenerator, |
| 25 | private IQueryBuilder $countQueryBuilder, |
| 26 | ) { |
| 27 | $adapter = new PagerFantaQueryAdapter($queryBuilder, $countQueryBuilder); |
| 28 | parent::__construct($adapter); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return static |
| 33 | */ |
| 34 | public function setRouteName(string $routeName = ''): self { |
| 35 | $this->routeName = $routeName; |
| 36 | return $this; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return LibresignPagination |
| 41 | */ |
| 42 | public function getPagination(int $page, int $length, array $filter = []): array { |
| 43 | $this->setMaxPerPage($length); |
| 44 | $total = $this->count(); |
| 45 | if ($total > $length) { |
| 46 | return [ |
| 47 | 'total' => $total, |
| 48 | 'current' => $this->linkToRoute(true, $page, $length, $filter), |
| 49 | 'next' => $this->linkToRoute($this->hasNextPage(), 'getNextPage', $length, $filter), |
| 50 | 'prev' => $this->linkToRoute($this->hasPreviousPage(), 'getPreviousPage', $length, $filter), |
| 51 | 'last' => $this->linkToRoute($this->hasNextPage(), 'getNbPages', $length, $filter), |
| 52 | 'first' => $this->linkToRoute($this->hasPreviousPage(), 1, $length, $filter), |
| 53 | ]; |
| 54 | } |
| 55 | return [ |
| 56 | 'total' => $total, |
| 57 | 'current' => null, |
| 58 | 'next' => null, |
| 59 | 'prev' => null, |
| 60 | 'last' => null, |
| 61 | 'first' => null, |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | private function linkToRoute(bool $condition, int|string $page, int $length, array $filter): ?string { |
| 66 | if (!$condition) { |
| 67 | return null; |
| 68 | } |
| 69 | if (is_string($page)) { |
| 70 | $page = $this->$page(); |
| 71 | } |
| 72 | $url = $this->urlGenerator->linkToRouteAbsolute( |
| 73 | $this->routeName, |
| 74 | array_merge(['page' => $page, 'length' => $length, 'apiVersion' => 'v1'], $filter) |
| 75 | ); |
| 76 | $url = $this->sortParameters($url); |
| 77 | return $url; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * This is necessary to fix problem at integration tests because the method linkToRoute change the order of parameters |
| 82 | */ |
| 83 | private function sortParameters(?string $url): ?string { |
| 84 | if (!$url) { |
| 85 | return $url; |
| 86 | } |
| 87 | parse_str(parse_url($url, PHP_URL_QUERY), $query); |
| 88 | ksort($query); |
| 89 | $url = strtok($url, '?') . '?' . http_build_query($query); |
| 90 | return $url; |
| 91 | } |
| 92 | } |