diff --git a/src/Entity/Statistics.php b/src/Entity/Statistics.php index 0106f53..d9eefd9 100644 --- a/src/Entity/Statistics.php +++ b/src/Entity/Statistics.php @@ -5,7 +5,6 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\Get; use App\Controller\StatisticsController; -use Psr\Cache\CacheItemPoolInterface; #[ApiResource( operations: [ @@ -28,19 +27,6 @@ class Statistics private ?int $domainTracked = null; private ?int $domainCountTotal = null; - public static function updateRDAPQueriesStat(CacheItemPoolInterface $pool, string $key): bool - { - try { - $item = $pool->getItem($key); - $item->set(($item->get() ?? 0) + 1); - - return $pool->save($item); - } catch (\Throwable) { - } - - return false; - } - public function getRdapQueries(): ?int { return $this->rdapQueries; diff --git a/src/MessageHandler/ProcessDomainTriggerHandler.php b/src/MessageHandler/ProcessDomainTriggerHandler.php index 8bbbf71..ff67db6 100644 --- a/src/MessageHandler/ProcessDomainTriggerHandler.php +++ b/src/MessageHandler/ProcessDomainTriggerHandler.php @@ -7,7 +7,6 @@ use App\Config\TriggerAction; use App\Config\WebhookScheme; use App\Entity\Domain; use App\Entity\DomainEvent; -use App\Entity\Statistics; use App\Entity\WatchList; use App\Entity\WatchListTrigger; use App\Message\ProcessDomainTrigger; @@ -16,6 +15,7 @@ use App\Notifier\DomainOrderNotification; use App\Notifier\DomainUpdateNotification; use App\Repository\DomainRepository; use App\Repository\WatchListRepository; +use App\Service\StatService; use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\KernelInterface; @@ -44,7 +44,8 @@ final readonly class ProcessDomainTriggerHandler private LoggerInterface $logger, private HttpClientInterface $client, private MailerInterface $mailer, - private CacheItemPoolInterface $cacheItemPool + private CacheItemPoolInterface $cacheItemPool, + private StatService $statService ) { $this->sender = new Address($mailerSenderEmail, $mailerSenderName); } @@ -86,7 +87,7 @@ final readonly class ProcessDomainTriggerHandler $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); $this->sendChatNotification($watchList, $notification); - Statistics::updateRDAPQueriesStat($this->cacheItemPool, 'stats.domain.purchased'); + $this->statService->incrementStat('stats.domain.purchased'); } catch (\Throwable) { $this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [ 'username' => $watchList->getUser()->getUserIdentifier(), @@ -96,7 +97,7 @@ final readonly class ProcessDomainTriggerHandler $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); $this->sendChatNotification($watchList, $notification); - Statistics::updateRDAPQueriesStat($this->cacheItemPool, 'stats.domain.purchase.failed'); + $this->statService->incrementStat('stats.domain.purchase.failed'); } } @@ -122,7 +123,7 @@ final readonly class ProcessDomainTriggerHandler $this->sendChatNotification($watchList, $notification); } - Statistics::updateRDAPQueriesStat($this->cacheItemPool, 'stats.alert.sent'); + $this->statService->incrementStat('stats.alert.sent'); } } } diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index 012ee1b..78eff45 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -12,7 +12,6 @@ use App\Entity\EntityEvent; use App\Entity\Nameserver; use App\Entity\NameserverEntity; use App\Entity\RdapServer; -use App\Entity\Statistics; use App\Entity\Tld; use App\Repository\DomainEntityRepository; use App\Repository\DomainEventRepository; @@ -25,7 +24,6 @@ use App\Repository\RdapServerRepository; use App\Repository\TldRepository; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Exception\ORMException; -use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpFoundation\Exception\BadRequestException; @@ -91,7 +89,7 @@ readonly class RDAPService private TldRepository $tldRepository, private EntityManagerInterface $em, private LoggerInterface $logger, - private CacheItemPoolInterface $pool + private StatService $statService ) { } @@ -103,7 +101,7 @@ readonly class RDAPService */ public static function isToBeWatchClosely(Domain $domain, \DateTimeImmutable $updatedAt): bool { - if ($updatedAt->diff(new \DateTimeImmutable('now'))->days < 1) { + if ($updatedAt->diff(new \DateTimeImmutable('now'))->h < 23) { return false; } @@ -171,7 +169,7 @@ readonly class RDAPService ]); try { - Statistics::updateRDAPQueriesStat($this->pool, 'stats.rdap_queries.count'); + $this->statService->incrementStat('stats.rdap_queries.count'); $res = $this->client->request( 'GET', $rdapServerUrl.'domain/'.$idnDomain diff --git a/src/Service/StatService.php b/src/Service/StatService.php new file mode 100644 index 0000000..336a415 --- /dev/null +++ b/src/Service/StatService.php @@ -0,0 +1,26 @@ +pool->getItem($key); + $item->set(($item->get() ?? 0) + 1); + + return $this->pool->save($item); + } catch (\Throwable) { + } + + return false; + } +}