mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
refactor: auto register domains on watchlist creation
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Config\EventAction;
|
||||
use App\Controller\DomainRefreshController;
|
||||
use App\Repository\DomainRepository;
|
||||
use App\Service\RDAPService;
|
||||
use App\State\AutoRegisterDomainProvider;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
@@ -42,7 +43,8 @@ use Symfony\Component\Serializer\Attribute\SerializedName;
|
||||
],
|
||||
read: false
|
||||
),
|
||||
]
|
||||
],
|
||||
provider: AutoRegisterDomainProvider::class
|
||||
)]
|
||||
class Domain
|
||||
{
|
||||
|
||||
65
src/State/AutoRegisterDomainProvider.php
Normal file
65
src/State/AutoRegisterDomainProvider.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Entity\Domain;
|
||||
use App\Service\RDAPService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||
|
||||
class AutoRegisterDomainProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.item_provider')]
|
||||
private ProviderInterface $itemProvider,
|
||||
private readonly RDAPService $RDAPService,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly KernelInterface $kernel,
|
||||
private readonly ParameterBagInterface $parameterBag,
|
||||
private readonly RateLimiterFactory $rdapRequestsLimiter, private readonly Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
$domain = $this->itemProvider->provide($operation, $uriVariables, $context);
|
||||
|
||||
if (!is_null($domain)) {
|
||||
return $domain;
|
||||
}
|
||||
|
||||
if (false === $this->kernel->isDebug() && true === $this->parameterBag->get('limited_features')) {
|
||||
$limiter = $this->rdapRequestsLimiter->create($this->security->getUser()->getUserIdentifier());
|
||||
$limit = $limiter->consume();
|
||||
|
||||
if (!$limit->isAccepted()) {
|
||||
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
|
||||
}
|
||||
}
|
||||
|
||||
$ldhName = RDAPService::convertToIdn($uriVariables['ldhName']);
|
||||
|
||||
try {
|
||||
$domain = $this->RDAPService->registerDomain($ldhName);
|
||||
} catch (NotFoundHttpException) {
|
||||
$domain = (new Domain())
|
||||
->setLdhName($ldhName)
|
||||
->setTld($this->RDAPService->getTld($ldhName))
|
||||
->setDelegationSigned(false)
|
||||
->setDeleted(true);
|
||||
|
||||
$this->entityManager->persist($domain);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $domain;
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,8 @@ use App\Entity\Domain;
|
||||
use App\Entity\User;
|
||||
use App\Entity\WatchList;
|
||||
use App\Notifier\TestChatNotification;
|
||||
use App\Repository\DomainRepository;
|
||||
use App\Service\ChatNotificationService;
|
||||
use App\Service\Connector\AbstractProvider;
|
||||
use App\Service\RDAPService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
@@ -20,8 +17,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||
|
||||
class WatchListUpdateProcessor implements ProcessorInterface
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user