5
5
namespace PhpList \RestBundle \Controller ;
6
6
7
7
use OpenApi \Attributes as OA ;
8
+ use PhpList \Core \Domain \Filter \SubscriberFilter ;
9
+ use PhpList \Core \Domain \Model \Subscription \Subscriber ;
8
10
use PhpList \Core \Domain \Model \Subscription \SubscriberList ;
9
11
use PhpList \Core \Security \Authentication ;
10
12
use PhpList \RestBundle \Entity \Request \SubscriptionRequest ;
11
13
use PhpList \RestBundle \Serializer \SubscriberNormalizer ;
12
14
use PhpList \RestBundle \Serializer \SubscriptionNormalizer ;
13
15
use PhpList \RestBundle \Service \Manager \SubscriptionManager ;
16
+ use PhpList \RestBundle \Service \Provider \PaginatedDataProvider ;
14
17
use PhpList \RestBundle \Validator \RequestValidator ;
15
18
use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
16
19
use Symfony \Component \HttpFoundation \JsonResponse ;
17
20
use Symfony \Component \HttpFoundation \Request ;
18
21
use Symfony \Component \HttpFoundation \Response ;
19
- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
20
22
use Symfony \Component \Routing \Attribute \Route ;
21
23
22
24
/**
@@ -30,18 +32,21 @@ class SubscriptionController extends BaseController
30
32
private SubscriptionManager $ subscriptionManager ;
31
33
private SubscriberNormalizer $ subscriberNormalizer ;
32
34
private SubscriptionNormalizer $ subscriptionNormalizer ;
35
+ private PaginatedDataProvider $ paginatedProvider ;
33
36
34
37
public function __construct (
35
38
Authentication $ authentication ,
36
39
RequestValidator $ validator ,
37
40
SubscriptionManager $ subscriptionManager ,
38
41
SubscriberNormalizer $ subscriberNormalizer ,
39
42
SubscriptionNormalizer $ subscriptionNormalizer ,
43
+ PaginatedDataProvider $ paginatedProvider ,
40
44
) {
41
45
parent ::__construct ($ authentication , $ validator );
42
46
$ this ->subscriptionManager = $ subscriptionManager ;
43
47
$ this ->subscriberNormalizer = $ subscriberNormalizer ;
44
48
$ this ->subscriptionNormalizer = $ subscriptionNormalizer ;
49
+ $ this ->paginatedProvider = $ paginatedProvider ;
45
50
}
46
51
47
52
#[Route('/{listId}/subscribers ' , name: 'get_subscriber_from_list ' , methods: ['GET ' ])]
@@ -64,15 +69,36 @@ public function __construct(
64
69
in: 'path ' ,
65
70
required: true ,
66
71
schema: new OA \Schema (type: 'string ' )
72
+ ),
73
+ new OA \Parameter (
74
+ name: 'after_id ' ,
75
+ description: 'Last id (starting from 0) ' ,
76
+ in: 'query ' ,
77
+ required: false ,
78
+ schema: new OA \Schema (type: 'integer ' , default: 1 , minimum: 1 )
79
+ ),
80
+ new OA \Parameter (
81
+ name: 'limit ' ,
82
+ description: 'Number of results per page ' ,
83
+ in: 'query ' ,
84
+ required: false ,
85
+ schema: new OA \Schema (type: 'integer ' , default: 25 , maximum: 100 , minimum: 1 )
67
86
)
68
87
],
69
88
responses: [
70
89
new OA \Response (
71
90
response: 200 ,
72
91
description: 'Success ' ,
73
92
content: new OA \JsonContent (
74
- type: 'array ' ,
75
- items: new OA \Items (ref: '#/components/schemas/Subscriber ' )
93
+ properties: [
94
+ new OA \Property (
95
+ property: 'items ' ,
96
+ type: 'array ' ,
97
+ items: new OA \Items (ref: '#/components/schemas/Subscriber ' )
98
+ ),
99
+ new OA \Property (property: 'pagination ' , ref: '#/components/schemas/CursorPagination ' )
100
+ ],
101
+ type: 'object '
76
102
)
77
103
),
78
104
new OA \Response (
@@ -94,13 +120,18 @@ public function getListMembers(
94
120
$ this ->requireAuthentication ($ request );
95
121
96
122
if (!$ list ) {
97
- throw new NotFoundHttpException ('Subscriber list not found. ' );
123
+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
98
124
}
99
125
100
- $ subscribers = $ this ->subscriptionManager ->getSubscriberListMembers ($ list );
101
- $ normalized = array_map (fn ($ subscriber ) => $ this ->subscriberNormalizer ->normalize ($ subscriber ), $ subscribers );
102
-
103
- return new JsonResponse ($ normalized , Response::HTTP_OK );
126
+ return new JsonResponse (
127
+ $ this ->paginatedProvider ->getPaginatedList (
128
+ $ request ,
129
+ $ this ->subscriberNormalizer ,
130
+ Subscriber::class,
131
+ (new SubscriberFilter ())->setListId ($ list ->getId ())
132
+ ),
133
+ Response::HTTP_OK
134
+ );
104
135
}
105
136
106
137
#[Route('/{listId}/subscribers/count ' , name: 'get_subscribers_count_from_list ' , methods: ['GET ' ])]
@@ -154,7 +185,7 @@ public function getSubscribersCount(
154
185
$ this ->requireAuthentication ($ request );
155
186
156
187
if (!$ list ) {
157
- throw new NotFoundHttpException ('Subscriber list not found. ' );
188
+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
158
189
}
159
190
160
191
return new JsonResponse (['subscribers_count ' => count ($ list ->getSubscribers ())], Response::HTTP_OK );
@@ -240,7 +271,7 @@ public function createSubscription(
240
271
$ this ->requireAuthentication ($ request );
241
272
242
273
if (!$ list ) {
243
- throw new NotFoundHttpException ('Subscriber list not found. ' );
274
+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
244
275
}
245
276
246
277
/** @var SubscriptionRequest $subscriptionRequest */
@@ -303,7 +334,7 @@ public function deleteSubscriptions(
303
334
): JsonResponse {
304
335
$ this ->requireAuthentication ($ request );
305
336
if (!$ list ) {
306
- throw new NotFoundHttpException ('Subscriber list not found. ' );
337
+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
307
338
}
308
339
$ subscriptionRequest = new SubscriptionRequest ();
309
340
$ subscriptionRequest ->emails = $ request ->query ->all ('emails ' );
0 commit comments