feat: add SendNotifWatchListTrigger Message

This commit is contained in:
Maël Gangloff 2024-07-21 00:56:27 +02:00
parent 86aa1d2f3f
commit 17a1cc2938
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
8 changed files with 115 additions and 8 deletions

View File

View File

View File

@ -1,7 +0,0 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
const rootElement = document.getElementById('root');
const root = ReactDOM.createRoot(rootElement!);
root.render(<></>)

View File

@ -25,6 +25,7 @@ framework:
Symfony\Component\Notifier\Message\ChatMessage: async
Symfony\Component\Notifier\Message\SmsMessage: async
App\Message\UpdateRdapServers: async
App\Message\SendNotifWatchListTrigger: async
# Route your messages to the transports
# 'App\Message\YourMessage': async

View File

@ -0,0 +1,16 @@
<?php
namespace App\Message;
final class SendNotifWatchListTrigger
{
/*
* Add whatever properties and methods you need
* to hold the data for this message class.
*/
// public function __construct(
// public readonly string $name,
// ) {
// }
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\MessageHandler;
use App\Config\TriggerAction;
use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\WatchList;
use App\Entity\WatchListTrigger;
use App\Message\SendNotifWatchListTrigger;
use App\Repository\WatchListRepository;
use App\Service\RDAPService;
use DateTimeImmutable;
use Exception;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Throwable;
#[AsMessageHandler]
final class SendNotifWatchListTriggerHandler
{
public function __construct(
private WatchListRepository $watchListRepository,
private RDAPService $RDAPService
)
{
}
/**
* @throws Exception
*/
public function __invoke(SendNotifWatchListTrigger $message): void
{
/** @var WatchList $watchList */
foreach ($this->watchListRepository->findAll() as $watchList) {
/** @var Domain $domain */
foreach ($watchList->getDomains()
->filter(fn($domain) => $domain->getUpdatedAt()
->diff(new DateTimeImmutable('now'))->days >= 7) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
try {
$domain = $this->RDAPService->registerDomain($domain->getLdhName());
} catch (Throwable) {
}
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $updatedAt < $event->getDate()) as $event) {
$watchListTriggers = $watchList->getWatchListTriggers()
->filter(fn($trigger) => $trigger->getAction() === $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');
}
}
}
}
}
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Scheduler;
use App\Message\SendNotifWatchListTrigger;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;
#[AsSchedule('notif_watchlist')]
final readonly class SendNotifWatchListTriggerSchedule implements ScheduleProviderInterface
{
public function __construct(
private CacheInterface $cache,
) {
}
public function getSchedule(): Schedule
{
return (new Schedule())
->add(
RecurringMessage::every('1 week', new SendNotifWatchListTrigger()),
)
->stateful($this->cache)
;
}
}

View File

@ -9,7 +9,7 @@ use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;
#[AsSchedule]
#[AsSchedule('rdap_udpate')]
final readonly class UpdateRdapServersSchedule implements ScheduleProviderInterface
{
public function __construct(