refactor: remove dead code

This commit is contained in:
Vincent
2025-05-27 13:59:51 +02:00
parent 51b10db9b5
commit d589ba389b

View File

@@ -60,106 +60,10 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class WatchListController extends AbstractController
{
public function __construct(
private readonly SerializerInterface&DecoderInterface&DenormalizerInterface $serializer,
private readonly EntityManagerInterface $em,
private readonly WatchListRepository $watchListRepository,
private readonly LoggerInterface $logger,
private readonly ChatNotificationService $chatNotificationService,
private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $rdapRequestsLimiter,
private readonly KernelInterface $kernel,
#[Autowire(service: 'service_container')]
private readonly ContainerInterface $locator,
) {
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \JsonException
* @throws \Exception
*/
#[Route(
path: '/api/watchlists',
name: 'watchlist_create',
defaults: [
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'create',
],
methods: ['POST']
)]
public function createWatchList(Request $request): WatchList
{
$watchList = $this->registerDomainsInWatchlist($request->getContent(), ['watchlist:create']);
/** @var User $user */
$user = $this->getUser();
$watchList->setUser($user);
/*
* In the limited version, we do not want a user to be able to register the same domain more than once in their watchlists.
* This policy guarantees the equal probability of obtaining a domain name if it is requested by several users.
*/
if ($this->getParameter('limited_features')) {
if ($watchList->getDomains()->count() > (int) $this->getParameter('limit_max_watchlist_domains')) {
$this->logger->notice('User {username} tried to create a Watchlist. The maximum number of domains has been reached.', [
'username' => $user->getUserIdentifier(),
]);
throw new AccessDeniedHttpException('You have exceeded the maximum number of domain names allowed in this Watchlist');
}
$userWatchLists = $user->getWatchLists();
if ($userWatchLists->count() >= (int) $this->getParameter('limit_max_watchlist')) {
$this->logger->notice('User {username} tried to create a Watchlist. The maximum number of Watchlists has been reached', [
'username' => $user->getUserIdentifier(),
]);
throw new AccessDeniedHttpException('You have exceeded the maximum number of Watchlists allowed');
}
/** @var Domain[] $trackedDomains */
$trackedDomains = $userWatchLists->reduce(fn (array $acc, WatchList $watchList) => [...$acc, ...$watchList->getDomains()->toArray()], []);
/** @var Domain $domain */
foreach ($watchList->getDomains()->getIterator() as $domain) {
if (in_array($domain, $trackedDomains)) {
$ldhName = $domain->getLdhName();
$this->logger->notice('User {username} tried to create a watchlist with domain name {ldhName}. It is forbidden to register the same domain name twice with limited mode', [
'username' => $user->getUserIdentifier(),
'ldhName' => $ldhName,
]);
throw new AccessDeniedHttpException("It is forbidden to register the same domain name twice in your watchlists with limited mode ($ldhName)");
}
}
if (null !== $watchList->getWebhookDsn() && count($watchList->getWebhookDsn()) > (int) $this->getParameter('limit_max_watchlist_webhooks')) {
$this->logger->notice('User {username} tried to create a Watchlist. The maximum number of webhooks has been reached.', [
'username' => $user->getUserIdentifier(),
]);
throw new AccessDeniedHttpException('You have exceeded the maximum number of webhooks allowed in this Watchlist');
}
}
$this->chatNotificationService->sendChatNotification($watchList, new TestChatNotification());
$this->verifyConnector($watchList, $watchList->getConnector());
$this->logger->info('User {username} registers a Watchlist ({token}).', [
'username' => $user->getUserIdentifier(),
'token' => $watchList->getToken(),
]);
$this->em->persist($watchList);
$this->em->flush();
return $watchList;
}
#[Route(
path: '/api/watchlists',
name: 'watchlist_get_all_mine',
@@ -300,49 +204,4 @@ class WatchListController extends AbstractController
return $domains;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \JsonException
*/
private function registerDomainsInWatchlist(string $content, array $groups): WatchList
{
/** @var WatchList $watchList */
$watchList = $this->serializer->deserialize($content, WatchList::class, 'json', ['groups' => $groups]);
return $watchList;
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
if (!is_array($data) || !isset($data['domains']) || !is_array($data['domains'])) {
throw new BadRequestHttpException('Invalid payload: missing or invalid "domains" field.');
}
$domains = array_map(fn (string $d) => str_replace('/api/domains/', '', $d), $data['domains']);
foreach ($domains as $ldhName) {
/** @var ?Domain $domain */
$domain = $this->domainRepository->findOneBy(['ldhName' => $ldhName]);
if (null === $domain) {
$domain = $this->RDAPService->registerDomain($ldhName);
if (false === $this->kernel->isDebug() && true === $this->getParameter('limited_features')) {
$limiter = $this->rdapRequestsLimiter->create($this->getUser()->getUserIdentifier());
$limit = $limiter->consume();
if (!$limit->isAccepted()) {
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
}
}
}
$watchList->addDomain($domain);
}
return $watchList;
}
}