chore: refactor messages

This commit is contained in:
Maël Gangloff
2024-08-27 00:09:25 +02:00
parent 739fc7fcbf
commit b5b958dd61
5 changed files with 70 additions and 52 deletions

View File

@@ -21,11 +21,12 @@ framework:
Symfony\Component\Mailer\Messenger\SendEmailMessage: async
Symfony\Component\Notifier\Message\ChatMessage: async
Symfony\Component\Notifier\Message\SmsMessage: async
App\Message\UpdateRdapServers: async
App\Message\ProcessWatchListsTrigger: async
App\Message\ProcessWatchListTrigger: async
App\Message\ProcessDomainTrigger: async
App\Message\OrderDomain: async
App\Message\ProcessWatchListsTrigger: async
App\Message\SendDomainEventNotif: async
App\Message\UpdateDomainsFromWatchlist: async
App\Message\UpdateRdapServers: async
# Route your messages to the transports
# 'App\Message\YourMessage': async

View File

@@ -10,6 +10,7 @@ use App\Notifier\DomainOrderErrorNotification;
use App\Notifier\DomainOrderNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
use App\Service\ChatNotificationService;
use App\Service\StatService;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
@@ -37,6 +38,7 @@ final readonly class OrderDomainHandler
private MailerInterface $mailer,
private LoggerInterface $logger,
private StatService $statService,
private ChatNotificationService $chatNotificationService
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
@@ -76,7 +78,7 @@ final readonly class OrderDomainHandler
$notification = (new DomainOrderNotification($this->sender, $domain, $connector));
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
SendDomainEventNotifHandler::sendChatNotification($watchList, $notification, $this->logger);
$this->chatNotificationService->sendChatNotification($watchList, $notification);
$this->statService->incrementStat('stats.domain.purchased');
} catch (\Throwable $exception) {
@@ -86,7 +88,7 @@ final readonly class OrderDomainHandler
$notification = (new DomainOrderErrorNotification($this->sender, $domain));
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
SendDomainEventNotifHandler::sendChatNotification($watchList, $notification, $this->logger);
$this->chatNotificationService->sendChatNotification($watchList, $notification);
$this->statService->incrementStat('stats.domain.purchase.failed');

View File

@@ -3,7 +3,6 @@
namespace App\MessageHandler;
use App\Config\TriggerAction;
use App\Config\WebhookScheme;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\WatchList;
@@ -12,6 +11,7 @@ use App\Message\SendDomainEventNotif;
use App\Notifier\DomainUpdateNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
use App\Service\ChatNotificationService;
use App\Service\StatService;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
@@ -19,10 +19,7 @@ use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
#[AsMessageHandler]
final readonly class SendDomainEventNotifHandler
@@ -36,7 +33,8 @@ final readonly class SendDomainEventNotifHandler
private MailerInterface $mailer,
private StatService $statService,
private DomainRepository $domainRepository,
private WatchListRepository $watchListRepository
private WatchListRepository $watchListRepository,
private ChatNotificationService $chatNotificationService
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
@@ -72,47 +70,11 @@ final readonly class SendDomainEventNotifHandler
if (TriggerAction::SendEmail == $watchListTrigger->getAction()) {
$this->mailer->send($notification->asEmailMessage($recipient)->getMessage());
} elseif (TriggerAction::SendChat == $watchListTrigger->getAction()) {
$this->sendChatNotification($watchList, $notification, $this->logger);
$this->chatNotificationService->sendChatNotification($watchList, $notification);
}
$this->statService->incrementStat('stats.alert.sent');
}
}
}
/**
* @throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
*/
public static function sendChatNotification(WatchList $watchList, ChatNotificationInterface $notification, LoggerInterface $logger): void
{
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
$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()));
$logger->info('Chat message sent with {schema} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable) {
$logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
}
}
}
}
}
}

View File

@@ -11,6 +11,7 @@ use App\Notifier\DomainUpdateErrorNotification;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
@@ -62,6 +63,10 @@ final readonly class UpdateDomainsFromWatchlistHandler
try {
$this->RDAPService->registerDomain($domain->getLdhName());
} catch (NotFoundHttpException) {
if (null !== $watchList->getConnector()) {
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
} catch (\Throwable $e) {
$this->logger->error('An update error email is sent to user {username}.', [
'username' => $watchList->getUser()->getUserIdentifier(),
@@ -73,10 +78,6 @@ final readonly class UpdateDomainsFromWatchlistHandler
}
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
if (null !== $watchList->getConnector() && $domain->getDeleted()) {
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Service;
use App\Config\WebhookScheme;
use App\Entity\WatchList;
use Psr\Log\LoggerInterface;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
readonly class ChatNotificationService
{
public function __construct(
private LoggerInterface $logger
) {
}
public function sendChatNotification(WatchList $watchList, ChatNotificationInterface $notification): void
{
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
$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()));
$this->logger->info('Chat message sent with {schema} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable) {
$this->logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
}
}
}
}
}
}