Skip to content

Commit 9bd58bc

Browse files
committed
ISSUE-345: pagination data provider
1 parent a2e98fd commit 9bd58bc

12 files changed

+104
-104
lines changed

config/services/providers.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ services:
44
autoconfigure: true
55
public: false
66

7-
PhpList\RestBundle\Service\Provider\SubscriberListProvider:
8-
autowire: true
9-
autoconfigure: true
10-
11-
PhpList\RestBundle\Service\Provider\AdministratorProvider:
7+
PhpList\RestBundle\Service\Provider\PaginatedDataProvider:
128
autowire: true
139
autoconfigure: true

src/Controller/AdministratorController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpList\RestBundle\Entity\Request\UpdateAdministratorRequest;
1212
use PhpList\RestBundle\Serializer\AdministratorNormalizer;
1313
use PhpList\RestBundle\Service\Manager\AdministratorManager;
14-
use PhpList\RestBundle\Service\Provider\AdministratorProvider;
14+
use PhpList\RestBundle\Service\Provider\PaginatedDataProvider;
1515
use PhpList\RestBundle\Validator\RequestValidator;
1616
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1717
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -28,19 +28,19 @@ class AdministratorController extends BaseController
2828
{
2929
private AdministratorManager $administratorManager;
3030
private AdministratorNormalizer $normalizer;
31-
private AdministratorProvider $administratorProvider;
31+
private PaginatedDataProvider $paginatedProvider;
3232

3333
public function __construct(
3434
Authentication $authentication,
35+
RequestValidator $validator,
3536
AdministratorManager $administratorManager,
3637
AdministratorNormalizer $normalizer,
37-
RequestValidator $validator,
38-
AdministratorProvider $administratorProvider
38+
PaginatedDataProvider $paginatedProvider
3939
) {
4040
parent::__construct($authentication, $validator);
4141
$this->administratorManager = $administratorManager;
4242
$this->normalizer = $normalizer;
43-
$this->administratorProvider = $administratorProvider;
43+
$this->paginatedProvider = $paginatedProvider;
4444
}
4545

4646
#[Route('', name: 'get_administrators', methods: ['GET'])]
@@ -101,7 +101,7 @@ public function getAdministrators(Request $request): JsonResponse
101101
$this->requireAuthentication($request);
102102

103103
return new JsonResponse(
104-
$this->administratorProvider->getPaginatedList($request),
104+
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Administrator::class),
105105
Response::HTTP_OK
106106
);
107107
}

src/Controller/CampaignController.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
namespace PhpList\RestBundle\Controller;
66

