|
8 | 8 | use OpenApi\Attributes as OA; |
9 | 9 | use PhpList\Core\Domain\Identity\Model\PrivilegeFlag; |
10 | 10 | use PhpList\Core\Domain\Subscription\Model\Subscriber; |
| 11 | +use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager; |
11 | 12 | use PhpList\Core\Security\Authentication; |
12 | 13 | use PhpList\RestBundle\Common\Controller\BaseController; |
13 | 14 | use PhpList\RestBundle\Common\Validator\RequestValidator; |
14 | 15 | use PhpList\RestBundle\Subscription\Request\CreateSubscriberRequest; |
15 | 16 | use PhpList\RestBundle\Subscription\Request\UpdateSubscriberRequest; |
16 | | -use PhpList\RestBundle\Subscription\Service\SubscriberService; |
| 17 | +use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer; |
| 18 | +use PhpList\RestBundle\Subscription\Service\SubscriberHistoryService; |
17 | 19 | use Symfony\Bridge\Doctrine\Attribute\MapEntity; |
18 | 20 | use Symfony\Component\HttpFoundation\JsonResponse; |
19 | 21 | use Symfony\Component\HttpFoundation\Request; |
20 | 22 | use Symfony\Component\HttpFoundation\Response; |
| 23 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21 | 24 | use Symfony\Component\Routing\Attribute\Route; |
22 | 25 |
|
23 | 26 | /** |
|
29 | 32 | #[Route('/subscribers', name: 'subscriber_')] |
30 | 33 | class SubscriberController extends BaseController |
31 | 34 | { |
32 | | - private SubscriberService $subscriberService; |
33 | | - |
34 | 35 | public function __construct( |
35 | 36 | Authentication $authentication, |
36 | 37 | RequestValidator $validator, |
37 | | - SubscriberService $subscriberService, |
| 38 | + private readonly SubscriberManager $subscriberManager, |
| 39 | + private readonly SubscriberNormalizer $subscriberNormalizer, |
| 40 | + private readonly SubscriberHistoryService $subscriberHistoryService, |
38 | 41 | private readonly EntityManagerInterface $entityManager, |
39 | 42 | ) { |
40 | 43 | parent::__construct($authentication, $validator); |
41 | 44 | $this->authentication = $authentication; |
42 | | - $this->subscriberService = $subscriberService; |
43 | 45 | } |
44 | 46 |
|
45 | 47 | #[Route('', name: 'create', methods: ['POST'])] |
@@ -95,8 +97,9 @@ public function createSubscriber(Request $request): JsonResponse |
95 | 97 |
|
96 | 98 | /** @var CreateSubscriberRequest $subscriberRequest */ |
97 | 99 | $subscriberRequest = $this->validator->validate($request, CreateSubscriberRequest::class); |
98 | | - $subscriberData = $this->subscriberService->createSubscriber($subscriberRequest); |
| 100 | + $subscriber = $this->subscriberManager->createSubscriber($subscriberRequest->getDto()); |
99 | 101 | $this->entityManager->flush(); |
| 102 | + $subscriberData = $this->subscriberNormalizer->normalize($subscriber, 'json'); |
100 | 103 |
|
101 | 104 | return $this->json($subscriberData, Response::HTTP_CREATED); |
102 | 105 | } |
@@ -166,8 +169,9 @@ public function updateSubscriber( |
166 | 169 | } |
167 | 170 | /** @var UpdateSubscriberRequest $updateSubscriberRequest */ |
168 | 171 | $updateSubscriberRequest = $this->validator->validate($request, UpdateSubscriberRequest::class); |
169 | | - $subscriberData = $this->subscriberService->updateSubscriber($updateSubscriberRequest, $admin); |
| 172 | + $subscriber = $this->subscriberManager->updateSubscriber($updateSubscriberRequest->getDto(), $admin); |
170 | 173 | $this->entityManager->flush(); |
| 174 | + $subscriberData = $this->subscriberNormalizer->normalize($subscriber, 'json'); |
171 | 175 |
|
172 | 176 | return $this->json($subscriberData, Response::HTTP_OK); |
173 | 177 | } |
@@ -217,7 +221,8 @@ public function getSubscriber(Request $request, int $subscriberId): JsonResponse |
217 | 221 | { |
218 | 222 | $this->requireAuthentication($request); |
219 | 223 |
|
220 | | - $subscriberData = $this->subscriberService->getSubscriber($subscriberId); |
| 224 | + $subscriber = $this->subscriberManager->getSubscriberById($subscriberId); |
| 225 | + $subscriberData = $this->subscriberNormalizer->normalize($subscriber); |
221 | 226 |
|
222 | 227 | return $this->json($subscriberData, Response::HTTP_OK); |
223 | 228 | } |
@@ -313,7 +318,7 @@ public function getSubscriberHistory( |
313 | 318 | ): JsonResponse { |
314 | 319 | $this->requireAuthentication($request); |
315 | 320 |
|
316 | | - $historyData = $this->subscriberService->getSubscriberHistory($request, $subscriber); |
| 321 | + $historyData = $this->subscriberHistoryService->getSubscriberHistory($request, $subscriber); |
317 | 322 |
|
318 | 323 | return $this->json( |
319 | 324 | data: $historyData, |
@@ -374,7 +379,7 @@ public function deleteSubscriber( |
374 | 379 | if (!$subscriber) { |
375 | 380 | throw $this->createNotFoundException('Subscriber not found.'); |
376 | 381 | } |
377 | | - $this->subscriberService->deleteSubscriber($subscriber); |
| 382 | + $this->subscriberManager->deleteSubscriber($subscriber); |
378 | 383 | $this->entityManager->flush(); |
379 | 384 |
|
380 | 385 | return $this->json(null, Response::HTTP_NO_CONTENT); |
@@ -443,8 +448,9 @@ public function resetBounceCount( |
443 | 448 | throw $this->createNotFoundException('Subscriber not found.'); |
444 | 449 | } |
445 | 450 |
|
446 | | - $subscriberData = $this->subscriberService->resetSubscriberBounceCount($subscriber); |
| 451 | + $subscriber = $this->subscriberManager->resetBounceCount($subscriber); |
447 | 452 | $this->entityManager->flush(); |
| 453 | + $subscriberData = $this->subscriberNormalizer->normalize($subscriber, 'json'); |
448 | 454 |
|
449 | 455 | return $this->json($subscriberData, Response::HTTP_OK); |
450 | 456 | } |
@@ -490,10 +496,10 @@ public function setSubscriberAsConfirmed(Request $request): Response |
490 | 496 | return new Response('<h1>Missing confirmation code.</h1>', 400); |
491 | 497 | } |
492 | 498 |
|
493 | | - $subscriber = $this->subscriberService->confirmSubscriber($uniqueId); |
494 | | - $this->entityManager->flush(); |
495 | | - |
496 | | - if (!$subscriber) { |
| 499 | + try { |
| 500 | + $this->subscriberManager->markAsConfirmedByUniqueId($uniqueId); |
| 501 | + $this->entityManager->flush(); |
| 502 | + } catch (NotFoundHttpException) { |
497 | 503 | return new Response('<h1>Subscriber isn\'t found or already confirmed.</h1>', 404); |
498 | 504 | } |
499 | 505 |
|
|
0 commit comments