10
10
use PhpList \Core \Security \Authentication ;
11
11
use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
12
12
use PhpList \RestBundle \Entity \CreateSubscriberRequest ;
13
+ use PhpList \RestBundle \Entity \UpdateSubscriberRequest ;
13
14
use PhpList \RestBundle \Serializer \SubscriberNormalizer ;
14
15
use PhpList \RestBundle \Service \Manager \SubscriberManager ;
15
16
use PhpList \RestBundle \Validator \RequestValidator ;
17
+ use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
16
18
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
17
19
use Symfony \Component \HttpFoundation \JsonResponse ;
18
20
use Symfony \Component \HttpFoundation \Request ;
24
26
* This controller provides REST API access to subscribers.
25
27
*
26
28
* @author Oliver Klee <[email protected] >
29
+ * @author Tatevik Grigoryan <[email protected] >
27
30
*/
31
+ #[Route('/subscribers ' )]
28
32
class SubscriberController extends AbstractController
29
33
{
30
34
use AuthenticationTrait;
@@ -42,7 +46,7 @@ public function __construct(
42
46
$ this ->subscriberManager = $ subscriberManager ;
43
47
}
44
48
45
- #[Route('/subscribers ' , name: 'create_subscriber ' , methods: ['POST ' ])]
49
+ #[Route('' , name: 'create_subscriber ' , methods: ['POST ' ])]
46
50
#[OA \Post(
47
51
path: '/subscribers ' ,
48
52
description: 'Creates a new subscriber (if there is no subscriber with the given email address yet). ' ,
@@ -112,7 +116,7 @@ public function __construct(
112
116
)
113
117
]
114
118
)]
115
- public function postAction (
119
+ public function createSubscriber (
116
120
Request $ request ,
117
121
SerializerInterface $ serializer ,
118
122
RequestValidator $ validator
@@ -131,10 +135,96 @@ public function postAction(
131
135
);
132
136
}
133
137
134
- #[Route('/subscribers/ {subscriberId} ' , name: 'get_subscriber_by_id ' , methods: ['GET ' ])]
138
+ #[Route('/{subscriberId} ' , name: 'update_subscriber ' , requirements: [ ' subscriberId ' => ' \d+ ' ], methods: ['PUT ' ])]
135
139
#[OA \Get(
136
140
path: '/subscribers/{subscriberId} ' ,
137
- description: 'Get subscriber date by id. ' ,
141
+ description: 'Update subscriber data by id. ' ,
142
+ summary: 'Update subscriber ' ,
143
+ requestBody: new OA \RequestBody (
144
+ description: 'Pass session credentials ' ,
145
+ required: true ,
146
+ content: new OA \JsonContent (
147
+ required: ['email ' ],
148
+ properties: [
149
+ new OA \
Property (property:
'email ' , type:
'string ' , format:
'string ' , example:
'[email protected] ' ),
150
+ new OA \Property (property: 'html_email ' , type: 'boolean ' , example: false ),
151
+ new OA \Property (property: 'confirmed ' , type: 'boolean ' , example: false ),
152
+ new OA \Property (property: 'blacklisted ' , type: 'boolean ' , example: false ),
153
+ new OA \Property (property: 'html_email ' , type: 'boolean ' , example: false ),
154
+ new OA \Property (property: 'disabled ' , type: 'boolean ' , example: false ),
155
+ new OA \Property (property: 'additional_data ' , type: 'string ' , example: 'asdf ' ),
156
+ ]
157
+ )
158
+ ),
159
+ tags: ['subscribers ' ],
160
+ parameters: [
161
+ new OA \Parameter (
162
+ name: 'session ' ,
163
+ description: 'Session ID obtained from authentication ' ,
164
+ in: 'header ' ,
165
+ required: true ,
166
+ schema: new OA \Schema (type: 'string ' )
167
+ ),
168
+ new OA \Parameter (
169
+ name: 'subscriberId ' ,
170
+ description: 'Subscriber ID ' ,
171
+ in: 'path ' ,
172
+ required: true ,
173
+ schema: new OA \Schema (type: 'string ' )
174
+ )
175
+ ],
176
+ responses: [
177
+ new OA \Response (
178
+ response: 200 ,
179
+ description: 'Success ' ,
180
+ content: new OA \JsonContent (ref: '#/components/schemas/Subscriber ' ),
181
+ ),
182
+ new OA \Response (
183
+ response: 403 ,
184
+ description: 'Failure ' ,
185
+ content: new OA \JsonContent (
186
+ properties: [
187
+ new OA \Property (
188
+ property: 'message ' ,
189
+ type: 'string ' ,
190
+ example: 'No valid session key was provided as basic auth password. '
191
+ )
192
+ ]
193
+ )
194
+ ),
195
+ new OA \Response (
196
+ response: 404 ,
197
+ description: 'Not Found ' ,
198
+ )
199
+ ]
200
+ )]
201
+ public function update (
202
+ Request $ request ,
203
+ #[MapEntity(mapping: ['subscriberId ' => 'id ' ])] Subscriber $ subscriber ,
204
+ SerializerInterface $ serializer ,
205
+ RequestValidator $ validator ,
206
+ SubscriberNormalizer $ subscriberNormalizer ,
207
+ ): JsonResponse {
208
+ $ this ->requireAuthentication ($ request );
209
+
210
+ /** @var UpdateSubscriberRequest $dto */
211
+ $ dto = $ serializer ->deserialize ($ request ->getContent (), UpdateSubscriberRequest::class, 'json ' );
212
+ $ dto ->subscriberId = $ subscriber ->getId ();
213
+ $ validator ->validateDto ($ dto );
214
+ $ subscriber = $ this ->subscriberManager ->updateSubscriber ($ dto );
215
+
216
+ return new JsonResponse (
217
+ $ subscriberNormalizer ->normalize ($ subscriber , 'json ' ),
218
+ Response::HTTP_OK ,
219
+ [],
220
+ false
221
+ );
222
+ }
223
+
224
+ #[Route('/{subscriberId} ' , name: 'get_subscriber_by_id ' , methods: ['GET ' ])]
225
+ #[OA \Get(
226
+ path: '/subscribers/{subscriberId} ' ,
227
+ description: 'Get subscriber data by id. ' ,
138
228
summary: 'Get a subscriber ' ,
139
229
tags: ['subscribers ' ],
140
230
parameters: [
@@ -182,10 +272,7 @@ public function getSubscriber(Request $request, int $subscriberId, SubscriberNor
182
272
{
183
273
$ this ->requireAuthentication ($ request );
184
274
185
- $ subscriber = $ this ->subscriberRepository ->findSubscriberWithSubscriptions ($ subscriberId );
186
- if (!$ subscriber ) {
187
- return new JsonResponse (['error ' => 'Subscriber not found ' ], Response::HTTP_NOT_FOUND );
188
- }
275
+ $ subscriber = $ this ->subscriberManager ->getSubscriber ($ subscriberId );
189
276
190
277
return new JsonResponse (
191
278
$ serializer ->normalize ($ subscriber , 'json ' ),
0 commit comments