Created method to check if domain exists based on authority and API key

This commit is contained in:
Alejandro Celaya 2022-04-23 10:58:33 +02:00
parent 984205e02c
commit 9a0e5ea626
3 changed files with 33 additions and 3 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Repository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Happyr\DoctrineSpecification\Spec;
use Shlinkio\Shlink\Core\Domain\Spec\IsDomain;
@ -40,8 +41,25 @@ class DomainRepository extends EntitySpecificationRepository implements DomainRe
public function findOneByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain
{
$qb = $this->createQueryBuilder('d');
$qb->leftJoin(ShortUrl::class, 's', Join::WITH, 's.domain = d')
$qb = $this->createDomainQueryBuilder($authority, $apiKey);
$qb->select('d');
return $qb->getQuery()->getOneOrNullResult();
}
public function domainExists(string $authority, ?ApiKey $apiKey = null): bool
{
$qb = $this->createDomainQueryBuilder($authority, $apiKey);
$qb->select('COUNT(d.id)');
return ((int) $qb->getQuery()->getSingleScalarResult()) > 0;
}
private function createDomainQueryBuilder(string $authority, ?ApiKey $apiKey): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->from(Domain::class, 'd')
->leftJoin(ShortUrl::class, 's', Join::WITH, 's.domain = d')
->where($qb->expr()->eq('d.authority', ':authority'))
->setParameter('authority', $authority)
->setMaxResults(1);
@ -51,7 +69,7 @@ class DomainRepository extends EntitySpecificationRepository implements DomainRe
$this->applySpecification($qb, $spec, $alias);
}
return $qb->getQuery()->getOneOrNullResult();
return $qb;
}
private function determineExtraSpecs(?ApiKey $apiKey): iterable

View File

@ -17,4 +17,6 @@ interface DomainRepositoryInterface extends ObjectRepository, EntitySpecificatio
public function findDomains(?ApiKey $apiKey = null): array;
public function findOneByAuthority(string $authority, ?ApiKey $apiKey = null): ?Domain;
public function domainExists(string $authority, ?ApiKey $apiKey = null): bool;
}

View File

@ -55,6 +55,10 @@ class DomainRepositoryTest extends DatabaseTestCase
self::assertEquals($detachedWithRedirects, $this->repo->findOneByAuthority('detached-with-redirects.com'));
self::assertNull($this->repo->findOneByAuthority('does-not-exist.com'));
self::assertEquals($detachedDomain, $this->repo->findOneByAuthority('detached.com'));
self::assertTrue($this->repo->domainExists('bar.com'));
self::assertTrue($this->repo->domainExists('detached-with-redirects.com'));
self::assertFalse($this->repo->domainExists('does-not-exist.com'));
self::assertTrue($this->repo->domainExists('detached.com'));
}
/** @test */
@ -115,6 +119,12 @@ class DomainRepositoryTest extends DatabaseTestCase
$this->repo->findOneByAuthority('detached-with-redirects.com', $detachedWithRedirectsApiKey),
);
self::assertNull($this->repo->findOneByAuthority('foo.com', $detachedWithRedirectsApiKey));
self::assertTrue($this->repo->domainExists('foo.com', $authorApiKey));
self::assertFalse($this->repo->domainExists('bar.com', $authorApiKey));
self::assertTrue($this->repo->domainExists('bar.com', $barDomainApiKey));
self::assertTrue($this->repo->domainExists('detached-with-redirects.com', $detachedWithRedirectsApiKey));
self::assertFalse($this->repo->domainExists('foo.com', $detachedWithRedirectsApiKey));
}
private function createShortUrl(Domain $domain, ?ApiKey $apiKey = null): ShortUrl