Skip to content

Commit 5d17a40

Browse files
committed
blacklist request
1 parent f8472e9 commit 5d17a40

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/Subscription/Controller/BlacklistController.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpList\Core\Security\Authentication;
1111
use PhpList\RestBundle\Common\Controller\BaseController;
1212
use PhpList\RestBundle\Common\Validator\RequestValidator;
13+
use PhpList\RestBundle\Subscription\Request\AddToBlacklistRequest;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
@@ -144,20 +145,13 @@ public function addEmailToBlacklist(Request $request): JsonResponse
144145
throw $this->createAccessDeniedException('You are not allowed to add emails to blacklist.');
145146
}
146147

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);
154150

155-
$this->blacklistManager->addEmailToBlacklist($email, $reason);
151+
$userBlacklisted = $this->blacklistManager->addEmailToBlacklist($definitionRequest->email, $definitionRequest->reason);
152+
$json = $this->normalizer->normalize($userBlacklisted, 'json');
156153

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);
161155
}
162156

163157
#[Route('/remove/{email}', name: 'remove', methods: ['DELETE'])]
@@ -209,10 +203,7 @@ public function removeEmailFromBlacklist(Request $request, string $email): JsonR
209203

210204
$this->blacklistManager->removeEmailFromBlacklist($email);
211205

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);
216207
}
217208

218209
#[Route('/info/{email}', name: 'info', methods: ['GET'])]
@@ -270,14 +261,16 @@ public function getBlacklistInfo(Request $request, string $email): JsonResponse
270261

271262
$blacklistInfo = $this->blacklistManager->getBlacklistInfo($email);
272263
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);
274267
}
275268

276269
$reason = $this->blacklistManager->getBlacklistReason($email);
277270

278271
return $this->json([
279272
'email' => $blacklistInfo->getEmail(),
280-
'added' => $blacklistInfo->getAdded() ? $blacklistInfo->getAdded()->format('c') : null,
273+
'added' => $blacklistInfo->getAdded()?->format('c'),
281274
'reason' => $reason,
282275
]);
283276
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Subscription\Request;
6+
7+
use PhpList\RestBundle\Common\Request\RequestInterface;
8+
use Symfony\Component\Validator\Constraints as Assert;
9+
10+
class AddToBlacklistRequest implements RequestInterface
11+
{
12+
#[Assert\NotBlank]
13+
#[Assert\Email]
14+
public string $email;
15+
16+
#[Assert\Type(type: 'string')]
17+
public ?string $reason = null;
18+
19+
public function getDto(): self
20+
{
21+
return $this;
22+
}
23+
}

0 commit comments

Comments
 (0)