-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPaginatedDataProvider.php
More file actions
58 lines (47 loc) · 1.91 KB
/
PaginatedDataProvider.php
File metadata and controls
58 lines (47 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Common\Service\Provider;
use Doctrine\ORM\EntityManagerInterface;
use PhpList\Core\Domain\Common\Model\Filter\FilterRequestInterface;
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
use PhpList\RestBundle\Common\Dto\CursorPaginationResult;
use PhpList\RestBundle\Common\Serializer\CursorPaginationNormalizer;
use PhpList\RestBundle\Common\Service\Factory\PaginationCursorRequestFactory;
use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PaginatedDataProvider
{
public function __construct(
private readonly CursorPaginationNormalizer $paginationNormalizer,
private readonly PaginationCursorRequestFactory $paginationFactory,
private readonly EntityManagerInterface $entityManager,
) {
}
public function getPaginatedList(
Request $request,
NormalizerInterface $normalizer,
string $className,
FilterRequestInterface $filter
): array {
$pagination = $this->paginationFactory->fromRequest($request);
$repository = $this->entityManager->getRepository($className);
if (!$repository instanceof PaginatableRepositoryInterface) {
throw new RuntimeException('Repository not found');
}
$filter->setLimit($pagination->limit);
$filter->setLastId($pagination->afterId);
$result = $repository->getFilteredAfterId(filter: $filter);
$normalizedItems = array_map(
fn($item) => $normalizer->normalize($item, 'json'),
$result->getItems()
);
return $this->paginationNormalizer->normalize(
new CursorPaginationResult(
items: $normalizedItems,
limit: $result->getLimit(),
total: $result->getTotal(),
)
);
}
}