src/Cms/DomainBundle/Subscriber/Dns/LookupSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace Cms\DomainBundle\Subscriber\Dns;
  3. use Amp\Dns\DnsRecord;
  4. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  5. use Cms\DomainBundle\Entity\Apex;
  6. use Cms\DomainBundle\Entity\Domain;
  7. use Cms\DomainBundle\Service\Dns\Lookup;
  8. use Platform\QueueBundle\Event\AsyncEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. final class LookupSubscriber implements EventSubscriberInterface
  11. {
  12.     public const EVENT_APEX 'app.dns.lookup.apex';
  13.     public const EVENT_DOMAIN 'app.dns.lookup.domain';
  14.     private EntityManager $em;
  15.     private Lookup $lookup;
  16.     public function __construct(EntityManager $emLookup $lookup)
  17.     {
  18.         $this->em $em;
  19.         $this->lookup $lookup;
  20.     }
  21.     /**
  22.      * {@inheritDoc}
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             self::EVENT_APEX => ['onApexLookup'0],
  28.             self::EVENT_DOMAIN => ['onDomainLookup'0],
  29.         ];
  30.     }
  31.     public function onApexLookup(AsyncEvent $event): void
  32.     {
  33.         $apex $this->em->getRepository(Apex::class)->find(
  34.             $event->getBody()->get('id'),
  35.         );
  36.         if ( ! $apex instanceof Apex) {
  37.             throw new \RuntimeException();
  38.         }
  39.         $event->getOutput()->writeln(
  40.             sprintf('DNS lookup for Apex  %s...'$apex->getHost())
  41.         );
  42.         $this->em->save(
  43.             $apex->setDnsLookupResult($this->lookup->lookup($apexDnsRecord::A))
  44.         );
  45.     }
  46.     public function onDomainLookup(AsyncEvent $event): void
  47.     {
  48.         $domain $this->em->getRepository(Domain::class)->find(
  49.             $event->getBody()->get('id'),
  50.         );
  51.         if ( ! $domain instanceof Domain) {
  52.             throw new \RuntimeException();
  53.         }
  54.         $event->getOutput()->writeln(
  55.             sprintf('DNS lookup for Domain  %s...'$domain->getHost())
  56.         );
  57.         $this->em->save(
  58.             $domain->setDnsLookupResult($this->lookup->lookup($domainDnsRecord::CNAME))
  59.         );
  60.     }
  61. }