|
7 | 7 | use OpenApi\Attributes as OA;
|
8 | 8 | use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
|
9 | 9 | use PhpList\Core\Domain\Subscription\Model\Subscriber;
|
10 |
| -use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager; |
11 | 10 | use PhpList\Core\Security\Authentication;
|
12 | 11 | use PhpList\RestBundle\Common\Controller\BaseController;
|
13 | 12 | use PhpList\RestBundle\Common\Validator\RequestValidator;
|
14 | 13 | use PhpList\RestBundle\Subscription\Request\CreateSubscriberRequest;
|
15 | 14 | use PhpList\RestBundle\Subscription\Request\UpdateSubscriberRequest;
|
16 |
| -use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer; |
| 15 | +use PhpList\RestBundle\Subscription\Service\SubscriberService; |
17 | 16 | use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
18 | 17 | use Symfony\Component\HttpFoundation\JsonResponse;
|
19 | 18 | use Symfony\Component\HttpFoundation\Request;
|
20 | 19 | use Symfony\Component\HttpFoundation\Response;
|
21 |
| -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
22 | 20 | use Symfony\Component\Routing\Attribute\Route;
|
23 | 21 |
|
24 | 22 | /**
|
|
30 | 28 | #[Route('/subscribers', name: 'subscriber_')]
|
31 | 29 | class SubscriberController extends BaseController
|
32 | 30 | {
|
33 |
| - private SubscriberManager $subscriberManager; |
34 |
| - private SubscriberNormalizer $subscriberNormalizer; |
| 31 | + private SubscriberService $subscriberService; |
35 | 32 |
|
36 | 33 | public function __construct(
|
37 | 34 | Authentication $authentication,
|
38 | 35 | RequestValidator $validator,
|
39 |
| - SubscriberManager $subscriberManager, |
40 |
| - SubscriberNormalizer $subscriberNormalizer, |
| 36 | + SubscriberService $subscriberService, |
41 | 37 | ) {
|
42 | 38 | parent::__construct($authentication, $validator);
|
43 | 39 | $this->authentication = $authentication;
|
44 |
| - $this->subscriberManager = $subscriberManager; |
45 |
| - $this->subscriberNormalizer = $subscriberNormalizer; |
| 40 | + $this->subscriberService = $subscriberService; |
46 | 41 | }
|
47 | 42 |
|
48 | 43 | #[Route('', name: 'create', methods: ['POST'])]
|
@@ -98,12 +93,9 @@ public function createSubscriber(Request $request): JsonResponse
|
98 | 93 |
|
99 | 94 | /** @var CreateSubscriberRequest $subscriberRequest */
|
100 | 95 | $subscriberRequest = $this->validator->validate($request, CreateSubscriberRequest::class);
|
101 |
| - $subscriber = $this->subscriberManager->createSubscriber($subscriberRequest->getDto()); |
| 96 | + $subscriberData = $this->subscriberService->createSubscriber($subscriberRequest); |
102 | 97 |
|
103 |
| - return $this->json( |
104 |
| - $this->subscriberNormalizer->normalize($subscriber, 'json'), |
105 |
| - Response::HTTP_CREATED |
106 |
| - ); |
| 98 | + return $this->json($subscriberData, Response::HTTP_CREATED); |
107 | 99 | }
|
108 | 100 |
|
109 | 101 | #[Route('/{subscriberId}', name: 'update', requirements: ['subscriberId' => '\d+'], methods: ['PUT'])]
|
@@ -171,9 +163,9 @@ public function updateSubscriber(
|
171 | 163 | }
|
172 | 164 | /** @var UpdateSubscriberRequest $updateSubscriberRequest */
|
173 | 165 | $updateSubscriberRequest = $this->validator->validate($request, UpdateSubscriberRequest::class);
|
174 |
| - $subscriber = $this->subscriberManager->updateSubscriber($updateSubscriberRequest->getDto()); |
| 166 | + $subscriberData = $this->subscriberService->updateSubscriber($updateSubscriberRequest); |
175 | 167 |
|
176 |
| - return $this->json($this->subscriberNormalizer->normalize($subscriber, 'json'), Response::HTTP_OK); |
| 168 | + return $this->json($subscriberData, Response::HTTP_OK); |
177 | 169 | }
|
178 | 170 |
|
179 | 171 | #[Route('/{subscriberId}', name: 'get_one', requirements: ['subscriberId' => '\d+'], methods: ['GET'])]
|
@@ -221,11 +213,111 @@ public function getSubscriber(Request $request, int $subscriberId): JsonResponse
|
221 | 213 | {
|
222 | 214 | $this->requireAuthentication($request);
|
223 | 215 |
|
224 |
| - $subscriber = $this->subscriberManager->getSubscriber($subscriberId); |
| 216 | + $subscriberData = $this->subscriberService->getSubscriber($subscriberId); |
225 | 217 |
|
226 |
| - return $this->json($this->subscriberNormalizer->normalize($subscriber), Response::HTTP_OK); |
| 218 | + return $this->json($subscriberData, Response::HTTP_OK); |
227 | 219 | }
|
228 | 220 |
|
| 221 | + #[Route('/{subscriberId}/history', name: 'history', requirements: ['subscriberId' => '\d+'], methods: ['GET'])] |
| 222 | + #[OA\Get( |
| 223 | + path: '/api/v2/subscribers/{subscriberId}/history', |
| 224 | + description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ', |
| 225 | + summary: 'Get subscriber event history', |
| 226 | + tags: ['subscribers'], |
| 227 | + parameters: [ |
| 228 | + new OA\Parameter( |
| 229 | + name: 'php-auth-pw', |
| 230 | + description: 'Session key obtained from login', |
| 231 | + in: 'header', |
| 232 | + required: true, |
| 233 | + schema: new OA\Schema(type: 'string') |
| 234 | + ), |
| 235 | + new OA\Parameter( |
| 236 | + name: 'subscriberId', |
| 237 | + description: 'Subscriber ID', |
| 238 | + in: 'path', |
| 239 | + required: true, |
| 240 | + schema: new OA\Schema(type: 'integer') |
| 241 | + ), |
| 242 | + new OA\Parameter( |
| 243 | + name: 'after_id', |
| 244 | + description: 'Page number (pagination)', |
| 245 | + in: 'query', |
| 246 | + required: false, |
| 247 | + schema: new OA\Schema(type: 'integer', default: 1) |
| 248 | + ), |
| 249 | + new OA\Parameter( |
| 250 | + name: 'limit', |
| 251 | + description: 'Max items per page', |
| 252 | + in: 'query', |
| 253 | + required: false, |
| 254 | + schema: new OA\Schema(type: 'integer', default: 25) |
| 255 | + ), |
| 256 | + new OA\Parameter( |
| 257 | + name: 'ip', |
| 258 | + description: 'Filter by IP address', |
| 259 | + in: 'query', |
| 260 | + required: false, |
| 261 | + schema: new OA\Schema(type: 'string') |
| 262 | + ), |
| 263 | + new OA\Parameter( |
| 264 | + name: 'date_from', |
| 265 | + description: 'Filter by date (format: Y-m-d)', |
| 266 | + in: 'query', |
| 267 | + required: false, |
| 268 | + schema: new OA\Schema(type: 'string', format: 'date') |
| 269 | + ), |
| 270 | + new OA\Parameter( |
| 271 | + name: 'summery', |
| 272 | + description: 'Filter by summary text', |
| 273 | + in: 'query', |
| 274 | + required: false, |
| 275 | + schema: new OA\Schema(type: 'string') |
| 276 | + ) |
| 277 | + ], |
| 278 | + responses: [ |
| 279 | + new OA\Response( |
| 280 | + response: 200, |
| 281 | + description: 'Paginated list of subscriber events', |
| 282 | + content: new OA\JsonContent( |
| 283 | + properties: [ |
| 284 | + new OA\Property( |
| 285 | + property: 'items', |
| 286 | + type: 'array', |
| 287 | + items: new OA\Items(ref: '#/components/schemas/SubscriberHistory') |
| 288 | + ), |
| 289 | + new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination') |
| 290 | + ], |
| 291 | + type: 'object' |
| 292 | + ) |
| 293 | + ), |
| 294 | + new OA\Response( |
| 295 | + response: 403, |
| 296 | + description: 'Unauthorized', |
| 297 | + content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse') |
| 298 | + ), |
| 299 | + new OA\Response( |
| 300 | + response: 404, |
| 301 | + description: 'Not Found', |
| 302 | + content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse') |
| 303 | + ) |
| 304 | + ] |
| 305 | + )] |
| 306 | + public function getSubscriberHistory( |
| 307 | + Request $request, |
| 308 | + #[MapEntity(mapping: ['subscriberId' => 'id'])] ?Subscriber $subscriber = null, |
| 309 | + ): JsonResponse { |
| 310 | + $this->requireAuthentication($request); |
| 311 | + |
| 312 | + $historyData = $this->subscriberService->getSubscriberHistory($request, $subscriber); |
| 313 | + |
| 314 | + return $this->json( |
| 315 | + data: $historyData, |
| 316 | + status: Response::HTTP_OK, |
| 317 | + ); |
| 318 | + } |
| 319 | + |
| 320 | + |
229 | 321 | #[Route('/{subscriberId}', name: 'delete', requirements: ['subscriberId' => '\d+'], methods: ['DELETE'])]
|
230 | 322 | #[OA\Delete(
|
231 | 323 | path: '/api/v2/subscribers/{subscriberId}',
|
@@ -278,7 +370,7 @@ public function deleteSubscriber(
|
278 | 370 | if (!$subscriber) {
|
279 | 371 | throw $this->createNotFoundException('Subscriber not found.');
|
280 | 372 | }
|
281 |
| - $this->subscriberManager->deleteSubscriber($subscriber); |
| 373 | + $this->subscriberService->deleteSubscriber($subscriber); |
282 | 374 |
|
283 | 375 | return $this->json(null, Response::HTTP_NO_CONTENT);
|
284 | 376 | }
|
@@ -323,9 +415,9 @@ public function setSubscriberAsConfirmed(Request $request): Response
|
323 | 415 | return new Response('<h1>Missing confirmation code.</h1>', 400);
|
324 | 416 | }
|
325 | 417 |
|
326 |
| - try { |
327 |
| - $this->subscriberManager->markAsConfirmedByUniqueId($uniqueId); |
328 |
| - } catch (NotFoundHttpException) { |
| 418 | + $subscriber = $this->subscriberService->confirmSubscriber($uniqueId); |
| 419 | + |
| 420 | + if (!$subscriber) { |
329 | 421 | return new Response('<h1>Subscriber isn\'t found or already confirmed.</h1>', 404);
|
330 | 422 | }
|
331 | 423 |
|
|
0 commit comments