66
77use OpenApi \Attributes as OA ;
88use PhpList \Core \Domain \Identity \Model \PrivilegeFlag ;
9+ use PhpList \Core \Domain \Subscription \Model \SubscribePage ;
910use PhpList \Core \Domain \Subscription \Service \Manager \SubscribePageManager ;
1011use PhpList \Core \Security \Authentication ;
1112use PhpList \RestBundle \Common \Controller \BaseController ;
1213use PhpList \RestBundle \Common \Validator \RequestValidator ;
14+ use PhpList \RestBundle \Subscription \Request \SubscribePageRequest ;
1315use PhpList \RestBundle \Subscription \Serializer \SubscribePageNormalizer ;
16+ use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
1417use Symfony \Component \HttpFoundation \JsonResponse ;
1518use Symfony \Component \HttpFoundation \Request ;
1619use Symfony \Component \HttpFoundation \Response ;
@@ -54,14 +57,7 @@ public function __construct(
5457 new OA \Response (
5558 response: 200 ,
5659 description: 'Success ' ,
57- content: new OA \JsonContent (
58- properties: [
59- new OA \Property (property: 'id ' , type: 'integer ' ),
60- new OA \Property (property: 'title ' , type: 'string ' ),
61- new OA \Property (property: 'active ' , type: 'boolean ' ),
62- new OA \Property (property: 'owner_id ' , type: 'integer ' , nullable: true ),
63- ]
64- ),
60+ content: new OA \JsonContent (ref: '#/components/schemas/SubscribePage ' ),
6561 ),
6662 new OA \Response (
6763 response: 403 ,
@@ -75,14 +71,18 @@ public function __construct(
7571 ),
7672 ]
7773 )]
78- public function getPage (Request $ request , int $ id ): JsonResponse
79- {
74+ public function getPage (
75+ Request $ request ,
76+ #[MapEntity(mapping: ['id ' => 'id ' ])] ?SubscribePage $ page = null
77+ ): JsonResponse {
8078 $ admin = $ this ->requireAuthentication ($ request );
8179 if (!$ admin ->getPrivileges ()->has (PrivilegeFlag::Subscribers)) {
8280 throw $ this ->createAccessDeniedException ('You are not allowed to view subscribe pages. ' );
8381 }
8482
85- $ page = $ this ->subscribePageManager ->getPage ($ id );
83+ if (!$ page ) {
84+ throw $ this ->createNotFoundException ('Subscribe page not found ' );
85+ }
8686
8787 return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_OK );
8888 }
@@ -115,14 +115,7 @@ public function getPage(Request $request, int $id): JsonResponse
115115 new OA \Response (
116116 response: 201 ,
117117 description: 'Created ' ,
118- content: new OA \JsonContent (
119- properties: [
120- new OA \Property (property: 'id ' , type: 'integer ' ),
121- new OA \Property (property: 'title ' , type: 'string ' ),
122- new OA \Property (property: 'active ' , type: 'boolean ' ),
123- new OA \Property (property: 'owner_id ' , type: 'integer ' , nullable: true ),
124- ]
125- )
118+ content: new OA \JsonContent (ref: '#/components/schemas/SubscribePage ' )
126119 ),
127120 new OA \Response (
128121 response: 403 ,
@@ -143,19 +136,10 @@ public function createPage(Request $request): JsonResponse
143136 throw $ this ->createAccessDeniedException ('You are not allowed to create subscribe pages. ' );
144137 }
145138
146- $ data = json_decode ($ request ->getContent (), true ) ?: [];
147- $ title = isset ($ data ['title ' ]) ? trim ((string )$ data ['title ' ]) : '' ;
148- $ active = isset ($ data ['active ' ]) ? (bool )$ data ['active ' ] : false ;
149-
150- if ($ title === '' ) {
151- return $ this ->json ([
152- 'errors ' => [
153- ['field ' => 'title ' , 'message ' => 'This field is required. ' ]
154- ]
155- ], Response::HTTP_UNPROCESSABLE_ENTITY );
156- }
139+ /** @var SubscribePageRequest $createRequest */
140+ $ createRequest = $ this ->validator ->validate ($ request , SubscribePageRequest::class);
157141
158- $ page = $ this ->subscribePageManager ->createPage ($ title , $ active , $ admin );
142+ $ page = $ this ->subscribePageManager ->createPage ($ createRequest -> title , $ createRequest -> active , $ admin );
159143
160144 return $ this ->json ($ this ->normalizer ->normalize ($ page ), Response::HTTP_CREATED );
161145 }
0 commit comments