refactor: create StatService

This commit is contained in:
Maël Gangloff
2024-08-25 03:31:09 +02:00
parent 4bb3ba7765
commit 0e1658f6fe
4 changed files with 35 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\Get;
use App\Controller\StatisticsController; use App\Controller\StatisticsController;
use Psr\Cache\CacheItemPoolInterface;
#[ApiResource( #[ApiResource(
operations: [ operations: [
@@ -28,19 +27,6 @@ class Statistics
private ?int $domainTracked = null; private ?int $domainTracked = null;
private ?int $domainCountTotal = 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 public function getRdapQueries(): ?int
{ {
return $this->rdapQueries; return $this->rdapQueries;

View File

@@ -7,7 +7,6 @@ use App\Config\TriggerAction;
use App\Config\WebhookScheme; use App\Config\WebhookScheme;
use App\Entity\Domain; use App\Entity\Domain;
use App\Entity\DomainEvent; use App\Entity\DomainEvent;
use App\Entity\Statistics;
use App\Entity\WatchList; use App\Entity\WatchList;
use App\Entity\WatchListTrigger; use App\Entity\WatchListTrigger;
use App\Message\ProcessDomainTrigger; use App\Message\ProcessDomainTrigger;
@@ -16,6 +15,7 @@ use App\Notifier\DomainOrderNotification;
use App\Notifier\DomainUpdateNotification; use App\Notifier\DomainUpdateNotification;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use App\Repository\WatchListRepository; use App\Repository\WatchListRepository;
use App\Service\StatService;
use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
@@ -44,7 +44,8 @@ final readonly class ProcessDomainTriggerHandler
private LoggerInterface $logger, private LoggerInterface $logger,
private HttpClientInterface $client, private HttpClientInterface $client,
private MailerInterface $mailer, private MailerInterface $mailer,
private CacheItemPoolInterface $cacheItemPool private CacheItemPoolInterface $cacheItemPool,
private StatService $statService
) { ) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName); $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->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->sendChatNotification($watchList, $notification); $this->sendChatNotification($watchList, $notification);
Statistics::updateRDAPQueriesStat($this->cacheItemPool, 'stats.domain.purchased'); $this->statService->incrementStat('stats.domain.purchased');
} catch (\Throwable) { } catch (\Throwable) {
$this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [ $this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [
'username' => $watchList->getUser()->getUserIdentifier(), 'username' => $watchList->getUser()->getUserIdentifier(),
@@ -96,7 +97,7 @@ final readonly class ProcessDomainTriggerHandler
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->sendChatNotification($watchList, $notification); $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); $this->sendChatNotification($watchList, $notification);
} }
Statistics::updateRDAPQueriesStat($this->cacheItemPool, 'stats.alert.sent'); $this->statService->incrementStat('stats.alert.sent');
} }
} }
} }

View File

@@ -12,7 +12,6 @@ use App\Entity\EntityEvent;
use App\Entity\Nameserver; use App\Entity\Nameserver;
use App\Entity\NameserverEntity; use App\Entity\NameserverEntity;
use App\Entity\RdapServer; use App\Entity\RdapServer;
use App\Entity\Statistics;
use App\Entity\Tld; use App\Entity\Tld;
use App\Repository\DomainEntityRepository; use App\Repository\DomainEntityRepository;
use App\Repository\DomainEventRepository; use App\Repository\DomainEventRepository;
@@ -25,7 +24,6 @@ use App\Repository\RdapServerRepository;
use App\Repository\TldRepository; use App\Repository\TldRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\Exception\ORMException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpFoundation\Exception\BadRequestException;
@@ -91,7 +89,7 @@ readonly class RDAPService
private TldRepository $tldRepository, private TldRepository $tldRepository,
private EntityManagerInterface $em, private EntityManagerInterface $em,
private LoggerInterface $logger, 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 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; return false;
} }
@@ -171,7 +169,7 @@ readonly class RDAPService
]); ]);
try { try {
Statistics::updateRDAPQueriesStat($this->pool, 'stats.rdap_queries.count'); $this->statService->incrementStat('stats.rdap_queries.count');
$res = $this->client->request( $res = $this->client->request(
'GET', $rdapServerUrl.'domain/'.$idnDomain 'GET', $rdapServerUrl.'domain/'.$idnDomain

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Service;
use Psr\Cache\CacheItemPoolInterface;
readonly class StatService
{
public function __construct(
private CacheItemPoolInterface $pool
) {
}
public function incrementStat(string $key): bool
{
try {
$item = $this->pool->getItem($key);
$item->set(($item->get() ?? 0) + 1);
return $this->pool->save($item);
} catch (\Throwable) {
}
return false;
}
}