55namespace PhpList \RestBundle \Controller ;
66
77use OpenApi \Attributes as OA ;
8+ use PhpList \Core \Domain \Filter \SubscriberFilter ;
9+ use PhpList \Core \Domain \Model \Subscription \Subscriber ;
810use PhpList \Core \Domain \Model \Subscription \SubscriberList ;
911use PhpList \Core \Security \Authentication ;
1012use PhpList \RestBundle \Entity \Request \SubscriptionRequest ;
1113use PhpList \RestBundle \Serializer \SubscriberNormalizer ;
1214use PhpList \RestBundle \Serializer \SubscriptionNormalizer ;
1315use PhpList \RestBundle \Service \Manager \SubscriptionManager ;
16+ use PhpList \RestBundle \Service \Provider \PaginatedDataProvider ;
1417use PhpList \RestBundle \Validator \RequestValidator ;
1518use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
1619use Symfony \Component \HttpFoundation \JsonResponse ;
1720use Symfony \Component \HttpFoundation \Request ;
1821use Symfony \Component \HttpFoundation \Response ;
19- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2022use Symfony \Component \Routing \Attribute \Route ;
2123
2224/**
@@ -30,18 +32,21 @@ class SubscriptionController extends BaseController
3032 private SubscriptionManager $ subscriptionManager ;
3133 private SubscriberNormalizer $ subscriberNormalizer ;
3234 private SubscriptionNormalizer $ subscriptionNormalizer ;
35+ private PaginatedDataProvider $ paginatedProvider ;
3336
3437 public function __construct (
3538 Authentication $ authentication ,
3639 RequestValidator $ validator ,
3740 SubscriptionManager $ subscriptionManager ,
3841 SubscriberNormalizer $ subscriberNormalizer ,
3942 SubscriptionNormalizer $ subscriptionNormalizer ,
43+ PaginatedDataProvider $ paginatedProvider ,
4044 ) {
4145 parent ::__construct ($ authentication , $ validator );
4246 $ this ->subscriptionManager = $ subscriptionManager ;
4347 $ this ->subscriberNormalizer = $ subscriberNormalizer ;
4448 $ this ->subscriptionNormalizer = $ subscriptionNormalizer ;
49+ $ this ->paginatedProvider = $ paginatedProvider ;
4550 }
4651
4752 #[Route('/{listId}/subscribers ' , name: 'get_subscriber_from_list ' , methods: ['GET ' ])]
@@ -64,15 +69,36 @@ public function __construct(
6469 in: 'path ' ,
6570 required: true ,
6671 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 )
6786 )
6887 ],
6988 responses: [
7089 new OA \Response (
7190 response: 200 ,
7291 description: 'Success ' ,
7392 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 '
76102 )
77103 ),
78104 new OA \Response (
@@ -94,13 +120,18 @@ public function getListMembers(
94120 $ this ->requireAuthentication ($ request );
95121
96122 if (!$ list ) {
97- throw new NotFoundHttpException ('Subscriber list not found. ' );
123+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
98124 }
99125
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+ );
104135 }
105136
106137 #[Route('/{listId}/subscribers/count ' , name: 'get_subscribers_count_from_list ' , methods: ['GET ' ])]
@@ -154,7 +185,7 @@ public function getSubscribersCount(
154185 $ this ->requireAuthentication ($ request );
155186
156187 if (!$ list ) {
157- throw new NotFoundHttpException ('Subscriber list not found. ' );
188+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
158189 }
159190
160191 return new JsonResponse (['subscribers_count ' => count ($ list ->getSubscribers ())], Response::HTTP_OK );
@@ -240,7 +271,7 @@ public function createSubscription(
240271 $ this ->requireAuthentication ($ request );
241272
242273 if (!$ list ) {
243- throw new NotFoundHttpException ('Subscriber list not found. ' );
274+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
244275 }
245276
246277 /** @var SubscriptionRequest $subscriptionRequest */
@@ -303,7 +334,7 @@ public function deleteSubscriptions(
303334 ): JsonResponse {
304335 $ this ->requireAuthentication ($ request );
305336 if (!$ list ) {
306- throw new NotFoundHttpException ('Subscriber list not found. ' );
337+ throw $ this -> createNotFoundException ('Subscriber list not found. ' );
307338 }
308339 $ subscriptionRequest = new SubscriptionRequest ();
309340 $ subscriptionRequest ->emails = $ request ->query ->all ('emails ' );
0 commit comments