Skip to content

Commit 71e16fd

Browse files
committed
Get/set page data, update/delete page, add tests
1 parent 580285e commit 71e16fd

File tree

6 files changed

+735
-0
lines changed

6 files changed

+735
-0
lines changed

src/Subscription/Controller/SubscribePageController.php

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpList\Core\Security\Authentication;
1212
use PhpList\RestBundle\Common\Controller\BaseController;
1313
use PhpList\RestBundle\Common\Validator\RequestValidator;
14+
use PhpList\RestBundle\Subscription\Request\SubscribePageDataRequest;
1415
use PhpList\RestBundle\Subscription\Request\SubscribePageRequest;
1516
use PhpList\RestBundle\Subscription\Serializer\SubscribePageNormalizer;
1617
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
@@ -143,4 +144,290 @@ public function createPage(Request $request): JsonResponse
143144

144145
return $this->json($this->normalizer->normalize($page), Response::HTTP_CREATED);
145146
}
147+
148+
#[Route('/{id}', name: 'update', requirements: ['id' => '\\d+'], methods: ['PUT'])]
149+
#[OA\Put(
150+
path: '/api/v2/subscribe-pages/{id}',
151+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
152+
summary: 'Update subscribe page',
153+
requestBody: new OA\RequestBody(
154+
required: true,
155+
content: new OA\JsonContent(
156+
properties: [
157+
new OA\Property(property: 'title', type: 'string', nullable: true),
158+
new OA\Property(property: 'active', type: 'boolean', nullable: true),
159+
]
160+
)
161+
),
162+
tags: ['subscriptions'],
163+
parameters: [
164+
new OA\Parameter(
165+
name: 'php-auth-pw',
166+
description: 'Session key obtained from login',
167+
in: 'header',
168+
required: true,
169+
schema: new OA\Schema(type: 'string')
170+
),
171+
new OA\Parameter(
172+
name: 'id',
173+
description: 'Subscribe page ID',
174+
in: 'path',
175+
required: true,
176+
schema: new OA\Schema(type: 'integer')
177+
)
178+
],
179+
responses: [
180+
new OA\Response(
181+
response: 200,
182+
description: 'Success',
183+
content: new OA\JsonContent(ref: '#/components/schemas/SubscribePage')
184+
),
185+
new OA\Response(
186+
response: 403,
187+
description: 'Failure',
188+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
189+
),
190+
new OA\Response(
191+
response: 404,
192+
description: 'Not Found',
193+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
194+
),
195+
]
196+
)]
197+
public function updatePage(
198+
Request $request,
199+
#[MapEntity(mapping: ['id' => 'id'])] ?SubscribePage $page = null
200+
): JsonResponse {
201+
$admin = $this->requireAuthentication($request);
202+
if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) {
203+
throw $this->createAccessDeniedException('You are not allowed to update subscribe pages.');
204+
}
205+
206+
if (!$page) {
207+
throw $this->createNotFoundException('Subscribe page not found');
208+
}
209+
210+
/** @var SubscribePageRequest $updateRequest */
211+
$updateRequest = $this->validator->validate($request, SubscribePageRequest::class);
212+
213+
$updated = $this->subscribePageManager->updatePage(
214+
page: $page,
215+
title: $updateRequest->title,
216+
active: $updateRequest->active,
217+
owner: $admin,
218+
);
219+
220+
return $this->json($this->normalizer->normalize($updated), Response::HTTP_OK);
221+
}
222+
223+
#[Route('/{id}', name: 'delete', requirements: ['id' => '\\d+'], methods: ['DELETE'])]
224+
#[OA\Delete(
225+
path: '/api/v2/subscribe-pages/{id}',
226+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
227+
summary: 'Delete subscribe page',
228+
tags: ['subscriptions'],
229+
parameters: [
230+
new OA\Parameter(
231+
name: 'php-auth-pw',
232+
description: 'Session key obtained from login',
233+
in: 'header',
234+
required: true,
235+
schema: new OA\Schema(type: 'string')
236+
),
237+
new OA\Parameter(
238+
name: 'id',
239+
description: 'Subscribe page ID',
240+
in: 'path',
241+
required: true,
242+
schema: new OA\Schema(type: 'integer')
243+
)
244+
],
245+
responses: [
246+
new OA\Response(response: 204, description: 'No Content'),
247+
new OA\Response(
248+
response: 403,
249+
description: 'Failure',
250+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
251+
),
252+
new OA\Response(
253+
response: 404,
254+
description: 'Not Found',
255+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
256+
)
257+
]
258+
)]
259+
public function deletePage(
260+
Request $request,
261+
#[MapEntity(mapping: ['id' => 'id'])] ?SubscribePage $page = null
262+
): JsonResponse {
263+
$admin = $this->requireAuthentication($request);
264+
if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) {
265+
throw $this->createAccessDeniedException('You are not allowed to delete subscribe pages.');
266+
}
267+
268+
if ($page === null) {
269+
throw $this->createNotFoundException('Subscribe page not found');
270+
}
271+
272+
$this->subscribePageManager->deletePage($page);
273+
274+
return $this->json(null, Response::HTTP_NO_CONTENT);
275+
}
276+
277+
#[Route('/{id}/data', name: 'get_data', requirements: ['id' => '\\d+'], methods: ['GET'])]
278+
#[OA\Get(
279+
path: '/api/v2/subscribe-pages/{id}/data',
280+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
281+
summary: 'Get subscribe page data',
282+
tags: ['subscriptions'],
283+
parameters: [
284+
new OA\Parameter(
285+
name: 'php-auth-pw',
286+
description: 'Session key obtained from login',
287+
in: 'header',
288+
required: true,
289+
schema: new OA\Schema(type: 'string')
290+
),
291+
new OA\Parameter(
292+
name: 'id',
293+
description: 'Subscribe page ID',
294+
in: 'path',
295+
required: true,
296+
schema: new OA\Schema(type: 'integer')
297+
)
298+
],
299+
responses: [
300+
new OA\Response(
301+
response: 200,
302+
description: 'Success',
303+
content: new OA\JsonContent(
304+
type: 'array',
305+
items: new OA\Items(
306+
properties: [
307+
new OA\Property(property: 'id', type: 'integer'),
308+
new OA\Property(property: 'name', type: 'string'),
309+
new OA\Property(property: 'data', type: 'string', nullable: true),
310+
],
311+
type: 'object'
312+
)
313+
)
314+
),
315+
new OA\Response(
316+
response: 403,
317+
description: 'Failure',
318+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
319+
),
320+
new OA\Response(
321+
response: 404,
322+
description: 'Not Found',
323+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
324+
)
325+
]
326+
)]
327+
public function getPageData(
328+
Request $request,
329+
#[MapEntity(mapping: ['id' => 'id'])] ?SubscribePage $page = null
330+
): JsonResponse {
331+
$admin = $this->requireAuthentication($request);
332+
if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) {
333+
throw $this->createAccessDeniedException('You are not allowed to view subscribe page data.');
334+
}
335+
336+
if (!$page) {
337+
throw $this->createNotFoundException('Subscribe page not found');
338+
}
339+
340+
$data = $this->subscribePageManager->getPageData($page);
341+
342+
$json = array_map(static function ($item) {
343+
return [
344+
'id' => $item->getId(),
345+
'name' => $item->getName(),
346+
'data' => $item->getData(),
347+
];
348+
}, $data);
349+
350+
return $this->json($json, Response::HTTP_OK);
351+
}
352+
353+
#[Route('/{id}/data', name: 'set_data', requirements: ['id' => '\\d+'], methods: ['PUT'])]
354+
#[OA\Put(
355+
path: '/api/v2/subscribe-pages/{id}/data',
356+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
357+
summary: 'Set subscribe page data item',
358+
requestBody: new OA\RequestBody(
359+
required: true,
360+
content: new OA\JsonContent(
361+
properties: [
362+
new OA\Property(property: 'name', type: 'string'),
363+
new OA\Property(property: 'value', type: 'string', nullable: true),
364+
]
365+
)
366+
),
367+
tags: ['subscriptions'],
368+
parameters: [
369+
new OA\Parameter(
370+
name: 'php-auth-pw',
371+
description: 'Session key obtained from login',
372+
in: 'header',
373+
required: true,
374+
schema: new OA\Schema(type: 'string')
375+
),
376+
new OA\Parameter(
377+
name: 'id',
378+
description: 'Subscribe page ID',
379+
in: 'path',
380+
required: true,
381+
schema: new OA\Schema(type: 'integer')
382+
)
383+
],
384+
responses: [
385+
new OA\Response(
386+
response: 200,
387+
description: 'Success',
388+
content: new OA\JsonContent(
389+
properties: [
390+
new OA\Property(property: 'id', type: 'integer'),
391+
new OA\Property(property: 'name', type: 'string'),
392+
new OA\Property(property: 'data', type: 'string', nullable: true),
393+
],
394+
type: 'object'
395+
)
396+
),
397+
new OA\Response(
398+
response: 403,
399+
description: 'Failure',
400+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
401+
),
402+
new OA\Response(
403+
response: 404,
404+
description: 'Not Found',
405+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
406+
)
407+
]
408+
)]
409+
public function setPageData(
410+
Request $request,
411+
#[MapEntity(mapping: ['id' => 'id'])] ?SubscribePage $page = null
412+
): JsonResponse {
413+
$admin = $this->requireAuthentication($request);
414+
if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) {
415+
throw $this->createAccessDeniedException('You are not allowed to update subscribe page data.');
416+
}
417+
418+
if (!$page) {
419+
throw $this->createNotFoundException('Subscribe page not found');
420+
}
421+
422+
/** @var SubscribePageDataRequest $createRequest */
423+
$createRequest = $this->validator->validate($request, SubscribePageDataRequest::class);
424+
425+
$item = $this->subscribePageManager->setPageData($page, $createRequest->name, $createRequest->value);
426+
427+
return $this->json([
428+
'id' => $item->getId(),
429+
'name' => $item->getName(),
430+
'data' => $item->getData(),
431+
], Response::HTTP_OK);
432+
}
146433
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Subscription\Request;
6+
7+
use PhpList\RestBundle\Common\Request\RequestInterface;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
class SubscribePageDataRequest implements RequestInterface
11+
{
12+
#[Assert\NotBlank]
13+
public string $name;
14+
15+
public ?string $value = null;
16+
17+
public function getDto(): SubscribePageDataRequest
18+
{
19+
return $this;
20+
}
21+
}

0 commit comments

Comments
 (0)