Skip to content

Commit c15f159

Browse files
committed
Add unsubscribe endpoint
1 parent e7404f4 commit c15f159

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

src/Subscription/Controller/SubscribePagePublicController.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\ORM\EntityManagerInterface;
88
use OpenApi\Attributes as OA;
9+
use PhpList\Core\Domain\Subscription\Model\SubscribePageData;
910
use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository;
1011
use PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager;
1112
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberAttributeManager;
@@ -127,7 +128,7 @@ public function subscribe(Request $request, int $pageId): JsonResponse
127128
{
128129
$page = $this->subscribePageManager->findPublicPage(id: $pageId);
129130
if (!$page) {
130-
throw $this->createNotFoundException('Subscriber subscribe page not found.');
131+
throw $this->createNotFoundException('Subscribe page not found.');
131132
}
132133

133134
/** @var PublicSubscriptionRequest $subscriptionRequest */
@@ -162,4 +163,84 @@ public function subscribe(Request $request, int $pageId): JsonResponse
162163

163164
return $this->json($normalized, Response::HTTP_CREATED);
164165
}
166+
167+
#[Route('/{pageId}', name: 'unsubscribe', methods: ['DELETE'])]
168+
#[OA\Delete(
169+
path: '/api/v2/public/subscribe-pages/{pageId}',
170+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.' .
171+
'Unsubscribe subscriber from a list from subscribe page.',
172+
summary: 'Delete subscription',
173+
tags: ['subscribe-pages'],
174+
parameters: [
175+
new OA\Parameter(
176+
name: 'pageId',
177+
description: 'Subscribe page ID',
178+
in: 'path',
179+
required: true,
180+
schema: new OA\Schema(type: 'integer')
181+
),
182+
new OA\Parameter(
183+
name: 'email',
184+
description: 'Subscriber email',
185+
in: 'query',
186+
required: true,
187+
schema: new OA\Schema(type: 'string')
188+
),
189+
],
190+
responses: [
191+
new OA\Response(
192+
response: 200,
193+
description: 'Success'
194+
),
195+
new OA\Response(
196+
response: 400,
197+
description: 'Failure',
198+
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
199+
),
200+
new OA\Response(
201+
response: 404,
202+
description: 'Failure',
203+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
204+
),
205+
new OA\Response(
206+
response: 422,
207+
description: 'Failure',
208+
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
209+
),
210+
]
211+
)]
212+
public function unsubscribe(Request $request, int $pageId): JsonResponse
213+
{
214+
$page = $this->subscribePageManager->findPage(id: $pageId);
215+
if (!$page) {
216+
throw $this->createNotFoundException('Subscribe page not found.');
217+
}
218+
219+
/** @var SubscribePageData|null $listsField */
220+
$listsField = array_find(
221+
$page->getData(),
222+
fn (SubscribePageData $data) => $data->getName() === 'lists'
223+
);
224+
225+
if ($listsField === null) {
226+
return $this->json(null, Response::HTTP_NO_CONTENT);
227+
}
228+
229+
$listsIds = explode(',', $listsField->getData() ?? '');
230+
231+
if (empty($listsIds)) {
232+
return $this->json(null, Response::HTTP_NO_CONTENT);
233+
}
234+
235+
$lists = $this->subscriberListRepository->findBy(['id' => $listsIds]);
236+
foreach ($lists as $list) {
237+
$this->subscriptionManager->deleteSubscriptions(
238+
subscriberList: $list,
239+
emails: [$request->query->get('email')]
240+
);
241+
}
242+
$this->entityManager->flush();
243+
244+
return $this->json(null, Response::HTTP_NO_CONTENT);
245+
}
165246
}

0 commit comments

Comments
 (0)