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

165 lines
6.7 KiB
PHP
Raw Normal View History

<?php
namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\DomainStatus;
use App\Entity\WatchList;
2024-08-26 23:45:30 +02:00
use App\Message\SendDomainEventNotif;
use App\Notifier\DomainStatusUpdateNotification;
2024-08-16 23:23:51 +02:00
use App\Notifier\DomainUpdateNotification;
use App\Repository\DomainEventRepository;
use App\Repository\DomainRepository;
use App\Repository\DomainStatusRepository;
use App\Repository\WatchListRepository;
2024-08-27 00:09:25 +02:00
use App\Service\ChatNotificationService;
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;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
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;
#[AsMessageHandler]
2024-08-26 23:45:30 +02:00
final readonly class SendDomainEventNotifHandler
{
2024-08-16 23:23:51 +02:00
private Address $sender;
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,
2024-08-27 00:09:25 +02:00
private WatchListRepository $watchListRepository,
private ChatNotificationService $chatNotificationService,
#[Autowire(param: 'influxdb_enabled')]
private bool $influxdbEnabled,
private InfluxdbService $influxdbService,
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);
}
/**
* @throws TransportExceptionInterface
2024-08-02 23:24:52 +02:00
* @throws \Exception
*/
2024-08-26 23:45:30 +02:00
public function __invoke(SendDomainEventNotif $message): void
{
/** @var WatchList $watchList */
2024-08-02 23:24:52 +02:00
$watchList = $this->watchListRepository->findOneBy(['token' => $message->watchListToken]);
/** @var Domain $domain */
2024-08-02 23:24:52 +02:00
$domain = $this->domainRepository->findOneBy(['ldhName' => $message->ldhName]);
$recipient = new Recipient($watchList->getUser()->getEmail());
/*
* For each new event whose date is after the domain name update date (before the current domain name update)
*/
/** @var DomainEvent[] $newEvents */
$newEvents = $this->domainEventRepository->createQueryBuilder('de')
->select()
->where('de.domain = :domain')
->andWhere('de.date > :updatedAt')
->andWhere('de.date < :now')
->setParameter('domain', $domain)
->setParameter('updatedAt', $message->updatedAt)
->setParameter('now', new \DateTimeImmutable())
->getQuery()->getResult();
foreach ($newEvents as $event) {
2025-10-15 19:52:44 +02:00
if (!in_array($event->getAction(), $watchList->getTrackedEvents())) {
continue;
}
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,
'username' => $watchList->getUser()->getUserIdentifier(),
]);
2025-10-15 19:52:44 +02:00
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
if ($this->influxdbEnabled) {
$this->influxdbService->addDomainNotificationPoint($domain, 'email', true);
}
2025-10-15 19:52:44 +02:00
$webhookDsn = $watchList->getWebhookDsn();
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,
'username' => $watchList->getUser()->getUserIdentifier(),
]);
2025-10-15 19:52:44 +02:00
$this->chatNotificationService->sendChatNotification($watchList, $notification);
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
$this->statService->incrementStat('stats.alert.sent');
}
/** @var DomainStatus $domainStatus */
$domainStatus = $this->domainStatusRepository->createQueryBuilder('ds')
->select()
->where('ds.domain = :domain')
->andWhere('ds.date = :date')
2025-10-19 22:31:59 +02:00
->orderBy('ds.createdAt', 'DESC')
->setParameter('domain', $domain)
->setParameter('date', $message->updatedAt)
->getQuery()
->getOneOrNullResult();
if (null !== $domainStatus && count(array_intersect(
$watchList->getTrackedEppStatus(),
[...$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,
'username' => $watchList->getUser()->getUserIdentifier(),
]);
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
2025-10-15 19:52:44 +02:00
if ($this->influxdbEnabled) {
$this->influxdbService->addDomainNotificationPoint($domain, 'email', true);
2024-08-18 21:49:34 +02:00
}
$webhookDsn = $watchList->getWebhookDsn();
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,
'username' => $watchList->getUser()->getUserIdentifier(),
]);
$this->chatNotificationService->sendChatNotification($watchList, $notification);
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
}
}
}