|
4 | 4 |
|
5 | 5 | namespace PhpList\WebFrontend\Controller; |
6 | 6 |
|
7 | | -use DateTimeImmutable; |
8 | 7 | use PhpList\RestApiClient\Endpoint\SubscribersClient; |
9 | | -use PhpList\RestApiClient\Entity\Subscriber; |
10 | | -use PhpList\RestApiClient\Request\Subscriber\ExportSubscriberRequest; |
11 | 8 | use PhpList\RestApiClient\Request\Subscriber\SubscribersFilterRequest; |
12 | | -use PhpList\RestApiClient\Response\Subscribers\SubscriberCollection; |
| 9 | +use PhpList\WebFrontend\Service\SubscriberCollectionNormalizer; |
| 10 | +use PhpList\WebFrontend\Service\SubscriberExportRequestFactory; |
13 | 11 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
14 | 12 | use Symfony\Component\HttpFoundation\JsonResponse; |
15 | 13 | use Symfony\Component\HttpFoundation\Request; |
|
20 | 18 | #[Route('/subscribers', name: 'subscriber_')] |
21 | 19 | class SubscribersController extends AbstractController |
22 | 20 | { |
23 | | - public function __construct(private readonly SubscribersClient $subscribersClient) |
24 | | - { |
| 21 | + public function __construct( |
| 22 | + private readonly SubscribersClient $subscribersClient, |
| 23 | + private readonly SubscriberCollectionNormalizer $subscriberCollectionNormalizer, |
| 24 | + private readonly SubscriberExportRequestFactory $subscriberExportRequestFactory |
| 25 | + ) { |
25 | 26 | } |
26 | 27 |
|
27 | 28 | /** |
@@ -68,23 +69,13 @@ public function index(Request $request): JsonResponse|Response |
68 | 69 | $prevId = $history[$index - 1]; |
69 | 70 | } |
70 | 71 |
|
71 | | - return $this->json($this->normalize($collection, $prevId, $afterId)); |
| 72 | + return $this->json($this->subscriberCollectionNormalizer->normalize($collection, $prevId, $afterId)); |
72 | 73 | } |
73 | 74 |
|
74 | | - /** |
75 | | - * @SuppressWarnings("CyclomaticComplexity") |
76 | | - * @SuppressWarnings("NPathComplexity") |
77 | | - */ |
78 | 75 | #[Route('/export', name: 'export', methods: ['GET'])] |
79 | 76 | public function export(Request $request): Response |
80 | 77 | { |
81 | | - $exportRequest = new ExportSubscriberRequest( |
82 | | - dateType: (string) $request->query->get('date_type', 'any'), |
83 | | - listId: $request->query->has('list_id') ? $request->query->getInt('list_id') : null, |
84 | | - dateFrom: $request->query->get('date_from') ?: null, |
85 | | - dateTo: $request->query->get('date_to') ?: null, |
86 | | - columns: array_values(array_filter($request->query->all('columns'))) |
87 | | - ); |
| 78 | + $exportRequest = $this->subscriberExportRequestFactory->fromQuery($request->query); |
88 | 79 |
|
89 | 80 | $upstreamResponse = $this->subscribersClient->exportSubscribers($exportRequest); |
90 | 81 |
|
@@ -118,29 +109,4 @@ static function () use ($body): void { |
118 | 109 |
|
119 | 110 | return $response; |
120 | 111 | } |
121 | | - |
122 | | - private function normalize(SubscriberCollection $collection, ?int $prevId, ?int $afterId): array |
123 | | - { |
124 | | - return [ |
125 | | - 'items' => array_map(static function (Subscriber $subscriber) { |
126 | | - return [ |
127 | | - 'id' => $subscriber->id, |
128 | | - 'email' => $subscriber->email, |
129 | | - 'confirmed' => $subscriber->confirmed, |
130 | | - 'blacklisted' => $subscriber->blacklisted, |
131 | | - 'createdAt' => (new DateTimeImmutable($subscriber->createdAt))->format('Y-m-d H:i:s'), |
132 | | - 'uniqueId' => $subscriber->uniqueId, |
133 | | - 'listCount' => count($subscriber->subscribedLists), |
134 | | - ]; |
135 | | - }, $collection->items ?? []), |
136 | | - 'pagination' => [ |
137 | | - 'limit' => $collection->pagination->limit, |
138 | | - 'afterId' => $collection->pagination->nextCursor, |
139 | | - 'hasMore' => $collection->pagination->hasMore , |
140 | | - 'total' => $collection->pagination->total, |
141 | | - 'prevId' => $prevId, |
142 | | - 'isFirstPage' => $afterId === null, |
143 | | - ], |
144 | | - ]; |
145 | | - } |
146 | 112 | } |
0 commit comments