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

91 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\User;
use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger;
use App\Message\ProcessWatchListTrigger;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
2024-08-04 14:45:27 +02:00
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
2024-08-05 01:30:27 +02:00
use Symfony\Component\Mime\Address;
#[AsMessageHandler]
final readonly class ProcessWatchListTriggerHandler
{
public function __construct(
2024-08-02 23:24:52 +02:00
private RDAPService $RDAPService,
private MailerInterface $mailer,
private string $mailerSenderEmail,
2024-08-05 01:30:27 +02:00
private string $mailerSenderName,
private MessageBusInterface $bus,
2024-08-04 14:45:27 +02:00
private WatchListRepository $watchListRepository,
private LoggerInterface $logger
2024-08-02 23:24:52 +02:00
) {
}
/**
* @throws TransportExceptionInterface
2024-08-02 23:24:52 +02:00
* @throws \Exception
* @throws ExceptionInterface
*/
public function __invoke(ProcessWatchListTrigger $message): void
{
/** @var WatchList $watchList */
2024-08-02 23:24:52 +02:00
$watchList = $this->watchListRepository->findOneBy(['token' => $message->watchListToken]);
2024-08-04 14:45:27 +02:00
$this->logger->info('Domain names from Watchlist {token} will be processed.', [
'token' => $message->watchListToken,
]);
/** @var Domain $domain */
foreach ($watchList->getDomains()
2024-08-02 23:24:52 +02:00
->filter(fn ($domain) => $domain->getUpdatedAt()
->diff(
2024-08-02 23:24:52 +02:00
new \DateTimeImmutable('now'))->days >= 7
2024-07-25 20:21:57 +02:00
|| $this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt())
) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
2024-07-29 16:55:45 +02:00
$domain = $this->RDAPService->registerDomain($domain->getLdhName());
2024-08-02 23:24:52 +02:00
} catch (\Throwable $e) {
2024-08-10 23:11:59 +02:00
$this->logger->error('An update error email is sent to user {username}.', [
2024-08-04 14:45:27 +02:00
'username' => $watchList->getUser()->getUserIdentifier(),
2024-08-10 23:11:59 +02:00
'error' => $e,
2024-08-04 14:45:27 +02:00
]);
2024-07-31 12:35:46 +02:00
$this->sendEmailDomainUpdateError($domain, $watchList->getUser());
2024-08-10 23:06:03 +02:00
}
2024-08-10 23:16:59 +02:00
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
}
/**
* @throws TransportExceptionInterface
*/
private function sendEmailDomainUpdateError(Domain $domain, User $user): void
{
$email = (new TemplatedEmail())
2024-08-05 01:30:27 +02:00
->from(new Address($this->mailerSenderEmail, $this->mailerSenderName))
->to($user->getEmail())
->subject('An error occurred while updating a domain name')
->htmlTemplate('emails/errors/domain_update.html.twig')
->locale('en')
->context([
2024-08-02 23:24:52 +02:00
'domain' => $domain,
]);
$this->mailer->send($email);
}
}