|
6 | 6 |
|
7 | 7 | use Doctrine\ORM\EntityManagerInterface; |
8 | 8 | use OpenApi\Attributes as OA; |
| 9 | +use PhpList\Core\Domain\Subscription\Model\SubscribePageData; |
9 | 10 | use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository; |
10 | 11 | use PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager; |
11 | 12 | use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberAttributeManager; |
@@ -127,7 +128,7 @@ public function subscribe(Request $request, int $pageId): JsonResponse |
127 | 128 | { |
128 | 129 | $page = $this->subscribePageManager->findPublicPage(id: $pageId); |
129 | 130 | if (!$page) { |
130 | | - throw $this->createNotFoundException('Subscriber subscribe page not found.'); |
| 131 | + throw $this->createNotFoundException('Subscribe page not found.'); |
131 | 132 | } |
132 | 133 |
|
133 | 134 | /** @var PublicSubscriptionRequest $subscriptionRequest */ |
@@ -162,4 +163,84 @@ public function subscribe(Request $request, int $pageId): JsonResponse |
162 | 163 |
|
163 | 164 | return $this->json($normalized, Response::HTTP_CREATED); |
164 | 165 | } |
| 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 | + } |
165 | 246 | } |
0 commit comments