-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailingListSignup.php
More file actions
207 lines (182 loc) · 6.59 KB
/
MailingListSignup.php
File metadata and controls
207 lines (182 loc) · 6.59 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
namespace MatchBot\Application\Actions;
use Laminas\Diactoros\Response\JsonResponse;
use MatchBot\Application\Assertion;
use MatchBot\Client\BadRequestException;
use MatchBot\Client\MailingList;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpBadRequestException;
class MailingListSignup extends Action
{
public function __construct(
LoggerInterface $logger,
private MailingList $mailingListClient,
) {
parent::__construct($logger);
}
/**
* @inheritDoc
*/
#[\Override] protected function action(Request $request, Response $response, array $args): Response
{
$body = (string) $request->getBody();
try {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$this->logger->info("Mailing list signup non-serialisable payload was: $body");
return new JsonResponse([
'success' => false,
'message' => 'Invalid JSON in request body',
], 400);
}
// Validate request body is an array
if (!is_array($data)) {
return new JsonResponse([
'success' => false,
'message' => 'Request body must be a JSON object',
], 400);
}
// Validate required fields
if (!isset($data['mailinglist'])) {
return new JsonResponse([
'success' => false,
'message' => 'Mailing list type is required',
], 400);
}
if (!isset($data['firstName'])) {
return new JsonResponse([
'success' => false,
'message' => 'First name is required',
], 400);
}
if (!isset($data['lastName'])) {
return new JsonResponse([
'success' => false,
'message' => 'Last name is required',
], 400);
}
if (!isset($data['emailAddress'])) {
return new JsonResponse([
'success' => false,
'message' => 'Email address is required',
], 400);
}
$mailingList = $data['mailinglist'];
if (!is_string($mailingList)) {
return new JsonResponse([
'success' => false,
'message' => 'Mailing list type must be a string',
], 400);
}
if ($mailingList !== 'donor' && $mailingList !== 'charity') {
return new JsonResponse([
'success' => false,
'message' => 'Mailing list must be either "donor" or "charity"',
], 400);
}
$firstName = $data['firstName'];
if (!is_string($firstName)) {
return new JsonResponse([
'success' => false,
'message' => 'First name must be a string',
], 400);
}
$lastName = $data['lastName'];
if (!is_string($lastName)) {
return new JsonResponse([
'success' => false,
'message' => 'Last name must be a string',
], 400);
}
$emailAddress = $data['emailAddress'];
if (!is_string($emailAddress)) {
return new JsonResponse([
'success' => false,
'message' => 'Email address must be a string',
], 400);
}
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return new JsonResponse([
'success' => false,
'message' => 'Invalid email address format',
], 400);
}
// Job title is required for charity mailing list
$jobTitle = null;
if ($mailingList === 'charity') {
if (!isset($data['jobTitle'])) {
return new JsonResponse([
'success' => false,
'message' => 'Job title is required for charity mailing list',
], 400);
}
/** @var mixed $rawJobTitle */
$rawJobTitle = $data['jobTitle'];
if (!is_string($rawJobTitle) || empty($rawJobTitle)) {
return new JsonResponse([
'success' => false,
'message' => 'Job title must be a non-empty string for charity mailing list',
], 400);
}
$jobTitle = $rawJobTitle;
} elseif (isset($data['jobTitle'])) {
/** @var mixed $rawJobTitle */
$rawJobTitle = $data['jobTitle'];
if (!is_string($rawJobTitle)) {
return new JsonResponse([
'success' => false,
'message' => 'Job title must be a string',
], 400);
}
$jobTitle = $rawJobTitle;
}
// Optional organisation name
$organisationName = null;
if (isset($data['organisationName'])) {
/** @var mixed $rawOrgName */
$rawOrgName = $data['organisationName'];
if (!is_string($rawOrgName)) {
return new JsonResponse([
'success' => false,
'message' => 'Organisation name must be a string',
], 400);
}
$organisationName = $rawOrgName;
}
try {
$success = $this->mailingListClient->signup(
$mailingList,
$firstName,
$lastName,
$emailAddress,
$jobTitle,
$organisationName
);
if (!$success) {
return new JsonResponse([
'success' => false,
'message' => 'Failed to sign up to mailing list',
], 500);
}
return new JsonResponse([
'success' => true,
'message' => 'Successfully signed up to mailing list',
]);
} catch (BadRequestException $e) {
$this->logger->error('Mailing list signup failed: ' . $e->getMessage());
return new JsonResponse([
'success' => false,
'message' => 'Failed to sign up to mailing list',
], 400);
} catch (\Exception $e) {
$this->logger->error('Mailing list signup error: ' . $e->getMessage());
return new JsonResponse([
'success' => false,
'message' => 'An error occurred while processing your request',
], 500);
}
}
}