55namespace PhpList \RestBundle \Controller ;
66
77use PhpList \Core \Domain \Model \Subscription \SubscriberList ;
8- use PhpList \Core \Domain \Repository \Subscription \SubscriberRepository ;
98use PhpList \RestBundle \Entity \CreateSubscriberListRequest ;
109use PhpList \RestBundle \Serializer \SubscriberListNormalizer ;
10+ use PhpList \RestBundle \Serializer \SubscriberNormalizer ;
1111use PhpList \RestBundle \Service \Manager \SubscriberListManager ;
1212use PhpList \RestBundle \Validator \RequestValidator ;
1313use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
1414use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
15- use PhpList \Core \Domain \Repository \Subscription \SubscriberListRepository ;
1615use PhpList \Core \Security \Authentication ;
1716use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
1817use Symfony \Component \HttpFoundation \JsonResponse ;
1918use Symfony \Component \HttpFoundation \Request ;
2019use Symfony \Component \HttpFoundation \Response ;
2120use Symfony \Component \Routing \Attribute \Route ;
22- use Symfony \Component \Serializer \Normalizer \AbstractNormalizer ;
23- use Symfony \Component \Serializer \SerializerInterface ;
2421use OpenApi \Attributes as OA ;
2522
2623/**
@@ -35,24 +32,18 @@ class ListController extends AbstractController
3532{
3633 use AuthenticationTrait;
3734
38- private SubscriberListRepository $ subscriberListRepository ;
39- private SubscriberRepository $ subscriberRepository ;
40- private SerializerInterface $ serializer ;
35+ private SubscriberListNormalizer $ normalizer ;
4136 private SubscriberListManager $ subscriberListManager ;
4237 private RequestValidator $ validator ;
4338
4439 public function __construct (
4540 Authentication $ authentication ,
46- SubscriberListRepository $ repository ,
47- SubscriberRepository $ subscriberRepository ,
48- SerializerInterface $ serializer ,
41+ SubscriberListNormalizer $ normalizer ,
4942 RequestValidator $ validator ,
5043 SubscriberListManager $ subscriberListManager
5144 ) {
5245 $ this ->authentication = $ authentication ;
53- $ this ->subscriberListRepository = $ repository ;
54- $ this ->subscriberRepository = $ subscriberRepository ;
55- $ this ->serializer = $ serializer ;
46+ $ this ->normalizer = $ normalizer ;
5647 $ this ->validator = $ validator ;
5748 $ this ->subscriberListManager = $ subscriberListManager ;
5849 }
@@ -114,12 +105,13 @@ public function __construct(
114105 public function getLists (Request $ request ): JsonResponse
115106 {
116107 $ this ->requireAuthentication ($ request );
117- $ data = $ this ->subscriberListRepository ->findAll ();
118- $ json = $ this ->serializer ->serialize ($ data , 'json ' , [
119- AbstractNormalizer::GROUPS => 'SubscriberList ' ,
120- ]);
108+ $ data = $ this ->subscriberListManager ->getAll ();
121109
122- return new JsonResponse ($ json , Response::HTTP_OK , [], true );
110+ $ normalized = array_map (function ($ item ) {
111+ return $ this ->normalizer ->normalize ($ item );
112+ }, $ data );
113+
114+ return new JsonResponse ($ normalized , Response::HTTP_OK );
123115 }
124116
125117 #[Route('/{listId} ' , name: 'get_list ' , methods: ['GET ' ])]
@@ -188,11 +180,8 @@ public function getList(
188180 #[MapEntity(mapping: ['listId ' => 'id ' ])] SubscriberList $ list
189181 ): JsonResponse {
190182 $ this ->requireAuthentication ($ request );
191- $ json = $ this ->serializer ->serialize ($ list , 'json ' , [
192- AbstractNormalizer::GROUPS => 'SubscriberList ' ,
193- ]);
194183
195- return new JsonResponse ($ json , Response::HTTP_OK , [], true );
184+ return new JsonResponse ($ this -> normalizer -> normalize ( $ list ) , Response::HTTP_OK );
196185 }
197186
198187 #[Route('/{listId} ' , name: 'delete_list ' , methods: ['DELETE ' ])]
@@ -249,9 +238,9 @@ public function deleteList(
249238 ): JsonResponse {
250239 $ this ->requireAuthentication ($ request );
251240
252- $ this ->subscriberListRepository -> remove ($ list );
241+ $ this ->subscriberListManager -> delete ($ list );
253242
254- return new JsonResponse (null , Response::HTTP_NO_CONTENT , [], false );
243+ return new JsonResponse (null , Response::HTTP_NO_CONTENT );
255244 }
256245
257246 #[Route('/{listId}/subscribers ' , name: 'get_subscriber_from_list ' , methods: ['GET ' ])]
@@ -294,17 +283,17 @@ public function deleteList(
294283 )]
295284 public function getListMembers (
296285 Request $ request ,
297- #[MapEntity(mapping: ['listId ' => 'id ' ])] SubscriberList $ list
286+ #[MapEntity(mapping: ['listId ' => 'id ' ])] SubscriberList $ list ,
287+ SubscriberNormalizer $ normalizer
298288 ): JsonResponse {
299289 $ this ->requireAuthentication ($ request );
300290
301- $ subscribers = $ this ->subscriberRepository ->getSubscribersBySubscribedListId ($ list ->getId ());
302-
303- $ json = $ this ->serializer ->serialize ($ subscribers , 'json ' , [
304- AbstractNormalizer::GROUPS => 'SubscriberListMembers ' ,
305- ]);
291+ $ subscribers = $ this ->subscriberListManager ->getSubscriberListMembers ($ list );
292+ $ normalized = array_map (function ($ item ) use ($ normalizer ) {
293+ return $ normalizer ->normalize ($ item );
294+ }, $ subscribers );
306295
307- return new JsonResponse ($ json , Response::HTTP_OK , [], true );
296+ return new JsonResponse ($ normalized , Response::HTTP_OK );
308297 }
309298
310299 #[Route('/{listId}/subscribers/count ' , name: 'get_subscribers_count_from_list ' , methods: ['GET ' ])]
@@ -332,7 +321,17 @@ public function getListMembers(
332321 responses: [
333322 new OA \Response (
334323 response: 200 ,
335- description: 'Success '
324+ description: 'Success ' ,
325+ content: new OA \JsonContent (
326+ properties: [
327+ new OA \Property (
328+ property: 'subscribers_count ' ,
329+ type: 'integer ' ,
330+ example: 42
331+ )
332+ ],
333+ type: 'object '
334+ )
336335 ),
337336 new OA \Response (
338337 response: 403 ,
@@ -346,9 +345,11 @@ public function getSubscribersCount(
346345 #[MapEntity(mapping: ['listId ' => 'id ' ])] SubscriberList $ list
347346 ): JsonResponse {
348347 $ this ->requireAuthentication ($ request );
349- $ json = $ this ->serializer ->serialize (count ($ list ->getSubscribers ()), 'json ' );
350348
351- return new JsonResponse ($ json , Response::HTTP_OK , [], true );
349+ return new JsonResponse (
350+ ['subscribers_count ' => count ($ list ->getSubscribers ())],
351+ Response::HTTP_OK ,
352+ );
352353 }
353354
354355 #[Route('' , name: 'create_list ' , methods: ['POST ' ])]
0 commit comments