44
55namespace PhpList \RestBundle \Controller ;
66
7+ use PhpList \RestBundle \Serializer \SubscriberNormalizer ;
78use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
89use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
910use PhpList \Core \Domain \Model \Subscription \Subscriber ;
1617use Symfony \Component \HttpKernel \Exception \ConflictHttpException ;
1718use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
1819use Symfony \Component \Routing \Attribute \Route ;
19- use Symfony \Component \Serializer \Normalizer \AbstractNormalizer ;
2020use Symfony \Component \Serializer \SerializerInterface ;
2121use OpenApi \Attributes as OA ;
2222
@@ -30,13 +30,16 @@ class SubscriberController extends AbstractController
3030 use AuthenticationTrait;
3131
3232 private SubscriberRepository $ subscriberRepository ;
33+ private SubscriberNormalizer $ subscriberNormalizer ;
3334
3435 public function __construct (
3536 Authentication $ authentication ,
36- SubscriberRepository $ repository
37+ SubscriberRepository $ repository ,
38+ SubscriberNormalizer $ subscriberNormalizer
3739 ) {
3840 $ this ->authentication = $ authentication ;
3941 $ this ->subscriberRepository = $ repository ;
42+ $ this ->subscriberNormalizer = $ subscriberNormalizer ;
4043 }
4144
4245 #[Route('/subscribers ' , name: 'create_subscriber ' , methods: ['POST ' ])]
@@ -229,15 +232,18 @@ public function postAction(Request $request, SerializerInterface $serializer): J
229232 public function getAction (
230233 Request $ request ,
231234 #[MapEntity(mapping: ['subscriberId ' => 'id ' ])] Subscriber $ subscriber ,
232- SerializerInterface $ serializer
233235 ): JsonResponse {
234236 $ this ->requireAuthentication ($ request );
235237
236- $ json = $ serializer ->serialize ($ subscriber , 'json ' , [
237- AbstractNormalizer::GROUPS => 'SubscriberListMembers ' ,
238- ]);
238+ $ subscriber = $ this ->subscriberRepository ->findSubscriberWithSubscriptions ($ subscriber ->getId ());
239239
240- return new JsonResponse ($ json , Response::HTTP_OK , [], true );
240+ if (!$ subscriber ) {
241+ return new JsonResponse (['error ' => 'Subscriber not found ' ], Response::HTTP_NOT_FOUND );
242+ }
243+
244+ $ data = $ this ->subscriberNormalizer ->normalize ($ subscriber );
245+
246+ return new JsonResponse ($ data , Response::HTTP_OK , []);
241247 }
242248
243249 /**
0 commit comments