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

101 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\WatchList;
2024-08-26 23:45:30 +02:00
use App\Message\OrderDomain;
use App\Message\SendDomainEventNotif;
use App\Message\UpdateDomainsFromWatchlist;
2024-08-16 23:23:51 +02:00
use App\Notifier\DomainUpdateErrorNotification;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
2024-08-04 14:45:27 +02:00
use Psr\Log\LoggerInterface;
2024-08-27 00:09:25 +02:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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;
2024-08-16 23:23:51 +02:00
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
#[AsMessageHandler]
2024-08-26 23:45:30 +02:00
final readonly class UpdateDomainsFromWatchlistHandler
{
2024-08-16 23:23:51 +02:00
private Address $sender;
public function __construct(
2024-08-02 23:24:52 +02:00
private RDAPService $RDAPService,
private MailerInterface $mailer,
2024-08-16 23:23:51 +02:00
string $mailerSenderEmail,
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
) {
2024-08-16 23:23:51 +02:00
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
/**
* @throws ExceptionInterface
* @throws TransportExceptionInterface
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @throws \Throwable
*/
2024-08-26 23:45:30 +02:00
public function __invoke(UpdateDomainsFromWatchlist $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()
2024-09-09 11:31:33 +02:00
->diff(new \DateTimeImmutable())->days >= 7
2024-09-05 15:45:25 +02:00
|| (
2024-09-06 13:20:39 +02:00
($domain->getUpdatedAt()
2024-09-09 11:31:33 +02:00
->diff(new \DateTimeImmutable())->h * 60 + $domain->getUpdatedAt()
->diff(new \DateTimeImmutable())->i) >= 50
2024-09-06 13:20:39 +02:00
&& $this->RDAPService::isToBeWatchClosely($domain)
2024-09-05 15:45:25 +02:00
)
2024-09-09 11:31:33 +02:00
|| (count(array_intersect($domain->getStatus(), ['auto renew period', 'client hold', 'server hold'])) > 0
&& $domain->getUpdatedAt()->diff(new \DateTimeImmutable())->days >= 1
)
) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
2024-08-10 23:29:31 +02:00
$this->RDAPService->registerDomain($domain->getLdhName());
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
2024-08-27 00:09:25 +02:00
} catch (NotFoundHttpException) {
if (null !== $watchList->getConnector()) {
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
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-08-16 23:23:51 +02:00
$email = (new DomainUpdateErrorNotification($this->sender, $domain))
->asEmailMessage(new Recipient($watchList->getUser()->getEmail()));
$this->mailer->send($email->getMessage());
2024-08-10 23:31:34 +02:00
throw $e;
}
}
}
}