Files
domain-watchdog/src/MessageHandler/ProcessWatchListsTriggerHandler.php

42 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace App\MessageHandler;
use App\Entity\WatchList;
use App\Message\ProcessWatchListsTrigger;
2024-08-26 23:45:30 +02:00
use App\Message\UpdateDomainsFromWatchlist;
use App\Repository\WatchListRepository;
2024-08-03 19:39:41 +02:00
use Random\Randomizer;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
#[AsMessageHandler]
2024-08-02 23:24:52 +02:00
final readonly class ProcessWatchListsTriggerHandler
{
public function __construct(
private WatchListRepository $watchListRepository,
private MessageBusInterface $bus,
2024-08-02 23:24:52 +02:00
) {
}
/**
* @throws ExceptionInterface
*/
public function __invoke(ProcessWatchListsTrigger $message): void
{
/*
* We shuffle the watch lists to process them in an order that we consider random.
* The shuffling is provided by a high-level API of a CSPRNG number generator.
*/
2024-08-03 19:39:41 +02:00
$randomizer = new Randomizer();
$watchLists = $randomizer->shuffleArray($this->watchListRepository->findAll());
/** @var WatchList $watchList */
2024-08-03 19:39:41 +02:00
foreach ($watchLists as $watchList) {
2024-08-26 23:45:30 +02:00
$this->bus->dispatch(new UpdateDomainsFromWatchlist($watchList->getToken()));
}
}
}