mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-20 19:25:40 +00:00
refactor: use Providers
This commit is contained in:
parent
0f06af937f
commit
9466b4985c
@ -1,29 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Controller;
|
|
||||||
|
|
||||||
use App\Entity\Connector;
|
|
||||||
use App\Entity\User;
|
|
||||||
use Doctrine\Common\Collections\Collection;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
|
||||||
|
|
||||||
class ConnectorController extends AbstractController
|
|
||||||
{
|
|
||||||
#[Route(
|
|
||||||
path: '/api/connectors',
|
|
||||||
name: 'connector_get_all_mine',
|
|
||||||
defaults: [
|
|
||||||
'_api_resource_class' => Connector::class,
|
|
||||||
'_api_operation_name' => 'get_all_mine',
|
|
||||||
],
|
|
||||||
methods: ['GET']
|
|
||||||
)]
|
|
||||||
public function getConnector(): Collection
|
|
||||||
{
|
|
||||||
/** @var User $user */
|
|
||||||
$user = $this->getUser();
|
|
||||||
|
|
||||||
return $user->getConnectors();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -5,12 +5,9 @@ namespace App\Controller;
|
|||||||
use App\Entity\Domain;
|
use App\Entity\Domain;
|
||||||
use App\Entity\DomainEvent;
|
use App\Entity\DomainEvent;
|
||||||
use App\Entity\DomainStatus;
|
use App\Entity\DomainStatus;
|
||||||
use App\Entity\User;
|
|
||||||
use App\Entity\Watchlist;
|
use App\Entity\Watchlist;
|
||||||
use App\Repository\DomainRepository;
|
|
||||||
use App\Repository\WatchlistRepository;
|
use App\Repository\WatchlistRepository;
|
||||||
use App\Service\CalendarService;
|
use App\Service\CalendarService;
|
||||||
use App\Service\RDAPService;
|
|
||||||
use Eluceo\iCal\Domain\Entity\Calendar;
|
use Eluceo\iCal\Domain\Entity\Calendar;
|
||||||
use Eluceo\iCal\Presentation\Component\Property;
|
use Eluceo\iCal\Presentation\Component\Property;
|
||||||
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
|
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
|
||||||
@ -29,9 +26,7 @@ class WatchlistController extends AbstractController
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly WatchlistRepository $watchlistRepository,
|
private readonly WatchlistRepository $watchlistRepository,
|
||||||
private readonly RDAPService $RDAPService,
|
|
||||||
private readonly CalendarService $calendarService,
|
private readonly CalendarService $calendarService,
|
||||||
private readonly DomainRepository $domainRepository,
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,32 +69,6 @@ class WatchlistController extends AbstractController
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
#[Route(
|
|
||||||
path: '/api/tracked',
|
|
||||||
name: 'watchlist_get_tracked_domains',
|
|
||||||
defaults: [
|
|
||||||
'_api_resource_class' => Watchlist::class,
|
|
||||||
'_api_operation_name' => 'get_tracked_domains',
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
public function getTrackedDomains(): array
|
|
||||||
{
|
|
||||||
/** @var User $user */
|
|
||||||
$user = $this->getUser();
|
|
||||||
|
|
||||||
$domains = $this->domainRepository->getMyTrackedDomains($user);
|
|
||||||
foreach ($domains as $domain) {
|
|
||||||
$domain->setExpiresInDays($this->RDAPService->getExpiresInDays($domain));
|
|
||||||
}
|
|
||||||
|
|
||||||
usort($domains, fn (Domain $d1, Domain $d2) => $d1->getExpiresInDays() - $d2->getExpiresInDays());
|
|
||||||
|
|
||||||
return $domains;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -11,6 +11,7 @@ use App\Config\ConnectorProvider;
|
|||||||
use App\Repository\ConnectorRepository;
|
use App\Repository\ConnectorRepository;
|
||||||
use App\State\ConnectorCreateProcessor;
|
use App\State\ConnectorCreateProcessor;
|
||||||
use App\State\ConnectorDeleteProcessor;
|
use App\State\ConnectorDeleteProcessor;
|
||||||
|
use App\State\MyConnectorsProvider;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
@ -21,9 +22,8 @@ use Symfony\Component\Uid\Uuid;
|
|||||||
shortName: 'Connector',
|
shortName: 'Connector',
|
||||||
operations: [
|
operations: [
|
||||||
new GetCollection(
|
new GetCollection(
|
||||||
routeName: 'connector_get_all_mine',
|
|
||||||
normalizationContext: ['groups' => 'connector:list'],
|
normalizationContext: ['groups' => 'connector:list'],
|
||||||
name: 'get_all_mine',
|
provider: MyConnectorsProvider::class
|
||||||
),
|
),
|
||||||
new Get(
|
new Get(
|
||||||
normalizationContext: ['groups' => 'connector:list'],
|
normalizationContext: ['groups' => 'connector:list'],
|
||||||
|
|||||||
@ -23,15 +23,6 @@ use Symfony\Component\Serializer\Attribute\SerializedName;
|
|||||||
#[ORM\Entity(repositoryClass: DomainRepository::class)]
|
#[ORM\Entity(repositoryClass: DomainRepository::class)]
|
||||||
#[ApiResource(
|
#[ApiResource(
|
||||||
operations: [
|
operations: [
|
||||||
/*
|
|
||||||
new GetCollection(
|
|
||||||
normalizationContext: [
|
|
||||||
'groups' => [
|
|
||||||
'domain:list'
|
|
||||||
]
|
|
||||||
]
|
|
||||||
),
|
|
||||||
*/
|
|
||||||
new GetCollection(
|
new GetCollection(
|
||||||
uriTemplate: '/domains',
|
uriTemplate: '/domains',
|
||||||
normalizationContext: [
|
normalizationContext: [
|
||||||
|
|||||||
@ -12,7 +12,9 @@ use App\Controller\InstanceController;
|
|||||||
uriTemplate: '/config',
|
uriTemplate: '/config',
|
||||||
controller: InstanceController::class,
|
controller: InstanceController::class,
|
||||||
shortName: 'Configuration',
|
shortName: 'Configuration',
|
||||||
|
description: 'Public configuration of the server',
|
||||||
read: false,
|
read: false,
|
||||||
|
name: 'server-configuration',
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)]
|
)]
|
||||||
|
|||||||
@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Patch;
|
|||||||
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\MyTrackedDomainProvider;
|
||||||
use App\State\MyWatchlistsProvider;
|
use App\State\MyWatchlistsProvider;
|
||||||
use App\State\WatchlistUpdateProcessor;
|
use App\State\WatchlistUpdateProcessor;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
@ -33,12 +34,10 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
'event:list',
|
'event:list',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
name: 'get_all_mine',
|
|
||||||
provider: MyWatchlistsProvider::class,
|
provider: MyWatchlistsProvider::class,
|
||||||
),
|
),
|
||||||
new GetCollection(
|
new GetCollection(
|
||||||
uriTemplate: '/tracked',
|
uriTemplate: '/tracked',
|
||||||
routeName: 'watchlist_get_tracked_domains',
|
|
||||||
normalizationContext: [
|
normalizationContext: [
|
||||||
'groups' => [
|
'groups' => [
|
||||||
'domain:list',
|
'domain:list',
|
||||||
@ -47,7 +46,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
'event:list',
|
'event:list',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
name: 'get_tracked_domains'
|
provider: MyTrackedDomainProvider::class
|
||||||
),
|
),
|
||||||
new Get(
|
new Get(
|
||||||
normalizationContext: [
|
normalizationContext: [
|
||||||
|
|||||||
23
src/State/MyConnectorsProvider.php
Normal file
23
src/State/MyConnectorsProvider.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\Entity\User;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
|
||||||
|
readonly class MyConnectorsProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(private Security $security)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||||
|
{
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $this->security->getUser();
|
||||||
|
|
||||||
|
return $user->getConnectors();
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/State/MyTrackedDomainProvider.php
Normal file
36
src/State/MyTrackedDomainProvider.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\Entity\Domain;
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Repository\DomainRepository;
|
||||||
|
use App\Service\RDAPService;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
|
||||||
|
readonly class MyTrackedDomainProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private Security $security,
|
||||||
|
private DomainRepository $domainRepository,
|
||||||
|
private RDAPService $RDAPService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||||
|
{
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $this->security->getUser();
|
||||||
|
|
||||||
|
$domains = $this->domainRepository->getMyTrackedDomains($user);
|
||||||
|
foreach ($domains as $domain) {
|
||||||
|
$domain->setExpiresInDays($this->RDAPService->getExpiresInDays($domain));
|
||||||
|
}
|
||||||
|
|
||||||
|
usort($domains, fn (Domain $d1, Domain $d2) => $d1->getExpiresInDays() - $d2->getExpiresInDays());
|
||||||
|
|
||||||
|
return $domains;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user