Skip to content

Commit 0a341aa

Browse files
committed
After review 1
1 parent e6dff7d commit 0a341aa

1 file changed

Lines changed: 45 additions & 36 deletions

File tree

src/Controller/SubscribersController.php

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use PhpList\RestApiClient\Entity\Subscriber;
1010
use PhpList\RestApiClient\Request\Subscriber\ExportSubscriberRequest;
1111
use PhpList\RestApiClient\Request\Subscriber\SubscribersFilterRequest;
12+
use PhpList\RestApiClient\Response\Subscribers\SubscriberCollection;
1213
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpFoundation\StreamedResponse;
1618
use Symfony\Component\Routing\Attribute\Route;
1719

1820
#[Route('/subscribers', name: 'subscriber_')]
@@ -66,55 +68,26 @@ public function index(Request $request): JsonResponse|Response
6668
$prevId = $history[$index - 1];
6769
}
6870

69-
$initialData = [
70-
'items' => array_map(static function (Subscriber $subscriber) {
71-
return [
72-
'id' => $subscriber->id,
73-
'email' => $subscriber->email,
74-
'confirmed' => $subscriber->confirmed,
75-
'blacklisted' => $subscriber->blacklisted,
76-
'createdAt' => (new DateTimeImmutable($subscriber->createdAt))->format('Y-m-d H:i:s'),
77-
'uniqueId' => $subscriber->uniqueId,
78-
'listCount' => count($subscriber->subscribedLists),
79-
];
80-
}, $collection->items ?? []),
81-
'pagination' => [
82-
'limit' => $collection->pagination->limit,
83-
'afterId' => $collection->pagination->nextCursor,
84-
'hasMore' => $collection->pagination->hasMore ,
85-
'total' => $collection->pagination->total,
86-
'prevId' => $prevId,
87-
'isFirstPage' => $afterId === null,
88-
],
89-
];
90-
91-
return $this->json($initialData);
71+
return $this->json($this->normalize($collection, $prevId, $afterId));
9272
}
9373

74+
/**
75+
* @SuppressWarnings("CyclomaticComplexity")
76+
* @SuppressWarnings("NPathComplexity")
77+
*/
9478
#[Route('/export', name: 'export', methods: ['GET'])]
9579
public function export(Request $request): Response
9680
{
97-
$columns = array_values(
98-
array_filter(
99-
$request->query->all('columns'),
100-
static fn (mixed $value): bool => is_string($value) && $value !== ''
101-
)
102-
);
103-
104-
$defaultRequest = new ExportSubscriberRequest();
105-
10681
$exportRequest = new ExportSubscriberRequest(
10782
dateType: (string) $request->query->get('date_type', 'any'),
10883
listId: $request->query->has('list_id') ? $request->query->getInt('list_id') : null,
10984
dateFrom: $request->query->get('date_from') ?: null,
11085
dateTo: $request->query->get('date_to') ?: null,
111-
columns: $columns === [] ? $defaultRequest->columns : $columns
86+
columns: array_values(array_filter($request->query->all('columns')))
11287
);
11388

11489
$upstreamResponse = $this->subscribersClient->exportSubscribers($exportRequest);
11590

116-
$content = (string) $upstreamResponse->getBody();
117-
11891
$contentType = $upstreamResponse->getHeaderLine('Content-Type');
11992
if ($contentType === '') {
12093
$contentType = 'text/csv; charset=UTF-8';
@@ -128,10 +101,46 @@ public function export(Request $request): Response
128101
);
129102
}
130103

131-
$response = new Response($content);
104+
$body = $upstreamResponse->getBody();
105+
$response = new StreamedResponse(
106+
static function () use ($body): void {
107+
if ($body->isSeekable()) {
108+
$body->rewind();
109+
}
110+
while (! $body->eof()) {
111+
echo $body->read(8192);
112+
}
113+
},
114+
$upstreamResponse->getStatusCode()
115+
);
132116
$response->headers->set('Content-Type', $contentType);
133117
$response->headers->set('Content-Disposition', $contentDisposition);
134118

135119
return $response;
136120
}
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+
}
137146
}

0 commit comments

Comments
 (0)