Skip to content

Commit f80cba4

Browse files
TatevikGrtatevikg1
andauthored
Mark subscriber as confirmed by UniqueId (#152)
Co-authored-by: Tatevik <[email protected]>
1 parent 7d15750 commit f80cba4

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

src/Subscription/Controller/SubscriberController.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\JsonResponse;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2122
use Symfony\Component\Routing\Attribute\Route;
2223

2324
/**
@@ -281,4 +282,53 @@ public function deleteSubscriber(
281282

282283
return $this->json(null, Response::HTTP_NO_CONTENT);
283284
}
285+
286+
#[Route('/confirm', name: 'confirm', methods: ['GET'])]
287+
#[OA\Get(
288+
path: '/api/v2/subscribers/confirm',
289+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
290+
summary: 'Confirm a subscriber by uniqueId.',
291+
requestBody: new OA\RequestBody(
292+
required: true,
293+
content: new OA\JsonContent(
294+
properties: [
295+
new OA\Property(property: 'uniqueId', type: 'string', example: 'e9d8c9b2e6')
296+
]
297+
)
298+
),
299+
tags: ['subscribers'],
300+
responses: [
301+
new OA\Response(
302+
response: 200,
303+
description: 'Subscriber confirmed',
304+
content: new OA\MediaType(
305+
mediaType: 'text/html'
306+
)
307+
),
308+
new OA\Response(
309+
response: 400,
310+
description: 'Missing or invalid uniqueId'
311+
),
312+
new OA\Response(
313+
response: 404,
314+
description: 'Subscriber not found'
315+
)
316+
]
317+
)]
318+
public function setSubscriberAsConfirmed(Request $request): Response
319+
{
320+
$uniqueId = $request->query->get('uniqueId');
321+
322+
if (!$uniqueId) {
323+
return new Response('<h1>Missing confirmation code.</h1>', 400);
324+
}
325+
326+
try {
327+
$this->subscriberManager->markAsConfirmedByUniqueId($uniqueId);
328+
} catch (NotFoundHttpException) {
329+
return new Response('<h1>Subscriber isn\'t found or already confirmed.</h1>', 404);
330+
}
331+
332+
return new Response('<h1>Thank you, your subscription is confirmed!</h1>');
333+
}
284334
}

src/Subscription/Controller/SubscriberImportController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(
5555
description: 'List id to add imported subscribers to',
5656
type: 'string',
5757
default: null,
58-
pattern: "^\\d+$"
58+
pattern: '^\\d+$'
5959
),
6060
new OA\Property(
6161
property: 'update_existing',

tests/Integration/Subscription/Controller/SubscriberControllerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpList\RestBundle\Subscription\Controller\SubscriberController;
1010
use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController;
1111
use PhpList\RestBundle\Tests\Integration\Subscription\Fixtures\SubscriberFixture;
12+
use Symfony\Component\HttpFoundation\Response;
1213

1314
/**
1415
* Testcase.
@@ -165,4 +166,44 @@ public function testPostSubscribersWithValidSessionKeyAssignsProvidedSubscriberD
165166
static::assertTrue($responseContent['html_email']);
166167
static::assertFalse($responseContent['disabled']);
167168
}
169+
public function testSetSubscriberAsConfirmedWithMissingUniqueIdReturnsBadRequestStatus()
170+
{
171+
self::getClient()->request('get', '/api/v2/subscribers/confirm');
172+
173+
$response = self::getClient()->getResponse();
174+
self::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
175+
self::assertStringContainsString('Missing confirmation code', $response->getContent());
176+
}
177+
178+
public function testSetSubscriberAsConfirmedWithInvalidUniqueIdReturnsNotFoundStatus()
179+
{
180+
self::getClient()->request('get', '/api/v2/subscribers/confirm', ['uniqueId' => 'invalid-unique-id']);
181+
182+
$response = self::getClient()->getResponse();
183+
self::assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
184+
self::assertStringContainsString('Subscriber isn\'t found', $response->getContent());
185+
}
186+
187+
public function testSetSubscriberAsConfirmedWithValidUniqueIdConfirmsSubscriber()
188+
{
189+
$email = '[email protected]';
190+
$jsonData = ['email' => $email, 'requestConfirmation' => true];
191+
192+
$this->authenticatedJsonRequest('post', '/api/v2/subscribers', [], [], [], json_encode($jsonData));
193+
$responseContent = $this->getDecodedJsonResponseContent();
194+
$uniqueId = $responseContent['unique_id'];
195+
196+
$subscriberId = $responseContent['id'];
197+
$subscriber = $this->subscriberRepository->find($subscriberId);
198+
self::assertFalse($subscriber->isConfirmed());
199+
200+
self::getClient()->request('get', '/api/v2/subscribers/confirm', ['uniqueId' => $uniqueId]);
201+
202+
$response = self::getClient()->getResponse();
203+
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
204+
self::assertStringContainsString('Thank you, your subscription is confirmed', $response->getContent());
205+
206+
$subscriber = $this->subscriberRepository->findOneByUniqueId($uniqueId);
207+
self::assertTrue($subscriber->isConfirmed());
208+
}
168209
}

0 commit comments

Comments
 (0)