2024-07-21 16:55:39 +02:00
|
|
|
<?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;
|
2024-07-21 16:55:39 +02:00
|
|
|
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;
|
2024-07-21 16:55:39 +02:00
|
|
|
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;
|
2024-08-27 00:48:42 +02:00
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
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,
|
|
|
|
|
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,
|
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);
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ExceptionInterface
|
2024-08-27 00:48:42 +02:00
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
|
* @throws ClientExceptionInterface
|
|
|
|
|
* @throws DecodingExceptionInterface
|
|
|
|
|
* @throws RedirectionExceptionInterface
|
|
|
|
|
* @throws ServerExceptionInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
|
* @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
|
|
|
{
|
|
|
|
|
/** @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,
|
|
|
|
|
]);
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
/** @var Domain $domain */
|
|
|
|
|
foreach ($watchList->getDomains()
|
2024-08-02 23:24:52 +02:00
|
|
|
->filter(fn ($domain) => $domain->getUpdatedAt()
|
2024-07-25 14:36:09 +02:00
|
|
|
->diff(
|
2024-09-06 13:20:39 +02:00
|
|
|
new \DateTimeImmutable())->days >= 7
|
2024-09-05 15:45:25 +02:00
|
|
|
|| (
|
2024-09-06 13:20:39 +02:00
|
|
|
($domain->getUpdatedAt()
|
|
|
|
|
->diff(
|
|
|
|
|
new \DateTimeImmutable())->h * 60 + $domain->getUpdatedAt()
|
|
|
|
|
->diff(
|
|
|
|
|
new \DateTimeImmutable())->i) >= 50
|
|
|
|
|
&& $this->RDAPService::isToBeWatchClosely($domain)
|
2024-09-05 15:45:25 +02:00
|
|
|
)
|
2024-07-25 14:36:09 +02:00
|
|
|
) as $domain
|
2024-07-21 16:55:39 +02:00
|
|
|
) {
|
|
|
|
|
$updatedAt = $domain->getUpdatedAt();
|
|
|
|
|
|
|
|
|
|
try {
|
2024-08-10 23:29:31 +02:00
|
|
|
$this->RDAPService->registerDomain($domain->getLdhName());
|
2024-08-27 00:48:42 +02:00
|
|
|
$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
|
|
|
|
2024-08-27 00:48:42 +02:00
|
|
|
throw $e;
|
|
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|