diff --git a/src/Command/RegisterDomainCommand.php b/src/Command/RegisterDomainCommand.php index 8d6647b..949cc65 100644 --- a/src/Command/RegisterDomainCommand.php +++ b/src/Command/RegisterDomainCommand.php @@ -2,8 +2,11 @@ namespace App\Command; +use App\Entity\WatchList; +use App\Message\SendDomainEventNotif; use App\Repository\DomainRepository; use App\Service\RDAPService; +use Random\Randomizer; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -11,6 +14,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Messenger\MessageBusInterface; #[AsCommand( name: 'app:register-domain', @@ -21,6 +25,7 @@ class RegisterDomainCommand extends Command public function __construct( private readonly DomainRepository $domainRepository, private readonly RDAPService $rdapService, + private readonly MessageBusInterface $bus, ) { parent::__construct(); } @@ -29,7 +34,8 @@ class RegisterDomainCommand extends Command { $this ->addArgument('domain', InputArgument::REQUIRED, 'The domain name to register') - ->addOption('force', 'f', InputOption::VALUE_NEGATABLE, 'Do not check the freshness of the data and still make the query', false); + ->addOption('force', 'f', InputOption::VALUE_NEGATABLE, 'Do not check the freshness of the data and still make the query', false) + ->addOption('notif', null, InputOption::VALUE_NEGATABLE, 'Send notifications to users if a change is detected', false); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -37,6 +43,7 @@ class RegisterDomainCommand extends Command $io = new SymfonyStyle($input, $output); $ldhName = strtolower(idn_to_ascii($input->getArgument('domain'))); $force = (bool) $input->getOption('force'); + $notif = (bool) $input->getOption('notif'); $domain = $this->domainRepository->findOneBy(['ldhName' => $ldhName]); try { @@ -47,7 +54,19 @@ class RegisterDomainCommand extends Command return Command::SUCCESS; } } + + $updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt(); $this->rdapService->registerDomain($ldhName); + + if ($notif) { + $randomizer = new Randomizer(); + $watchLists = $randomizer->shuffleArray($domain->getWatchLists()->toArray()); + + /** @var WatchList $watchList */ + foreach ($watchLists as $watchList) { + $this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt)); + } + } } catch (\Throwable $e) { $io->error($e->getMessage());