domain-watchdog/src/MessageHandler/SendDomainEventNotifHandler.php

119 lines
5.0 KiB
PHP
Raw Normal View History

<?php
namespace App\MessageHandler;
use App\Config\TriggerAction;
2024-08-16 23:23:51 +02:00
use App\Config\WebhookScheme;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\WatchList;
use App\Entity\WatchListTrigger;
2024-08-26 23:45:30 +02:00
use App\Message\SendDomainEventNotif;
2024-08-16 23:23:51 +02:00
use App\Notifier\DomainUpdateNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
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\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2024-08-16 23:23:51 +02:00
use Symfony\Component\Messenger\Exception\ExceptionInterface;
2024-08-05 01:30:27 +02:00
use Symfony\Component\Mime\Address;
2024-08-18 21:49:34 +02:00
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
2024-08-16 23:23:51 +02:00
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
2024-08-17 22:22:18 +02:00
use Symfony\Component\Notifier\Transport\Dsn;
#[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,
private WatchListRepository $watchListRepository
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-16 23:23:51 +02:00
* @throws ExceptionInterface
*/
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]);
2024-07-30 21:34:48 +02:00
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn ($event) => $message->updatedAt < $event->getDate() && $event->getDate() < new \DateTime()) as $event) {
2024-07-30 21:34:48 +02:00
$watchListTriggers = $watchList->getWatchListTriggers()
2024-08-02 23:24:52 +02:00
->filter(fn ($trigger) => $trigger->getEvent() === $event->getAction());
2024-07-29 16:10:34 +02:00
2024-07-30 21:34:48 +02:00
/** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
2024-08-04 14:45:27 +02:00
$this->logger->info('Action {event} has been detected on the domain name {ldhName}. A notification is sent to user {username}.', [
'event' => $event->getAction(),
'ldhName' => $message->ldhName,
'username' => $watchList->getUser()->getUserIdentifier(),
]);
2024-08-16 23:23:51 +02:00
$recipient = new Recipient($watchList->getUser()->getEmail());
$notification = new DomainUpdateNotification($this->sender, $event);
2024-08-02 23:24:52 +02:00
if (TriggerAction::SendEmail == $watchListTrigger->getAction()) {
2024-08-16 23:23:51 +02:00
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
} elseif (TriggerAction::SendChat == $watchListTrigger->getAction()) {
2024-08-26 23:45:30 +02:00
$this->sendChatNotification($watchList, $notification, $this->logger);
2024-08-18 21:49:34 +02:00
}
2024-08-22 18:11:07 +02:00
2024-08-25 03:31:09 +02:00
$this->statService->incrementStat('stats.alert.sent');
2024-08-18 21:49:34 +02:00
}
}
}
/**
* @throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
*/
2024-08-26 23:45:30 +02:00
public static function sendChatNotification(WatchList $watchList, ChatNotificationInterface $notification, LoggerInterface $logger): void
2024-08-18 21:49:34 +02:00
{
2024-08-25 03:55:20 +02:00
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
2024-08-18 21:49:34 +02:00
$dsn = new Dsn($dsnString);
$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);
if (null !== $webhookScheme) {
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
try {
$transportFactory->create($dsn)->send($notification->asChatMessage(new Recipient()));
2024-08-26 23:45:30 +02:00
$logger->info('Chat message sent with {schema} for Watchlist {token}',
2024-08-18 21:49:34 +02:00
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable) {
2024-08-26 23:45:30 +02:00
$logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
2024-08-18 21:49:34 +02:00
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
2024-08-16 23:23:51 +02:00
}
}
}
}
}
}