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

150 lines
6.1 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;
2025-01-02 18:08:51 +01:00
use App\Notifier\DomainDeletedNotification;
2024-08-16 23:23:51 +02:00
use App\Notifier\DomainUpdateErrorNotification;
use App\Repository\WatchListRepository;
2025-01-02 18:08:51 +01:00
use App\Service\ChatNotificationService;
2025-02-22 13:36:10 +01:00
use App\Service\Connector\AbstractProvider;
use App\Service\Connector\CheckDomainProviderInterface;
use App\Service\RDAPService;
2024-08-04 14:45:27 +02:00
use Psr\Log\LoggerInterface;
2025-02-22 13:36:10 +01:00
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
2024-08-27 00:09:25 +02:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
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;
#[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,
2025-01-02 18:08:51 +01:00
private ChatNotificationService $chatNotificationService,
2024-08-02 23:24:52 +02:00
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,
2025-02-22 13:36:10 +01:00
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
2024-08-02 23:24:52 +02:00
) {
2024-08-16 23:23:51 +02:00
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
/**
* @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
2025-10-03 18:14:43 +02:00
$this->logger->info('Domain names listed in the Watchlist will be updated', [
'watchlist' => $message->watchListToken,
2024-08-04 14:45:27 +02:00
]);
2025-02-23 16:35:57 +01:00
/** @var AbstractProvider $connectorProvider */
$connectorProvider = $this->getConnectorProvider($watchList);
2025-02-22 13:36:10 +01:00
2025-02-23 16:35:57 +01:00
if ($connectorProvider instanceof CheckDomainProviderInterface) {
2025-10-03 18:14:43 +02:00
$this->logger->notice('Watchlist is linked to a connector', [
2025-02-23 16:35:57 +01:00
'watchlist' => $watchList->getToken(),
'connector' => $watchList->getConnector()->getId(),
2025-02-22 13:36:10 +01:00
]);
try {
$checkedDomains = $connectorProvider->checkDomains(
...array_unique(array_map(fn (Domain $d) => $d->getLdhName(), $watchList->getDomains()->toArray()))
);
} catch (\Throwable $exception) {
2025-10-03 18:14:43 +02:00
$this->logger->warning('Unable to check domain names availability with this connector', [
2025-02-23 16:35:57 +01:00
'connector' => $watchList->getConnector()->getId(),
2025-02-22 13:36:10 +01:00
]);
throw $exception;
}
foreach ($checkedDomains as $domain) {
2025-02-23 16:35:57 +01:00
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain));
2025-02-22 13:36:10 +01:00
}
return;
}
/*
* A domain name is updated if one or more of these conditions are met:
* - was updated more than 7 days ago
2025-01-04 19:35:55 +01:00
* - has statuses that suggest it will expire soon AND was updated more than 15 minutes ago
* - has specific statuses that suggest there is a dispute between the registrant and the registrar AND was updated more than a day ago
*/
/** @var Domain $domain */
2025-04-06 15:25:38 +02:00
foreach ($watchList->getDomains()->filter(fn ($domain) => $domain->isToBeUpdated(false, null !== $watchList->getConnector())) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
/*
* Domain name update
* We send messages that correspond to the sending of notifications that will not be processed here.
*/
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) {
2025-01-04 17:35:12 +01:00
if (!$domain->getDeleted()) {
2025-02-23 16:35:57 +01:00
$notification = new DomainDeletedNotification($this->sender, $domain);
2025-01-04 17:35:12 +01:00
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchList, $notification);
}
2025-01-02 18:08:51 +01:00
2025-02-23 16:35:57 +01:00
if ($watchList->getConnector()) {
/*
* If the domain name no longer appears in the WHOIS AND a connector is associated with this Watchlist,
* this connector is used to purchase the domain name.
*/
2025-02-23 16:35:57 +01:00
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain->getLdhName()));
2024-08-27 00:09:25 +02:00
}
2024-08-02 23:24:52 +02:00
} catch (\Throwable $e) {
/*
* In case of another unknown error,
* the owner of the Watchlist is informed that an error occurred in updating the domain name.
*/
2025-10-03 18:14:43 +02:00
$this->logger->error('Update error email is sent to user', [
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;
}
}
}
2025-02-23 16:35:57 +01:00
private function getConnectorProvider(WatchList $watchList): ?object
{
$connector = $watchList->getConnector();
if (null === $connector || null === $connector->getProvider()) {
return null;
}
$providerClass = $connector->getProvider()->getConnectorProvider();
return $this->locator->get($providerClass);
}
}