Skip to content

Commit 285910e

Browse files
committed
Add: history to subscriber object
1 parent 953c350 commit 285910e

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

src/Subscription/Controller/SubscriberController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function getSubscriber(Request $request, int $subscriberId): JsonResponse
339339
{
340340
$this->requireAuthentication($request);
341341

342-
$subscriber = $this->subscriberManager->getSubscriberById($subscriberId);
342+
$subscriber = $this->subscriberManager->getSubscriberDetails($subscriberId);
343343
$subscriberData = $this->subscriberNormalizer->normalize($subscriber);
344344

345345
return $this->json($subscriberData, Response::HTTP_OK);

src/Subscription/Serializer/SubscriberNormalizer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use OpenApi\Attributes as OA;
88
use PhpList\Core\Domain\Subscription\Model\Subscriber;
9+
use PhpList\Core\Domain\Subscription\Model\SubscriberHistory;
910
use PhpList\Core\Domain\Subscription\Model\Subscription;
1011
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1112

@@ -20,6 +21,12 @@
2021
format: 'date-time',
2122
example: '2023-01-01T12:00:00Z',
2223
),
24+
new OA\Property(
25+
property: 'updated_at',
26+
type: 'string',
27+
format: 'date-time',
28+
example: '2026-01-01T12:00:00Z',
29+
),
2330
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
2431
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
2532
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
@@ -31,13 +38,20 @@
3138
type: 'array',
3239
items: new OA\Items(ref: '#/components/schemas/SubscriberList')
3340
),
41+
new OA\Property(
42+
property: 'history',
43+
type: 'array',
44+
items: new OA\Items(ref: '#/components/schemas/SubscriberHistory')
45+
),
3446
],
3547
type: 'object'
3648
)]
3749
class SubscriberNormalizer implements NormalizerInterface
3850
{
39-
public function __construct(private readonly SubscriberListNormalizer $subscriberListNormalizer)
40-
{
51+
public function __construct(
52+
private readonly SubscriberListNormalizer $subscriberListNormalizer,
53+
private readonly SubscriberHistoryNormalizer $subscriberHistoryNormalizer,
54+
) {
4155
}
4256

4357
/**
@@ -53,6 +67,7 @@ public function normalize($object, string $format = null, array $context = []):
5367
'id' => $object->getId(),
5468
'email' => $object->getEmail(),
5569
'created_at' => $object->getCreatedAt()->format('Y-m-d\TH:i:sP'),
70+
'updated_at' => $object->getUpdatedAt()->format('Y-m-d\TH:i:sP'),
5671
'confirmed' => $object->isConfirmed(),
5772
'blacklisted' => $object->isBlacklisted(),
5873
'bounce_count' => $object->getBounceCount(),
@@ -62,6 +77,9 @@ public function normalize($object, string $format = null, array $context = []):
6277
'subscribed_lists' => array_map(function (Subscription $subscription) {
6378
return $this->subscriberListNormalizer->normalize($subscription->getSubscriberList());
6479
}, $object->getSubscriptions()->toArray()),
80+
'history' => array_map(function (SubscriberHistory $history) {
81+
return $this->subscriberHistoryNormalizer->normalize($history);
82+
}, $object->getHistory()),
6583
];
6684
}
6785

tests/Unit/Subscription/Serializer/SubscriberNormalizerTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpList\Core\Domain\Subscription\Model\Subscriber;
1010
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
1111
use PhpList\Core\Domain\Subscription\Model\Subscription;
12+
use PhpList\RestBundle\Subscription\Serializer\SubscriberHistoryNormalizer;
1213
use PhpList\RestBundle\Subscription\Serializer\SubscriberListNormalizer;
1314
use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer;
1415
use PHPUnit\Framework\TestCase;
@@ -18,7 +19,7 @@ class SubscriberNormalizerTest extends TestCase
1819
{
1920
public function testSupportsNormalization(): void
2021
{
21-
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
22+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer(), new SubscriberHistoryNormalizer());
2223
$subscriber = $this->createMock(Subscriber::class);
2324

2425
$this->assertTrue($normalizer->supportsNormalization($subscriber));
@@ -50,12 +51,13 @@ public function testNormalize(): void
5051
$subscriber->method('isDisabled')->willReturn(false);
5152
$subscriber->method('getSubscriptions')->willReturn(new ArrayCollection([$subscription]));
5253

53-
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
54+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer(), new SubscriberHistoryNormalizer());
5455

5556
$expected = [
5657
'id' => 101,
5758
'email' => 'test@example.com',
5859
'created_at' => '2024-12-31T12:00:00+00:00',
60+
'updated_at' => '',
5961
'confirmed' => true,
6062
'blacklisted' => false,
6163
'bounce_count' => 0,
@@ -73,15 +75,16 @@ public function testNormalize(): void
7375
'public' => true,
7476
'category' => '',
7577
]
76-
]
78+
],
79+
'history' => [],
7780
];
7881

7982
$this->assertSame($expected, $normalizer->normalize($subscriber));
8083
}
8184

8285
public function testNormalizeWithInvalidObject(): void
8386
{
84-
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer());
87+
$normalizer = new SubscriberNormalizer(new SubscriberListNormalizer(), new SubscriberHistoryNormalizer());
8588
$this->assertSame([], $normalizer->normalize(new stdClass()));
8689
}
8790
}

0 commit comments

Comments
 (0)