mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Created method to check if domain exists based on authority and API key
This commit is contained in:
parent
984205e02c
commit
9a0e5ea626
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user