2024-07-21 16:55:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\MessageHandler;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Domain;
|
2025-10-25 17:26:56 +02:00
|
|
|
use App\Entity\Watchlist;
|
2025-10-07 15:55:17 +02:00
|
|
|
use App\Exception\DomainNotFoundException;
|
|
|
|
|
use App\Exception\TldNotSupportedException;
|
|
|
|
|
use App\Exception\UnknownRdapServerException;
|
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;
|
2025-10-07 15:55:17 +02:00
|
|
|
use App\Repository\DomainRepository;
|
2025-10-25 17:26:56 +02:00
|
|
|
use App\Repository\WatchlistRepository;
|
2025-01-02 18:08:51 +01:00
|
|
|
use App\Service\ChatNotificationService;
|
2025-10-13 13:51:51 +02:00
|
|
|
use App\Service\Provider\AbstractProvider;
|
|
|
|
|
use App\Service\Provider\CheckDomainProviderInterface;
|
2024-07-21 16:55:39 +02:00
|
|
|
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-07-21 16:55:39 +02:00
|
|
|
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;
|
2024-07-21 16:55:39 +02:00
|
|
|
|
|
|
|
|
#[AsMessageHandler]
|
2024-08-26 23:45:30 +02:00
|
|
|
final readonly class UpdateDomainsFromWatchlistHandler
|
2024-07-21 16:55:39 +02:00
|
|
|
{
|
2024-08-16 23:23:51 +02:00
|
|
|
private Address $sender;
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
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,
|
2024-07-21 16:55:39 +02:00
|
|
|
private MessageBusInterface $bus,
|
2025-10-25 17:26:56 +02:00
|
|
|
private WatchlistRepository $watchlistRepository,
|
2024-11-01 00:46:25 +01:00
|
|
|
private LoggerInterface $logger,
|
2025-02-22 13:36:10 +01:00
|
|
|
#[Autowire(service: 'service_container')]
|
2025-10-07 16:33:27 +02:00
|
|
|
private ContainerInterface $locator,
|
|
|
|
|
private DomainRepository $domainRepository,
|
2024-08-02 23:24:52 +02:00
|
|
|
) {
|
2024-08-16 23:23:51 +02:00
|
|
|
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-27 00:48:42 +02:00
|
|
|
* @throws \Throwable
|
2024-07-21 16:55:39 +02:00
|
|
|
*/
|
2024-08-26 23:45:30 +02:00
|
|
|
public function __invoke(UpdateDomainsFromWatchlist $message): void
|
2024-07-21 16:55:39 +02:00
|
|
|
{
|
2025-10-25 17:26:56 +02:00
|
|
|
/** @var Watchlist $watchlist */
|
|
|
|
|
$watchlist = $this->watchlistRepository->findOneBy(['token' => $message->watchlistToken]);
|
2024-08-04 14:45:27 +02:00
|
|
|
|
2025-10-13 13:51:51 +02:00
|
|
|
$this->logger->debug('Domain names listed in the Watchlist will be updated', [
|
2025-10-25 17:26:56 +02:00
|
|
|
'watchlist' => $message->watchlistToken,
|
2024-08-04 14:45:27 +02:00
|
|
|
]);
|
|
|
|
|
|
2025-02-23 16:35:57 +01:00
|
|
|
/** @var AbstractProvider $connectorProvider */
|
2025-10-25 17:26:56 +02:00
|
|
|
$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-13 13:51:51 +02:00
|
|
|
$this->logger->debug('Watchlist is linked to a connector', [
|
2025-10-25 17:26:56 +02:00
|
|
|
'watchlist' => $watchlist->getToken(),
|
|
|
|
|
'connector' => $watchlist->getConnector()->getId(),
|
2025-02-22 13:36:10 +01:00
|
|
|
]);
|
|
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
$domainList = array_unique(array_map(fn (Domain $d) => $d->getLdhName(), $watchlist->getDomains()->toArray()));
|
2025-10-13 13:51:51 +02:00
|
|
|
|
2025-02-22 13:36:10 +01:00
|
|
|
try {
|
2025-10-13 13:51:51 +02:00
|
|
|
$checkedDomains = $connectorProvider->checkDomains(...$domainList);
|
2025-02-22 13:36:10 +01:00
|
|
|
} catch (\Throwable $exception) {
|
2025-10-03 18:14:43 +02:00
|
|
|
$this->logger->warning('Unable to check domain names availability with this connector', [
|
2025-10-25 17:26:56 +02:00
|
|
|
'connector' => $watchlist->getConnector()->getId(),
|
2025-10-13 13:51:51 +02:00
|
|
|
'ldhName' => $domainList,
|
2025-02-22 13:36:10 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($checkedDomains as $domain) {
|
2025-10-25 17:26:56 +02:00
|
|
|
$this->bus->dispatch(new OrderDomain($watchlist->getToken(), $domain));
|
2025-02-22 13:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
/*
|
|
|
|
|
* 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
|
2024-12-03 21:10:38 +01:00
|
|
|
* - has specific statuses that suggest there is a dispute between the registrant and the registrar AND was updated more than a day ago
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
/** @var Domain $domain */
|
2025-10-25 17:26:56 +02:00
|
|
|
foreach ($watchlist->getDomains()->filter(fn ($domain) => $this->RDAPService->isToBeUpdated($domain, false, null !== $watchlist->getConnector())) as $domain
|
2024-07-21 16:55:39 +02:00
|
|
|
) {
|
|
|
|
|
$updatedAt = $domain->getUpdatedAt();
|
2025-10-07 15:55:17 +02:00
|
|
|
$deleted = $domain->getDeleted();
|
2024-07-21 16:55:39 +02:00
|
|
|
|
|
|
|
|
try {
|
2024-12-03 21:10:38 +01:00
|
|
|
/*
|
|
|
|
|
* 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());
|
2025-10-25 17:26:56 +02:00
|
|
|
$this->bus->dispatch(new SendDomainEventNotif($watchlist->getToken(), $domain->getLdhName(), $updatedAt));
|
2025-10-07 15:55:17 +02:00
|
|
|
} catch (DomainNotFoundException) {
|
|
|
|
|
$newDomain = $this->domainRepository->findOneBy(['ldhName' => $domain->getLdhName()]);
|
|
|
|
|
|
|
|
|
|
if (!$deleted && null !== $newDomain && $newDomain->getDeleted()) {
|
2025-02-23 16:35:57 +01:00
|
|
|
$notification = new DomainDeletedNotification($this->sender, $domain);
|
2025-10-25 17:26:56 +02:00
|
|
|
$this->mailer->send($notification->asEmailMessage(new Recipient($watchlist->getUser()->getEmail()))->getMessage());
|
|
|
|
|
$this->chatNotificationService->sendChatNotification($watchlist, $notification);
|
2025-01-04 17:35:12 +01:00
|
|
|
}
|
2025-01-02 18:08:51 +01:00
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
if ($watchlist->getConnector()) {
|
2024-12-03 21:10:38 +01:00
|
|
|
/*
|
|
|
|
|
* 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-10-25 17:26:56 +02:00
|
|
|
$this->bus->dispatch(new OrderDomain($watchlist->getToken(), $domain->getLdhName()));
|
2024-08-27 00:09:25 +02:00
|
|
|
}
|
2025-10-07 15:55:17 +02:00
|
|
|
} catch (TldNotSupportedException|UnknownRdapServerException) {
|
2024-12-03 21:10:38 +01:00
|
|
|
/*
|
2025-10-07 15:55:17 +02:00
|
|
|
* In this case, the domain name can no longer be updated. Unfortunately, there is nothing more that can be done.
|
2024-12-03 21:10:38 +01:00
|
|
|
*/
|
2024-08-27 00:48:42 +02:00
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-23 16:35:57 +01:00
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
private function getConnectorProvider(Watchlist $watchlist): ?object
|
2025-02-23 16:35:57 +01:00
|
|
|
{
|
2025-10-25 17:26:56 +02:00
|
|
|
$connector = $watchlist->getConnector();
|
2025-02-23 16:35:57 +01:00
|
|
|
if (null === $connector || null === $connector->getProvider()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$providerClass = $connector->getProvider()->getConnectorProvider();
|
|
|
|
|
|
|
|
|
|
return $this->locator->get($providerClass);
|
|
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|