Ensured domain is taken into account when checking if a slug is in use

This commit is contained in:
Alejandro Celaya
2019-10-01 21:42:35 +02:00
parent 8da6b336f5
commit 495643f4f1
9 changed files with 130 additions and 36 deletions

View File

@@ -138,4 +138,25 @@ DQL;
$result = $query->getOneOrNullResult();
return $result === null || $result->maxVisitsReached() ? null : $result;
}
public function slugIsInUse(string $slug, ?string $domain = null): bool
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(DISTINCT s.id)')
->from(ShortUrl::class, 's')
->where($qb->expr()->isNotNull('s.shortCode'))
->andWhere($qb->expr()->eq('s.shortCode', ':slug'))
->setParameter('slug', $slug);
if ($domain !== null) {
$qb->join('s.domain', 'd')
->andWhere($qb->expr()->eq('d.authority', ':authority'))
->setParameter('authority', $domain);
} else {
$qb->andWhere($qb->expr()->isNull('s.domain'));
}
$result = (int) $qb->getQuery()->getSingleScalarResult();
return $result > 0;
}
}

View File

@@ -27,4 +27,6 @@ interface ShortUrlRepositoryInterface extends ObjectRepository
public function countList(?string $searchTerm = null, array $tags = []): int;
public function findOneByShortCode(string $shortCode): ?ShortUrl;
public function slugIsInUse(string $slug, ?string $domain): bool;
}