Skip to content

Commit 061342e

Browse files
committed
ISSUE-344: add fields in normalizer
1 parent 8fd213d commit 061342e

File tree

4 files changed

+100
-43
lines changed

4 files changed

+100
-43
lines changed

src/Controller/ListController.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,24 +300,39 @@ public function deleteList(
300300
type: 'array',
301301
items: new OA\Items(
302302
properties: [
303+
new OA\Property(property: 'id', type: 'integer', example: 1),
304+
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
303305
new OA\Property(
304306
property: 'creation_date',
305307
type: 'string',
306308
format: 'date-time',
307-
example: '2016-07-22T15:01:17+00:00'
309+
example: '2023-01-01T12:00:00Z'
308310
),
309-
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
310311
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
311-
new OA\Property(property: 'blacklisted', type: 'boolean', example: true),
312-
new OA\Property(property: 'bounce_count', type: 'integer', example: 17),
312+
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
313+
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
314+
new OA\Property(property: 'unique_id', type: 'string', example: 'abc123'),
315+
new OA\Property(property: 'html_email', type: 'boolean', example: true),
316+
new OA\Property(property: 'disabled', type: 'boolean', example: false),
313317
new OA\Property(
314-
property: 'unique_id',
315-
type: 'string',
316-
example: '95feb7fe7e06e6c11ca8d0c48cb46e89'
318+
property: 'subscribedLists',
319+
type: 'array',
320+
items: new OA\Items(
321+
properties: [
322+
new OA\Property(property: 'id', type: 'integer', example: 2),
323+
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
324+
new OA\Property(property: 'description', type: 'string', example: 'Monthly updates'),
325+
new OA\Property(
326+
property: 'creation_date',
327+
type: 'string',
328+
format: 'date-time',
329+
example: '2022-12-01T10:00:00Z'
330+
),
331+
new OA\Property(property: 'public', type: 'boolean', example: true),
332+
],
333+
type: 'object'
334+
)
317335
),
318-
new OA\Property(property: 'html_email', type: 'boolean', example: true),
319-
new OA\Property(property: 'disabled', type: 'boolean', example: true),
320-
new OA\Property(property: 'id', type: 'integer', example: 1)
321336
],
322337
type: 'object'
323338
)

src/Controller/SubscriberController.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace PhpList\RestBundle\Controller;
66

7-
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
8-
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
97
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
108
use PhpList\Core\Domain\Model\Subscription\Subscriber;
119
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
@@ -30,16 +28,11 @@ class SubscriberController extends AbstractController
3028
use AuthenticationTrait;
3129

3230
private SubscriberRepository $subscriberRepository;
33-
private SubscriberNormalizer $subscriberNormalizer;
3431

35-
public function __construct(
36-
Authentication $authentication,
37-
SubscriberRepository $repository,
38-
SubscriberNormalizer $subscriberNormalizer
39-
) {
32+
public function __construct(Authentication $authentication, SubscriberRepository $repository)
33+
{
4034
$this->authentication = $authentication;
4135
$this->subscriberRepository = $repository;
42-
$this->subscriberNormalizer = $subscriberNormalizer;
4336
}
4437

4538
#[Route('/subscribers', name: 'create_subscriber', methods: ['POST'])]
@@ -189,25 +182,41 @@ public function postAction(Request $request, SerializerInterface $serializer): J
189182
description: 'Success',
190183
content: new OA\JsonContent(
191184
properties: [
185+
new OA\Property(property: 'id', type: 'integer', example: 1),
186+
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
192187
new OA\Property(
193188
property: 'creation_date',
194189
type: 'string',
195190
format: 'date-time',
196-
example: '2017-12-16T18:44:27+00:00'
191+
example: '2023-01-01T12:00:00Z'
197192
),
198-
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
199-
new OA\Property(property: 'confirmed', type: 'boolean', example: false),
193+
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
200194
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
201-
new OA\Property(property: 'bounced', type: 'integer', example: 0),
195+
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
196+
new OA\Property(property: 'unique_id', type: 'string', example: 'abc123'),
197+
new OA\Property(property: 'html_email', type: 'boolean', example: true),
198+
new OA\Property(property: 'disabled', type: 'boolean', example: false),
202199
new OA\Property(
203-
property: 'unique_id',
204-
type: 'string',
205-
example: '69f4e92cf50eafca9627f35704f030f4'
200+
property: 'subscribedLists',
201+
type: 'array',
202+
items: new OA\Items(
203+
properties: [
204+
new OA\Property(property: 'id', type: 'integer', example: 2),
205+
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
206+
new OA\Property(property: 'description', type: 'string', example: 'Monthly updates'),
207+
new OA\Property(
208+
property: 'creation_date',
209+
type: 'string',
210+
format: 'date-time',
211+
example: '2022-12-01T10:00:00Z'
212+
),
213+
new OA\Property(property: 'public', type: 'boolean', example: true),
214+
],
215+
type: 'object'
216+
)
206217
),
207-
new OA\Property(property: 'html_email', type: 'boolean', example: false),
208-
new OA\Property(property: 'disabled', type: 'boolean', example: false),
209-
new OA\Property(property: 'id', type: 'integer', example: 1)
210-
]
218+
],
219+
type: 'object'
211220
)
212221
),
213222
new OA\Response(
@@ -229,21 +238,19 @@ public function postAction(Request $request, SerializerInterface $serializer): J
229238
)
230239
]
231240
)]
232-
public function getAction(
233-
Request $request,
234-
#[MapEntity(mapping: ['subscriberId' => 'id'])] Subscriber $subscriber,
235-
): JsonResponse {
241+
public function getAction(Request $request, int $subscriberId, SerializerInterface $serializer): JsonResponse
242+
{
236243
$this->requireAuthentication($request);
237244

238-
$subscriber = $this->subscriberRepository->findSubscriberWithSubscriptions($subscriber->getId());
245+
$subscriber = $this->subscriberRepository->findSubscriberWithSubscriptions($subscriberId);
239246

240247
if (!$subscriber) {
241248
return new JsonResponse(['error' => 'Subscriber not found'], Response::HTTP_NOT_FOUND);
242249
}
243250

244-
$data = $this->subscriberNormalizer->normalize($subscriber);
251+
$data = $serializer->serialize($subscriber, 'json');
245252

246-
return new JsonResponse($data, Response::HTTP_OK, []);
253+
return new JsonResponse($data, Response::HTTP_OK, [], true);
247254
}
248255

249256
/**

src/Serializer/SubscriberNormalizer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\RestBundle\Serializer;
66

77
use PhpList\Core\Domain\Model\Subscription\Subscriber;
8+
use PhpList\Core\Domain\Model\Subscription\Subscription;
89
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
910

1011
class SubscriberNormalizer implements NormalizerInterface
@@ -18,11 +19,20 @@ public function normalize($object, string $format = null, array $context = []):
1819
return [
1920
'id' => $object->getId(),
2021
'email' => $object->getEmail(),
21-
'subscribedLists' => array_map(function ($subscription) {
22-
22+
'creation_date' => $object->getCreationDate()->format('Y-m-d\TH:i:sP'),
23+
'confirmed' => $object->isConfirmed(),
24+
'blacklisted' => $object->isBlacklisted(),
25+
'bounce_count' => $object->getBounceCount(),
26+
'unique_id' => $object->getUniqueId(),
27+
'html_email' => $object->hasHtmlEmail(),
28+
'disabled' => $object->isDisabled(),
29+
'subscribedLists' => array_map(function (Subscription $subscription) {
2330
return [
2431
'id' => $subscription->getSubscriberList()->getId(),
2532
'name' => $subscription->getSubscriberList()->getName(),
33+
'description' => $subscription->getSubscriberList()->getDescription(),
34+
'creation_date' => $subscription->getSubscriberList()->getCreationDate()->format('Y-m-d\TH:i:sP'),
35+
'public' => $subscription->getSubscriberList()->isPublic(),
2636
];
2737
}, $object->getSubscriptions()->toArray()),
2838
];

tests/Integration/Controller/ListControllerTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,50 @@ public function testGetListMembersWithCurrentSessionKeyForExistingListWithSubscr
238238
$this->assertJsonResponseContentEquals(
239239
[
240240
[
241-
'creation_date' => '2016-07-22T15:01:17+00:00',
241+
'id' => 1,
242242
'email' => '[email protected]',
243+
'creation_date' => '2016-07-22T15:01:17+00:00',
243244
'confirmed' => true,
244245
'blacklisted' => true,
245246
'bounce_count' => 17,
246247
'unique_id' => '95feb7fe7e06e6c11ca8d0c48cb46e89',
247248
'html_email' => true,
248249
'disabled' => true,
249-
'id' => 1,
250+
'subscribedLists' => [
251+
[
252+
'id' => 2,
253+
'name' => 'More news',
254+
'description' => '',
255+
'creation_date' => '2016-06-22T15:01:17+00:00',
256+
'public' => true,
257+
],
258+
],
250259
], [
251-
'creation_date' => '2016-07-22T15:01:17+00:00',
260+
'id' => 2,
252261
'email' => '[email protected]',
262+
'creation_date' => '2016-07-22T15:01:17+00:00',
253263
'confirmed' => true,
254264
'blacklisted' => true,
255265
'bounce_count' => 17,
256266
'unique_id' => '95feb7fe7e06e6c11ca8d0c48cb46e87',
257267
'html_email' => true,
258268
'disabled' => true,
259-
'id' => 2,
269+
'subscribedLists' => [
270+
[
271+
'id' => 2,
272+
'name' => 'More news',
273+
'description' => '',
274+
'creation_date' => '2016-06-22T15:01:17+00:00',
275+
'public' => true,
276+
],
277+
[
278+
'id' => 1,
279+
'name' => 'News',
280+
'description' => 'News (and some fun stuff)',
281+
'creation_date' => '2016-06-22T15:01:17+00:00',
282+
'public' => true,
283+
],
284+
],
260285
],
261286
]
262287
);

0 commit comments

Comments
 (0)