refactor: merge dql

This commit is contained in:
Maël Gangloff
2025-11-08 20:24:37 +01:00
parent 8b03c54a16
commit c320311e92

View File

@@ -5,16 +5,15 @@ namespace App\State;
use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface; use ApiPlatform\State\ProviderInterface;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use App\Repository\EntityRepository;
use App\Service\RDAPService; use App\Service\RDAPService;
use Doctrine\ORM\Query\Expr\Join;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
readonly class FindDomainCollectionFromEntityProvider implements ProviderInterface readonly class FindDomainCollectionFromEntityProvider implements ProviderInterface
{ {
public function __construct( public function __construct(
private RequestStack $requestStack, private RequestStack $requestStack,
private EntityRepository $entityRepository,
private DomainRepository $domainRepository, private DomainRepository $domainRepository,
) { ) {
} }
@@ -36,30 +35,21 @@ readonly class FindDomainCollectionFromEntityProvider implements ProviderInterfa
foreach ($forbidden as $word) { foreach ($forbidden as $word) {
if (str_contains(strtolower($registrant), $word)) { if (str_contains(strtolower($registrant), $word)) {
throw new HttpException(403, 'Forbidden search term'); throw new BadRequestHttpException('Forbidden search term');
} }
} }
$entities = $this->entityRepository->createQueryBuilder('e')
->where('e.tld IS NOT NULL')
->andWhere('e.handle NOT IN (:blacklist)')
->andWhere('UPPER(e.jCardOrg) = UPPER(:registrant) OR UPPER(e.jCardFn) = UPPER(:registrant)')
->setParameter('registrant', $registrant)
->setParameter('blacklist', RDAPService::ENTITY_HANDLE_BLACKLIST)
->getQuery()
->getResult();
if (empty($entities)) {
return [];
}
return $this->domainRepository->createQueryBuilder('d') return $this->domainRepository->createQueryBuilder('d')
->select('DISTINCT d') ->select('DISTINCT d')
->join('d.domainEntities', 'de') ->join('d.domainEntities', 'de', Join::WITH, 'de.deletedAt IS NULL AND JSONB_CONTAINS(de.roles, :role) = true')
->where('de.entity IN (:entityIds)') ->join(
->andWhere('JSONB_CONTAINS(de.roles, :role) = true') 'de.entity',
->andWhere('de.deletedAt IS NULL') 'e',
->setParameter('entityIds', array_map(fn ($e) => $e->getId(), $entities)) Join::WITH,
'e.tld IS NOT NULL AND e.handle NOT IN (:blacklist) AND (UPPER(e.jCardOrg) = UPPER(:registrant) OR UPPER(e.jCardFn) = UPPER(:registrant))'
)
->setParameter('registrant', $registrant)
->setParameter('blacklist', RDAPService::ENTITY_HANDLE_BLACKLIST)
->setParameter('role', '"registrant"') ->setParameter('role', '"registrant"')
->getQuery()->getResult(); ->getQuery()->getResult();
} }