mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: send an email when a domain has been modified
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\MessageHandler;
|
||||
use App\Config\TriggerAction;
|
||||
use App\Entity\Domain;
|
||||
use App\Entity\DomainEvent;
|
||||
use App\Entity\User;
|
||||
use App\Entity\WatchList;
|
||||
use App\Entity\WatchListTrigger;
|
||||
use App\Message\SendNotifWatchListTrigger;
|
||||
@@ -12,22 +13,29 @@ use App\Repository\WatchListRepository;
|
||||
use App\Service\RDAPService;
|
||||
use DateTimeImmutable;
|
||||
use Exception;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Throwable;
|
||||
|
||||
#[AsMessageHandler]
|
||||
final class SendNotifWatchListTriggerHandler
|
||||
readonly final class SendNotifWatchListTriggerHandler
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private WatchListRepository $watchListRepository,
|
||||
private RDAPService $RDAPService
|
||||
private RDAPService $RDAPService,
|
||||
private MailerInterface $mailer,
|
||||
private string $mailerSenderEmail
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function __invoke(SendNotifWatchListTrigger $message): void
|
||||
{
|
||||
@@ -40,24 +48,26 @@ final class SendNotifWatchListTriggerHandler
|
||||
->diff(new DateTimeImmutable('now'))->days >= 7) as $domain
|
||||
) {
|
||||
$updatedAt = $domain->getUpdatedAt();
|
||||
|
||||
try {
|
||||
$domain = $this->RDAPService->registerDomain($domain->getLdhName());
|
||||
} catch (Throwable) {
|
||||
|
||||
$this->sendEmailDomainUpdateError($domain, $watchList->getUser());
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var DomainEvent $event */
|
||||
foreach ($domain->getEvents()->filter(fn($event) => $updatedAt < $event->getDate()) as $event) {
|
||||
|
||||
$watchListTriggers = $watchList->getWatchListTriggers()
|
||||
->filter(fn($trigger) => $trigger->getAction() === $event->getAction());
|
||||
->filter(fn($trigger) => $trigger->getEvent() === $event->getAction());
|
||||
|
||||
/** @var WatchListTrigger $watchListTrigger */
|
||||
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
|
||||
|
||||
switch ($watchListTrigger->getAction()) {
|
||||
case TriggerAction::SendEmail:
|
||||
//TODO: To be implemented
|
||||
throw new Exception('To be implemented');
|
||||
$this->sendEmailDomainUpdated($event, $watchList->getUser());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,4 +75,44 @@ final class SendNotifWatchListTriggerHandler
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function sendEmailDomainUpdateError(Domain $domain, User $user): Email
|
||||
{
|
||||
$email = (new TemplatedEmail())
|
||||
->from($this->mailerSenderEmail)
|
||||
->to($user->getEmail())
|
||||
->subject('An error occurred while updating a domain name')
|
||||
->htmlTemplate('emails/errors/domain_update.html.twig')
|
||||
->locale('en')
|
||||
->context([
|
||||
"domain" => $domain
|
||||
]);
|
||||
|
||||
$this->mailer->send($email);
|
||||
return $email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function sendEmailDomainUpdated(DomainEvent $domainEvent, User $user): Email
|
||||
{
|
||||
$email = (new TemplatedEmail())
|
||||
->from($this->mailerSenderEmail)
|
||||
->to($user->getEmail())
|
||||
->priority(Email::PRIORITY_HIGHEST)
|
||||
->subject('A domain name has been changed')
|
||||
->htmlTemplate('emails/domain_updated.html.twig')
|
||||
->locale('en')
|
||||
->context([
|
||||
"event" => $domainEvent
|
||||
]);
|
||||
|
||||
$this->mailer->send($email);
|
||||
return $email;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user