77
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Filter\MessageFilter;
89
use PhpList\Core\Domain\Model\Messaging\Message;
910
use PhpList\Core\Security\Authentication;
1011
use PhpList\RestBundle\Entity\Request\CreateMessageRequest;
1112
use PhpList\RestBundle\Entity\Request\UpdateMessageRequest;
1213
use PhpList\RestBundle\Serializer\MessageNormalizer;
1314
use PhpList\RestBundle\Service\Manager\MessageManager;
15+
use PhpList\RestBundle\Service\Provider\PaginatedDataProvider;
1416
use PhpList\RestBundle\Validator\RequestValidator;
1517
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1618
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -29,16 +31,19 @@ class CampaignController extends BaseController
2931
{
3032
private MessageNormalizer $normalizer;
3133
private MessageManager $messageManager;
34+
private PaginatedDataProvider $paginatedProvider;
3235

3336
public function __construct(
3437
Authentication $authentication,
3538
RequestValidator $validator,
3639
MessageNormalizer $normalizer,
37-
MessageManager $messageManager
40+
MessageManager $messageManager,
41+
PaginatedDataProvider $paginatedProvider,
3842
) {
3943
parent::__construct($authentication, $validator);
4044
$this->normalizer = $normalizer;
4145
$this->messageManager = $messageManager;
46+
$this->paginatedProvider = $paginatedProvider;
4247
}
4348

4449
#[Route('', name: 'get_campaigns', methods: ['GET'])]
@@ -56,15 +61,36 @@ public function __construct(
5661
schema: new OA\Schema(
5762
type: 'string'
5863
)
64+
),
65+
new OA\Parameter(
66+
name: 'after_id',
67+
description: 'Last id (starting from 0)',
68+
in: 'query',
69+
required: false,
70+
schema: new OA\Schema(type: 'integer', default: 1, minimum: 1)
71+
),
72+
new OA\Parameter(
73+
name: 'limit',
74+
description: 'Number of results per page',
75+
in: 'query',
76+
required: false,
77+
schema: new OA\Schema(type: 'integer', default: 25, maximum: 100, minimum: 1)
5978
)
6079
],
6180
responses: [
6281
new OA\Response(
6382
response: 200,
6483
description: 'Success',
6584
content: new OA\JsonContent(
66-
type: 'array',
67-
items: new OA\Items(ref: '#/components/schemas/Message')
85+
properties: [
86+
new OA\Property(
87+
property: 'items',
88+
type: 'array',
89+
items: new OA\Items(ref: '#/components/schemas/Message')
90+
),
91+
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
92+
],
93+
type: 'object'
6894
)
6995
),
7096
new OA\Response(
@@ -77,13 +103,13 @@ public function __construct(
77103
public function getMessages(Request $request): JsonResponse
78104
{
79105
$authUer = $this->requireAuthentication($request);
80-
$data = $this->messageManager->getMessagesByOwner($authUer);
81106

82-
$normalized = array_map(function ($item) {
83-
return $this->normalizer->normalize($item);
84-
}, $data);
107+
$filter = (new MessageFilter())->setOwner($authUer);
85108

86-
return new JsonResponse($normalized, Response::HTTP_OK);
109+
return new JsonResponse(
110+
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Message::class, $filter),
111+
Response::HTTP_OK
112+
);
87113
}
88114

89115
#[Route('/{messageId}', name: 'get_campaign', methods: ['GET'])]

src/Controller/ListController.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
use PhpList\Core\Security\Authentication;
1010
use PhpList\RestBundle\Entity\Request\CreateSubscriberListRequest;
1111
use PhpList\RestBundle\Serializer\SubscriberListNormalizer;
12-
use PhpList\RestBundle\Service\Factory\PaginationCursorRequestFactory;
1312
use PhpList\RestBundle\Service\Manager\SubscriberListManager;
14-
use PhpList\RestBundle\Service\Provider\SubscriberListProvider;
13+
use PhpList\RestBundle\Service\Provider\PaginatedDataProvider;
1514
use PhpList\RestBundle\Validator\RequestValidator;
1615
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1716
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -32,22 +31,19 @@ class ListController extends BaseController
3231
{
3332
private SubscriberListNormalizer $normalizer;
3433
private SubscriberListManager $subscriberListManager;
35-
private PaginationCursorRequestFactory $paginationFactory;
36-
private SubscriberListProvider $subscriberListProvider;
34+
private PaginatedDataProvider $paginatedDataProvider;
3735

3836
public function __construct(
3937
Authentication $authentication,
40-
SubscriberListNormalizer $normalizer,
4138
RequestValidator $validator,
39+
SubscriberListNormalizer $normalizer,
4240
SubscriberListManager $subscriberListManager,
43-
PaginationCursorRequestFactory $paginationFactory,
44-
SubscriberListProvider $subscriberListProvider
41+
PaginatedDataProvider $paginatedDataProvider,
4542
) {
4643
parent::__construct($authentication, $validator);
4744
$this->normalizer = $normalizer;
4845
$this->subscriberListManager = $subscriberListManager;
49-
$this->paginationFactory = $paginationFactory;
50-
$this->subscriberListProvider = $subscriberListProvider;
46+
$this->paginatedDataProvider = $paginatedDataProvider;
5147
}
5248

5349
#[Route('', name: 'get_lists', methods: ['GET'])]
@@ -107,10 +103,9 @@ public function __construct(
107103
public function getLists(Request $request): JsonResponse
108104
{
109105
$this->requireAuthentication($request);
110-
$pagination = $this->paginationFactory->fromRequest($request);
111106

112107
return new JsonResponse(
113-
$this->subscriberListProvider->getPaginatedList($pagination),
108+
$this->paginatedDataProvider->getPaginatedList($request, $this->normalizer, SubscriberList::class),
114109
Response::HTTP_OK
115110
);
116111
}

src/Controller/SessionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class SessionController extends BaseController
3232

3333
public function __construct(
3434
Authentication $authentication,
35+
RequestValidator $validator,
3536
SessionManager $sessionManager,
36-
RequestValidator $validator
3737
) {
3838
parent::__construct($authentication, $validator);
3939

src/Controller/SubscriberController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class SubscriberController extends BaseController
3333

3434
public function __construct(
3535
Authentication $authentication,
36-
SubscriberManager $subscriberManager,
3736
RequestValidator $validator,
37+
SubscriberManager $subscriberManager,
3838
SubscriberNormalizer $subscriberNormalizer,
3939
) {
4040
parent::__construct($authentication, $validator);

src/Controller/SubscriptionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class SubscriptionController extends BaseController
3333

3434
public function __construct(
3535
Authentication $authentication,
36-
SubscriptionManager $subscriptionManager,
3736
RequestValidator $validator,
37+
SubscriptionManager $subscriptionManager,
3838
SubscriberNormalizer $subscriberNormalizer,
3939
SubscriptionNormalizer $subscriptionNormalizer,
4040
) {

src/Controller/TemplateController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class TemplateController extends BaseController
3333

3434
public function __construct(
3535
Authentication $authentication,
36+
RequestValidator $validator,
3637
TemplateRepository $templateRepository,
3738
TemplateNormalizer $normalizer,
38-
RequestValidator $validator,
3939
TemplateManager $templateManager
4040
) {
4141
parent::__construct($authentication, $validator);

src/Service/Provider/AdministratorProvider.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Service\Provider;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Filter\FilterRequestInterface;
9+
use PhpList\Core\Domain\Repository\Interfaces\PaginatableRepositoryInterface;
10+
use PhpList\RestBundle\Entity\Dto\CursorPaginationResult;
11+
use PhpList\RestBundle\Serializer\CursorPaginationNormalizer;
12+
use PhpList\RestBundle\Service\Factory\PaginationCursorRequestFactory;
13+
use RuntimeException;
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16+
17+
class PaginatedDataProvider
18+
{
19+
public function __construct(
20+
private readonly CursorPaginationNormalizer $paginationNormalizer,
21+
private readonly PaginationCursorRequestFactory $paginationFactory,
22+
private readonly EntityManagerInterface $entityManager,
23+
) {
24+
}
25+
26+
public function getPaginatedList(
27+
Request $request,
28+
NormalizerInterface $normalizer,
29+
string $className,
30+
FilterRequestInterface $filter = null
31+
): array {
32+
$pagination = $this->paginationFactory->fromRequest($request);
33+
34+
$repository = $this->entityManager->getRepository($className);
35+
36+
if (!$repository instanceof PaginatableRepositoryInterface) {
37+
throw new RuntimeException('Repository not found');
38+
}
39+
40+
$items = $repository->getFilteredAfterId($pagination->afterId, $pagination->limit, $filter);
41+
$total = $repository->count();
42+
43+
$normalizedItems = array_map(
44+
fn($item) => $normalizer->normalize($item, 'json'),
45+
$items
46+
);
47+
48+
return $this->paginationNormalizer->normalize(
49+
new CursorPaginationResult($normalizedItems, $pagination->limit, $total)
50+
);
51+
}
52+
}

src/Service/Provider/SubscriberListProvider.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/Unit/Controller/TemplateControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ protected function setUp(): void
3636

3737
$this->controller = new TemplateController(
3838
$authentication,
39+
$this->validator,
3940
$this->templateRepository,
4041
$this->normalizer,
41-
$this->validator,
4242
$this->templateManager
4343
);
4444
}

0 commit comments

Comments
 (0)