Skip to content

Commit 580285e

Browse files
committed
SubscribePageCreateRequest
1 parent a6686e7 commit 580285e

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"require": {
3838
"php": "^8.1",
39-
"phplist/core": "dev-dev",
39+
"phplist/core": "dev-subscribepage",
4040
"friendsofsymfony/rest-bundle": "*",
4141
"symfony/test-pack": "^1.0",
4242
"symfony/process": "^6.4",

src/Subscription/Controller/SubscribePageController.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
use OpenApi\Attributes as OA;
88
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
9+
use PhpList\Core\Domain\Subscription\Model\SubscribePage;
910
use PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager;
1011
use PhpList\Core\Security\Authentication;
1112
use PhpList\RestBundle\Common\Controller\BaseController;
1213
use PhpList\RestBundle\Common\Validator\RequestValidator;
14+
use PhpList\RestBundle\Subscription\Request\SubscribePageRequest;
1315
use PhpList\RestBundle\Subscription\Serializer\SubscribePageNormalizer;
16+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1417
use Symfony\Component\HttpFoundation\JsonResponse;
1518
use Symfony\Component\HttpFoundation\Request;
1619
use 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
}

src/Subscription/OpenApi/SwaggerSchemasResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@
142142
),
143143
],
144144
)]
145+
#[OA\Schema(
146+
schema: 'SubscribePage',
147+
properties: [
148+
new OA\Property(property: 'id', type: 'integer', example: 1),
149+
new OA\Property(property: 'title', type: 'string', example: 'Subscribe to our newsletter'),
150+
new OA\Property(property: 'active', type: 'boolean', example: true),
151+
new OA\Property(property: 'owner', ref: '#/components/schemas/Administrator'),
152+
],
153+
)]
145154
class SwaggerSchemasResponse
146155
{
147156
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 SubscribePageRequest implements RequestInterface
11+
{
12+
#[Assert\NotBlank]
13+
#[Assert\Email]
14+
public string $title;
15+
16+
#[Assert\Type(type: 'bool')]
17+
public ?bool $active = false;
18+
19+
public function getDto(): SubscribePageRequest
20+
{
21+
return $this;
22+
}
23+
}

0 commit comments

Comments
 (0)