diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index f66c82d..38a0041 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -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 diff --git a/src/MessageHandler/OrderDomainHandler.php b/src/MessageHandler/OrderDomainHandler.php index 7aad323..57c6601 100644 --- a/src/MessageHandler/OrderDomainHandler.php +++ b/src/MessageHandler/OrderDomainHandler.php @@ -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'); diff --git a/src/MessageHandler/SendDomainEventNotifHandler.php b/src/MessageHandler/SendDomainEventNotifHandler.php index 5770ff8..481ed90 100644 --- a/src/MessageHandler/SendDomainEventNotifHandler.php +++ b/src/MessageHandler/SendDomainEventNotifHandler.php @@ -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(), - ]); - } - } - } - } - } } diff --git a/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php b/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php index b651b00..de1aecd 100644 --- a/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php +++ b/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php @@ -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)); - } } } } diff --git a/src/Service/ChatNotificationService.php b/src/Service/ChatNotificationService.php new file mode 100644 index 0000000..72024d4 --- /dev/null +++ b/src/Service/ChatNotificationService.php @@ -0,0 +1,52 @@ +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(), + ]); + } + } + } + } + } +}