mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-17 17:55:42 +00:00
wip: begin refactoring into proper API Platform
This commit is contained in:
parent
5184f2c190
commit
01c8c72fe6
@ -447,6 +447,7 @@ class WatchListController extends AbstractController
|
|||||||
{
|
{
|
||||||
/** @var WatchList $watchList */
|
/** @var WatchList $watchList */
|
||||||
$watchList = $this->serializer->deserialize($content, WatchList::class, 'json', ['groups' => $groups]);
|
$watchList = $this->serializer->deserialize($content, WatchList::class, 'json', ['groups' => $groups]);
|
||||||
|
return $watchList;
|
||||||
|
|
||||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ use ApiPlatform\Metadata\GetCollection;
|
|||||||
use ApiPlatform\Metadata\Post;
|
use ApiPlatform\Metadata\Post;
|
||||||
use ApiPlatform\Metadata\Put;
|
use ApiPlatform\Metadata\Put;
|
||||||
use App\Repository\WatchListRepository;
|
use App\Repository\WatchListRepository;
|
||||||
|
use App\State\WatchListUpdateProcessor;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
@ -78,16 +79,19 @@ use Symfony\Component\Uid\Uuid;
|
|||||||
name: 'calendar'
|
name: 'calendar'
|
||||||
),
|
),
|
||||||
new Post(
|
new Post(
|
||||||
routeName: 'watchlist_create', normalizationContext: ['groups' => 'watchlist:list'],
|
routeName: 'watchlist_create',
|
||||||
|
normalizationContext: ['groups' => 'watchlist:list'],
|
||||||
denormalizationContext: ['groups' => 'watchlist:create'],
|
denormalizationContext: ['groups' => 'watchlist:create'],
|
||||||
name: 'create'
|
name: 'create',
|
||||||
|
processor: WatchListUpdateProcessor::class,
|
||||||
),
|
),
|
||||||
new Put(
|
new Put(
|
||||||
routeName: 'watchlist_update',
|
routeName: 'watchlist_update',
|
||||||
normalizationContext: ['groups' => 'watchlist:item'],
|
normalizationContext: ['groups' => 'watchlist:item'],
|
||||||
denormalizationContext: ['groups' => ['watchlist:create', 'watchlist:token']],
|
denormalizationContext: ['groups' => ['watchlist:create', 'watchlist:token']],
|
||||||
security: 'object.user == user',
|
security: 'object.user == user',
|
||||||
name: 'update'
|
name: 'update',
|
||||||
|
processor: WatchListUpdateProcessor::class,
|
||||||
),
|
),
|
||||||
new Delete(
|
new Delete(
|
||||||
security: 'object.user == user'
|
security: 'object.user == user'
|
||||||
@ -110,7 +114,7 @@ class WatchList
|
|||||||
#[ORM\JoinTable(name: 'watch_lists_domains',
|
#[ORM\JoinTable(name: 'watch_lists_domains',
|
||||||
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')],
|
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')],
|
||||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])]
|
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])]
|
||||||
#[Groups(['watchlist:list', 'watchlist:item'])]
|
#[Groups(['watchlist:create', 'watchlist:list', 'watchlist:item'])]
|
||||||
private Collection $domains;
|
private Collection $domains;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
68
src/State/WatchListUpdateProcessor.php
Normal file
68
src/State/WatchListUpdateProcessor.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\Metadata\Post;
|
||||||
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
|
use App\Entity\Domain;
|
||||||
|
use App\Entity\WatchList;
|
||||||
|
use App\Repository\DomainRepository;
|
||||||
|
use App\Service\RDAPService;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
|
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||||
|
|
||||||
|
class WatchListUpdateProcessor implements ProcessorInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly DomainRepository $domainRepository,
|
||||||
|
private readonly RDAPService $RDAPService,
|
||||||
|
private readonly KernelInterface $kernel,
|
||||||
|
private readonly Security $security,
|
||||||
|
private readonly RateLimiterFactory $rdapRequestsLimiter,
|
||||||
|
private readonly ParameterBagInterface $parameterBag,
|
||||||
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||||
|
private readonly ProcessorInterface $persistProcessor,
|
||||||
|
)
|
||||||
|
{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param WatchList $data
|
||||||
|
* @param Operation $operation
|
||||||
|
* @param array $uriVariables
|
||||||
|
* @param array $context
|
||||||
|
* @return WatchList
|
||||||
|
*/
|
||||||
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||||
|
{
|
||||||
|
foreach ($data->getDomains() as $ldhName) {
|
||||||
|
/** @var ?Domain $domain */
|
||||||
|
$domain = $this->domainRepository->findOneBy(['ldhName' => $ldhName]);
|
||||||
|
|
||||||
|
if (null === $domain) {
|
||||||
|
$domain = $this->RDAPService->registerDomain($ldhName);
|
||||||
|
|
||||||
|
if (false === $this->kernel->isDebug() && true === $this->parameterBag->get('limited_features')) {
|
||||||
|
$limiter = $this->rdapRequestsLimiter->create($this->security->getUser()->getUserIdentifier());
|
||||||
|
$limit = $limiter->consume();
|
||||||
|
|
||||||
|
if (!$limit->isAccepted()) {
|
||||||
|
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$data->addDomain($domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($operation instanceof Post) {
|
||||||
|
$this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user