<?php
namespace Cms\DomainBundle\Subscriber\Dns;
use Amp\Dns\DnsRecord;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Cms\DomainBundle\Entity\Apex;
use Cms\DomainBundle\Entity\Domain;
use Cms\DomainBundle\Service\Dns\Lookup;
use Platform\QueueBundle\Event\AsyncEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class LookupSubscriber implements EventSubscriberInterface
{
public const EVENT_APEX = 'app.dns.lookup.apex';
public const EVENT_DOMAIN = 'app.dns.lookup.domain';
private EntityManager $em;
private Lookup $lookup;
public function __construct(EntityManager $em, Lookup $lookup)
{
$this->em = $em;
$this->lookup = $lookup;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
self::EVENT_APEX => ['onApexLookup', 0],
self::EVENT_DOMAIN => ['onDomainLookup', 0],
];
}
public function onApexLookup(AsyncEvent $event): void
{
$apex = $this->em->getRepository(Apex::class)->find(
$event->getBody()->get('id'),
);
if ( ! $apex instanceof Apex) {
throw new \RuntimeException();
}
$event->getOutput()->writeln(
sprintf('DNS lookup for Apex %s...', $apex->getHost())
);
$this->em->save(
$apex->setDnsLookupResult($this->lookup->lookup($apex, DnsRecord::A))
);
}
public function onDomainLookup(AsyncEvent $event): void
{
$domain = $this->em->getRepository(Domain::class)->find(
$event->getBody()->get('id'),
);
if ( ! $domain instanceof Domain) {
throw new \RuntimeException();
}
$event->getOutput()->writeln(
sprintf('DNS lookup for Domain %s...', $domain->getHost())
);
$this->em->save(
$domain->setDnsLookupResult($this->lookup->lookup($domain, DnsRecord::CNAME))
);
}
}