|
10 | 10 | use PhpList\Core\Security\Authentication; |
11 | 11 | use PhpList\RestBundle\Common\Controller\BaseController; |
12 | 12 | use PhpList\RestBundle\Common\Validator\RequestValidator; |
| 13 | +use PhpList\RestBundle\Subscription\Request\AddToBlacklistRequest; |
13 | 14 | use Symfony\Component\HttpFoundation\JsonResponse; |
14 | 15 | use Symfony\Component\HttpFoundation\Request; |
15 | 16 | use Symfony\Component\HttpFoundation\Response; |
@@ -144,20 +145,13 @@ public function addEmailToBlacklist(Request $request): JsonResponse |
144 | 145 | throw $this->createAccessDeniedException('You are not allowed to add emails to blacklist.'); |
145 | 146 | } |
146 | 147 |
|
147 | | - $data = json_decode($request->getContent(), true); |
148 | | - if (!isset($data['email']) || empty($data['email'])) { |
149 | | - return $this->json(['error' => 'Email is required'], Response::HTTP_UNPROCESSABLE_ENTITY); |
150 | | - } |
151 | | - |
152 | | - $email = $data['email']; |
153 | | - $reason = $data['reason'] ?? null; |
| 148 | + /** @var AddToBlacklistRequest $definitionRequest */ |
| 149 | + $definitionRequest = $this->validator->validate($request, AddToBlacklistRequest::class); |
154 | 150 |
|
155 | | - $this->blacklistManager->addEmailToBlacklist($email, $reason); |
| 151 | + $userBlacklisted = $this->blacklistManager->addEmailToBlacklist($definitionRequest->email, $definitionRequest->reason); |
| 152 | + $json = $this->normalizer->normalize($userBlacklisted, 'json'); |
156 | 153 |
|
157 | | - return $this->json([ |
158 | | - 'success' => true, |
159 | | - 'message' => sprintf('Email %s has been added to the blacklist', $email), |
160 | | - ], Response::HTTP_CREATED); |
| 154 | + return $this->json($json, Response::HTTP_CREATED); |
161 | 155 | } |
162 | 156 |
|
163 | 157 | #[Route('/remove/{email}', name: 'remove', methods: ['DELETE'])] |
@@ -209,10 +203,7 @@ public function removeEmailFromBlacklist(Request $request, string $email): JsonR |
209 | 203 |
|
210 | 204 | $this->blacklistManager->removeEmailFromBlacklist($email); |
211 | 205 |
|
212 | | - return $this->json([ |
213 | | - 'success' => true, |
214 | | - 'message' => sprintf('Email %s has been removed from the blacklist', $email), |
215 | | - ]); |
| 206 | + return $this->json(null, Response::HTTP_NO_CONTENT); |
216 | 207 | } |
217 | 208 |
|
218 | 209 | #[Route('/info/{email}', name: 'info', methods: ['GET'])] |
@@ -270,14 +261,16 @@ public function getBlacklistInfo(Request $request, string $email): JsonResponse |
270 | 261 |
|
271 | 262 | $blacklistInfo = $this->blacklistManager->getBlacklistInfo($email); |
272 | 263 | if (!$blacklistInfo) { |
273 | | - return $this->json(['error' => sprintf('Email %s is not blacklisted', $email)], Response::HTTP_NOT_FOUND); |
| 264 | + return $this->json([ |
| 265 | + 'error' => sprintf('Email %s is not blacklisted', $email) |
| 266 | + ], Response::HTTP_NOT_FOUND); |
274 | 267 | } |
275 | 268 |
|
276 | 269 | $reason = $this->blacklistManager->getBlacklistReason($email); |
277 | 270 |
|
278 | 271 | return $this->json([ |
279 | 272 | 'email' => $blacklistInfo->getEmail(), |
280 | | - 'added' => $blacklistInfo->getAdded() ? $blacklistInfo->getAdded()->format('c') : null, |
| 273 | + 'added' => $blacklistInfo->getAdded()?->format('c'), |
281 | 274 | 'reason' => $reason, |
282 | 275 | ]); |
283 | 276 | } |
|
0 commit comments