2024-07-21 16:55:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\MessageHandler;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Domain;
|
|
|
|
|
use App\Entity\DomainEvent;
|
2025-10-19 21:37:52 +02:00
|
|
|
use App\Entity\DomainStatus;
|
2025-10-25 17:26:56 +02:00
|
|
|
use App\Entity\Watchlist;
|
2025-10-28 12:09:38 +01:00
|
|
|
use App\Message\DetectDomainChange;
|
2025-10-19 21:37:52 +02:00
|
|
|
use App\Notifier\DomainStatusUpdateNotification;
|
2024-08-16 23:23:51 +02:00
|
|
|
use App\Notifier\DomainUpdateNotification;
|
2025-10-19 21:37:52 +02:00
|
|
|
use App\Repository\DomainEventRepository;
|
2024-07-21 16:55:39 +02:00
|
|
|
use App\Repository\DomainRepository;
|
2025-10-19 21:37:52 +02:00
|
|
|
use App\Repository\DomainStatusRepository;
|
2025-10-25 17:26:56 +02:00
|
|
|
use App\Repository\WatchlistRepository;
|
2024-08-27 00:09:25 +02:00
|
|
|
use App\Service\ChatNotificationService;
|
2024-12-08 13:54:51 +01:00
|
|
|
use App\Service\InfluxdbService;
|
2024-08-25 03:31:09 +02:00
|
|
|
use App\Service\StatService;
|
2024-08-04 14:45:27 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2024-12-08 13:54:51 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
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;
|
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]
|
2025-10-28 12:09:38 +01:00
|
|
|
final readonly class DetectDomainChangeHandler
|
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-16 23:23:51 +02:00
|
|
|
string $mailerSenderEmail,
|
|
|
|
|
string $mailerSenderName,
|
2024-08-06 03:38:00 +02:00
|
|
|
private LoggerInterface $logger,
|
2024-08-23 21:19:34 +02:00
|
|
|
private MailerInterface $mailer,
|
2024-08-26 23:45:30 +02:00
|
|
|
private StatService $statService,
|
|
|
|
|
private DomainRepository $domainRepository,
|
2025-10-25 17:26:56 +02:00
|
|
|
private WatchlistRepository $watchlistRepository,
|
2024-11-01 00:46:25 +01:00
|
|
|
private ChatNotificationService $chatNotificationService,
|
2024-12-08 13:54:51 +01:00
|
|
|
#[Autowire(param: 'influxdb_enabled')]
|
|
|
|
|
private bool $influxdbEnabled,
|
|
|
|
|
private InfluxdbService $influxdbService,
|
2025-10-19 21:37:52 +02:00
|
|
|
private DomainEventRepository $domainEventRepository,
|
|
|
|
|
private DomainStatusRepository $domainStatusRepository,
|
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 TransportExceptionInterface
|
2024-08-02 23:24:52 +02:00
|
|
|
* @throws \Exception
|
2024-07-21 16:55:39 +02:00
|
|
|
*/
|
2025-10-28 12:09:38 +01:00
|
|
|
public function __invoke(DetectDomainChange $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-07-21 16:55:39 +02:00
|
|
|
/** @var Domain $domain */
|
2024-08-02 23:24:52 +02:00
|
|
|
$domain = $this->domainRepository->findOneBy(['ldhName' => $message->ldhName]);
|
2025-10-25 17:26:56 +02:00
|
|
|
$recipient = new Recipient($watchlist->getUser()->getEmail());
|
2024-07-21 16:55:39 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
/*
|
|
|
|
|
* For each new event whose date is after the domain name update date (before the current domain name update)
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-19 21:37:52 +02:00
|
|
|
/** @var DomainEvent[] $newEvents */
|
2025-10-21 12:52:43 +02:00
|
|
|
$newEvents = $this->domainEventRepository->findNewDomainEvents($domain, $message->updatedAt);
|
2025-10-19 21:37:52 +02:00
|
|
|
|
|
|
|
|
foreach ($newEvents as $event) {
|
2025-10-25 17:26:56 +02:00
|
|
|
if (!in_array($event->getAction(), $watchlist->getTrackedEvents())) {
|
2025-10-15 19:52:44 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2024-12-03 21:10:38 +01:00
|
|
|
|
2025-10-15 19:52:44 +02:00
|
|
|
$notification = new DomainUpdateNotification($this->sender, $event);
|
2024-08-16 23:23:51 +02:00
|
|
|
|
2025-10-15 19:52:44 +02:00
|
|
|
$this->logger->info('New action has been detected on this domain name : an email is sent to user', [
|
|
|
|
|
'event' => $event->getAction(),
|
|
|
|
|
'ldhName' => $message->ldhName,
|
2025-10-25 17:26:56 +02:00
|
|
|
'username' => $watchlist->getUser()->getUserIdentifier(),
|
2025-10-15 19:52:44 +02:00
|
|
|
]);
|
2025-10-13 13:51:51 +02:00
|
|
|
|
2025-10-15 19:52:44 +02:00
|
|
|
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
|
2025-10-13 13:51:51 +02:00
|
|
|
|
2025-10-19 21:37:52 +02:00
|
|
|
if ($this->influxdbEnabled) {
|
|
|
|
|
$this->influxdbService->addDomainNotificationPoint($domain, 'email', true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
$webhookDsn = $watchlist->getWebhookDsn();
|
2025-10-15 19:52:44 +02:00
|
|
|
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
|
|
|
|
|
$this->logger->info('New action has been detected on this domain name : a notification is sent to user', [
|
|
|
|
|
'event' => $event->getAction(),
|
|
|
|
|
'ldhName' => $message->ldhName,
|
2025-10-25 17:26:56 +02:00
|
|
|
'username' => $watchlist->getUser()->getUserIdentifier(),
|
2025-10-15 19:52:44 +02:00
|
|
|
]);
|
2025-10-13 13:51:51 +02:00
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
$this->chatNotificationService->sendChatNotification($watchlist, $notification);
|
2025-10-19 21:37:52 +02:00
|
|
|
if ($this->influxdbEnabled) {
|
|
|
|
|
$this->influxdbService->addDomainNotificationPoint($domain, 'chat', true);
|
|
|
|
|
}
|
2025-10-15 19:52:44 +02:00
|
|
|
}
|
2024-08-22 18:11:07 +02:00
|
|
|
|
2025-10-19 21:37:52 +02:00
|
|
|
$this->statService->incrementStat('stats.alert.sent');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var DomainStatus $domainStatus */
|
2025-10-21 12:52:43 +02:00
|
|
|
$domainStatus = $this->domainStatusRepository->findNewDomainStatus($domain, $message->updatedAt);
|
2025-10-19 21:37:52 +02:00
|
|
|
|
|
|
|
|
if (null !== $domainStatus && count(array_intersect(
|
2025-10-25 17:26:56 +02:00
|
|
|
$watchlist->getTrackedEppStatus(),
|
2025-10-19 21:37:52 +02:00
|
|
|
[...$domainStatus->getAddStatus(), ...$domainStatus->getDeleteStatus()]
|
|
|
|
|
))) {
|
|
|
|
|
$notification = new DomainStatusUpdateNotification($this->sender, $domain, $domainStatus);
|
|
|
|
|
|
|
|
|
|
$this->logger->info('New domain status has been detected on this domain name : an email is sent to user', [
|
|
|
|
|
'addStatus' => $domainStatus->getAddStatus(),
|
|
|
|
|
'deleteStatus' => $domainStatus->getDeleteStatus(),
|
|
|
|
|
'status' => $domain->getStatus(),
|
|
|
|
|
'ldhName' => $message->ldhName,
|
2025-10-25 17:26:56 +02:00
|
|
|
'username' => $watchlist->getUser()->getUserIdentifier(),
|
2025-10-19 21:37:52 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
|
|
|
|
|
|
2025-10-15 19:52:44 +02:00
|
|
|
if ($this->influxdbEnabled) {
|
2025-10-19 21:37:52 +02:00
|
|
|
$this->influxdbService->addDomainNotificationPoint($domain, 'email', true);
|
2024-08-18 21:49:34 +02:00
|
|
|
}
|
2025-10-19 21:37:52 +02:00
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
$webhookDsn = $watchlist->getWebhookDsn();
|
2025-10-19 21:37:52 +02:00
|
|
|
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
|
|
|
|
|
$this->logger->info('New domain status has been detected on this domain name : a notification is sent to user', [
|
|
|
|
|
'addStatus' => $domainStatus->getAddStatus(),
|
|
|
|
|
'deleteStatus' => $domainStatus->getDeleteStatus(),
|
|
|
|
|
'status' => $domain->getStatus(),
|
|
|
|
|
'ldhName' => $message->ldhName,
|
2025-10-25 17:26:56 +02:00
|
|
|
'username' => $watchlist->getUser()->getUserIdentifier(),
|
2025-10-19 21:37:52 +02:00
|
|
|
]);
|
|
|
|
|
|
2025-10-25 17:26:56 +02:00
|
|
|
$this->chatNotificationService->sendChatNotification($watchlist, $notification);
|
2025-10-19 21:37:52 +02:00
|
|
|
|
|
|
|
|
if ($this->influxdbEnabled) {
|
|
|
|
|
$this->influxdbService->addDomainNotificationPoint($domain, 'chat', true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 19:52:44 +02:00
|
|
|
$this->statService->incrementStat('stats.alert.sent');
|
2024-08-18 21:49:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|