diff --git a/src/Entity/Domain.php b/src/Entity/Domain.php index ea0d906..4044f46 100644 --- a/src/Entity/Domain.php +++ b/src/Entity/Domain.php @@ -330,7 +330,7 @@ class Domain return $this; } - private function setUpdatedAt(?\DateTimeImmutable $updatedAt): void + public function setUpdatedAt(?\DateTimeImmutable $updatedAt): void { $this->updatedAt = $updatedAt; } diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index 62a804a..121363c 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -289,7 +289,6 @@ class RDAPService } $domain->setDeleted(true); - $this->em->persist($domain); $this->em->flush(); } diff --git a/src/State/AutoRegisterDomainProvider.php b/src/State/AutoRegisterDomainProvider.php index f378e22..f464abc 100644 --- a/src/State/AutoRegisterDomainProvider.php +++ b/src/State/AutoRegisterDomainProvider.php @@ -13,6 +13,7 @@ use App\Exception\UnknownRdapServerException; use App\Message\SendDomainEventNotif; use App\Repository\DomainRepository; use App\Service\RDAPService; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\OptimisticLockException; use Psr\Log\LoggerInterface; use Random\Randomizer; @@ -42,6 +43,7 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface private DomainRepository $domainRepository, private MessageBusInterface $bus, private RequestStack $requestStack, + private EntityManagerInterface $em, ) { } @@ -61,6 +63,8 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface */ public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null { + $fromWatchlist = Watchlist::class === $context['root_operation']?->getClass(); + $userId = $this->security->getUser()->getUserIdentifier(); $idnDomain = RDAPService::convertToIdn($uriVariables['ldhName']); @@ -97,7 +101,30 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface } $updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt(); - $domain = $this->RDAPService->registerDomain($idnDomain); + + try { + $domain = $this->RDAPService->registerDomain($idnDomain); + } catch (DomainNotFoundException $exception) { + if (!$fromWatchlist) { + throw $exception; + } + + $domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]); + if (null !== $domain) { + return $domain; + } + + $domain = (new Domain()) + ->setLdhName($idnDomain) + ->setTld($this->RDAPService->getTld($idnDomain)) + ->setDelegationSigned(false) + ->setDeleted(true); + + $this->em->persist($domain); + $this->em->flush(); + + return $domain; + } $randomizer = new Randomizer(); $watchlists = $randomizer->shuffleArray($domain->getWatchlists()->toArray()); diff --git a/tests/MessageHandler/UpdateDomainsFromWatchlistHandlerTest.php b/tests/MessageHandler/UpdateDomainsFromWatchlistHandlerTest.php new file mode 100644 index 0000000..2a068c8 --- /dev/null +++ b/tests/MessageHandler/UpdateDomainsFromWatchlistHandlerTest.php @@ -0,0 +1,48 @@ +get(EntityManagerInterface::class); + $handler = $container->get(UpdateDomainsFromWatchlistHandler::class); + $bus = $container->get('messenger.bus.default'); + + $deletedDomainLdhName = new UuidV4().'.com'; + + $client = WatchlistUpdateProcessorTest::createUserAndWatchlist(null, ['/api/domains/'.$deletedDomainLdhName]); + + /** @var Domain $domain */ + $domain = $entityManager->getRepository(Domain::class)->findOneBy(['ldhName' => $deletedDomainLdhName]); + $domain->setUpdatedAt((new \DateTimeImmutable())->setTimestamp(0)); + $entityManager->flush(); + + $response = $client->request('GET', '/api/watchlists'); + + /** @var Watchlist $watchlist */ + $watchlist = $entityManager->getRepository(Watchlist::class)->findOneBy(['token' => $response->toArray()['hydra:member'][0]['token']]); + + /* @var TraceableMessageBus $bus */ + $bus->reset(); + + $handler(new UpdateDomainsFromWatchlist($watchlist->getToken())); + + $this->expectNotToPerformAssertions(); + } +}