-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSubscribersController.php
More file actions
120 lines (102 loc) · 4.41 KB
/
Copy pathSubscribersController.php
File metadata and controls
120 lines (102 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace PhpList\WebFrontend\Controller;
use PhpList\RestApiClient\Endpoint\SubscribersClient;
use PhpList\RestApiClient\Request\Subscriber\SubscribersFilterRequest;
use PhpList\WebFrontend\Service\SubscriberCollectionNormalizer;
use PhpList\WebFrontend\Service\SubscriberExportRequestFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/subscribers', name: 'subscriber_')]
class SubscribersController extends AbstractController
{
public function __construct(
private readonly SubscribersClient $subscribersClient,
private readonly SubscriberCollectionNormalizer $subscriberCollectionNormalizer,
private readonly SubscriberExportRequestFactory $subscriberExportRequestFactory
) {
}
/**
* @SuppressWarnings("CyclomaticComplexity")
* @SuppressWarnings("NPathComplexity")
*/
#[Route('/', name: 'list', methods: ['GET'])]
public function index(Request $request): JsonResponse|Response
{
$accept = (string) $request->headers->get('Accept', '');
$wantsJson = $request->isXmlHttpRequest() || str_contains($accept, 'application/json');
if (! $wantsJson) {
return $this->render('@PhpListFrontend/spa.html.twig', [
'page' => 'Subscribers',
]);
}
$afterId = $request->query->has('after_id') ? $request->query->getInt('after_id') : null;
$filter = new SubscribersFilterRequest(
isConfirmed: $request->query->has('confirmed') ? true :
($request->query->has('unconfirmed') ? false : null),
isBlacklisted: $request->query->has('blacklisted') ? true :
($request->query->has('non-blacklisted') ? false : null),
findColumn: $request->query->get('findColumn'),
findValue: $request->query->get('findValue'),
);
$collection = $this->subscribersClient->getSubscribers($filter, $afterId, 10);
$history = $request->getSession()->get('subscribers_history', []);
if (!in_array($afterId, $history, true)) {
$history[] = $afterId;
$request->getSession()->set('subscribers_history', $history);
}
$prevId = null;
$index = array_search($afterId, $history, true);
if ($index === 0) {
$prevId = 0;
} elseif ($index > 0) {
$prevId = $history[$index - 1];
}
return $this->json($this->subscriberCollectionNormalizer->normalize($collection, $prevId, $afterId));
}
/**
* @SuppressWarnings("CyclomaticComplexity")
*/
#[Route('/export', name: 'export', methods: ['GET'])]
public function export(Request $request): Response
{
$exportRequest = $this->subscriberExportRequestFactory->fromQuery($request->query);
$upstreamResponse = $this->subscribersClient->exportSubscribers($exportRequest);
$statusCode = $upstreamResponse->getStatusCode();
$isSuccessfulExport = $statusCode >= 200 && $statusCode < 300;
$contentType = $upstreamResponse->getHeaderLine('Content-Type');
if ($isSuccessfulExport && $contentType === '') {
$contentType = 'text/csv; charset=UTF-8';
}
$contentDisposition = $upstreamResponse->getHeaderLine('Content-Disposition');
if ($isSuccessfulExport && $contentDisposition === '') {
$contentDisposition = sprintf(
'attachment; filename="subscribers_export_%s.csv"',
date('Y-m-d_H-i-s')
);
}
$body = $upstreamResponse->getBody();
$response = new StreamedResponse(
static function () use ($body): void {
if ($body->isSeekable()) {
$body->rewind();
}
while (! $body->eof()) {
echo $body->read(8192);
}
},
$statusCode
);
if ($contentType !== '') {
$response->headers->set('Content-Type', $contentType);
}
if ($contentDisposition !== '') {
$response->headers->set('Content-Disposition', $contentDisposition);
}
return $response;
}
}