mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: reduce IO when registering domains
This commit is contained in:
@@ -3,8 +3,21 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\Domain;
|
||||||
|
use App\Entity\DomainEntity;
|
||||||
|
use App\Entity\DomainEvent;
|
||||||
|
use App\Repository\DomainRepository;
|
||||||
use App\Service\RDAPService;
|
use App\Service\RDAPService;
|
||||||
|
use Eluceo\iCal\Domain\Entity\Attendee;
|
||||||
|
use Eluceo\iCal\Domain\Entity\Calendar;
|
||||||
|
use Eluceo\iCal\Domain\Entity\Event;
|
||||||
|
use Eluceo\iCal\Domain\ValueObject\Category;
|
||||||
|
use Eluceo\iCal\Domain\ValueObject\Date;
|
||||||
|
use Eluceo\iCal\Domain\ValueObject\EmailAddress;
|
||||||
|
use Eluceo\iCal\Domain\ValueObject\SingleDay;
|
||||||
|
use Eluceo\iCal\Presentation\Factory\CalendarFactory;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Sabre\VObject\Reader;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -14,7 +27,8 @@ class TestController extends AbstractController
|
|||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly RDAPService $RDAPService
|
private readonly RDAPService $RDAPService,
|
||||||
|
private readonly DomainRepository $domainRepository
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -24,11 +38,46 @@ class TestController extends AbstractController
|
|||||||
public function testRegisterDomain(string $fqdn): Response
|
public function testRegisterDomain(string $fqdn): Response
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->RDAPService->registerDomain($fqdn);
|
$this->RDAPService->registerDomains([$fqdn]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
return new Response();
|
return new Response();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/test/publish/calendar', name: 'test_publish_calendar')]
|
||||||
|
public function testPublishCalendar(): Response
|
||||||
|
{
|
||||||
|
$calendar = new Calendar();
|
||||||
|
|
||||||
|
|
||||||
|
/** @var Domain $domain */
|
||||||
|
foreach ($this->domainRepository->findAll() as $domain) {
|
||||||
|
$attendees = [];
|
||||||
|
|
||||||
|
/** @var DomainEntity $entity */
|
||||||
|
foreach ($domain->getDomainEntities()->toArray() as $entity) {
|
||||||
|
$vCard = Reader::readJson($entity->getEntity()->getJCard());
|
||||||
|
$email = (string)$vCard->EMAIL;
|
||||||
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) continue;
|
||||||
|
|
||||||
|
$attendees[] = (new Attendee(new EmailAddress($email)))->setDisplayName((string)$vCard->FN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var DomainEvent $event */
|
||||||
|
foreach ($domain->getEvents()->toArray() as $event) {
|
||||||
|
$calendar->addEvent((new Event())
|
||||||
|
->setSummary($domain->getLdhName() . ' (' . $event->getAction()->value . ')')
|
||||||
|
->addCategory(new Category($event->getAction()->value))
|
||||||
|
->setAttendees($attendees)
|
||||||
|
->setOccurrence(new SingleDay(new Date($event->getDate())))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response((new CalendarFactory())->createCalendar($calendar), Response::HTTP_OK, [
|
||||||
|
"Content-Type" => 'text/calendar; charset=utf-8'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -47,11 +47,22 @@ readonly class RDAPService
|
|||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function registerDomain(string $fqdn): Domain
|
public function registerDomains(array $domains): void
|
||||||
|
{
|
||||||
|
$dnsRoot = json_decode(file_get_contents($this->params->get('kernel.project_dir') . '/src/Config/dns.json'))->services;
|
||||||
|
foreach ($domains as $fqdn) {
|
||||||
|
$this->registerDomain($dnsRoot, $fqdn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function registerDomain(array $dnsRoot, string $fqdn): void
|
||||||
{
|
{
|
||||||
$idnDomain = idn_to_ascii($fqdn);
|
$idnDomain = idn_to_ascii($fqdn);
|
||||||
try {
|
try {
|
||||||
$rdapServer = $this->getRDAPServer(RDAPService::getTld($idnDomain));
|
$rdapServer = $this->getRDAPServer($dnsRoot, RDAPService::getTld($idnDomain));
|
||||||
} catch (Exception) {
|
} catch (Exception) {
|
||||||
throw new Exception("Unable to determine which RDAP server to contact");
|
throw new Exception("Unable to determine which RDAP server to contact");
|
||||||
}
|
}
|
||||||
@@ -156,17 +167,15 @@ readonly class RDAPService
|
|||||||
$this->em->persist($domain);
|
$this->em->persist($domain);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
return $domain;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function getRDAPServer(string $tld)
|
private function getRDAPServer(array $dnsRoot, string $tld)
|
||||||
{
|
{
|
||||||
|
|
||||||
$dnsRoot = json_decode(file_get_contents($this->params->get('kernel.project_dir') . '/src/Config/dns.json'))->services;
|
|
||||||
foreach ($dnsRoot as $dns) {
|
foreach ($dnsRoot as $dns) {
|
||||||
if (in_array($tld, $dns[0])) return $dns[1][0];
|
if (in_array($tld, $dns[0])) return $dns[1][0];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user