mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated non-dynamic query to DQL in ShortUrlRepository
This commit is contained in:
parent
8966cf9910
commit
2030401859
@ -16,12 +16,9 @@ use function key;
|
|||||||
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
|
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param int|null $limit
|
* @param string[] $tags
|
||||||
* @param int|null $offset
|
|
||||||
* @param string|null $searchTerm
|
|
||||||
* @param array $tags
|
|
||||||
* @param string|array|null $orderBy
|
* @param string|array|null $orderBy
|
||||||
* @return \Shlinkio\Shlink\Core\Entity\ShortUrl[]
|
* @return ShortUrl[]
|
||||||
*/
|
*/
|
||||||
public function findList(
|
public function findList(
|
||||||
int $limit = null,
|
int $limit = null,
|
||||||
@ -51,7 +48,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processOrderByForList(QueryBuilder $qb, $orderBy)
|
private function processOrderByForList(QueryBuilder $qb, $orderBy): array
|
||||||
{
|
{
|
||||||
// Map public field names to column names
|
// Map public field names to column names
|
||||||
$fieldNameMap = [
|
$fieldNameMap = [
|
||||||
@ -78,13 +75,6 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Counts the number of elements in a list using provided filtering data
|
|
||||||
*
|
|
||||||
* @param null|string $searchTerm
|
|
||||||
* @param array $tags
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function countList(string $searchTerm = null, array $tags = []): int
|
public function countList(string $searchTerm = null, array $tags = []): int
|
||||||
{
|
{
|
||||||
$qb = $this->createListQueryBuilder($searchTerm, $tags);
|
$qb = $this->createListQueryBuilder($searchTerm, $tags);
|
||||||
@ -93,12 +83,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||||||
return (int) $qb->getQuery()->getSingleScalarResult();
|
return (int) $qb->getQuery()->getSingleScalarResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function createListQueryBuilder(?string $searchTerm = null, array $tags = []): QueryBuilder
|
||||||
* @param null|string $searchTerm
|
|
||||||
* @param array $tags
|
|
||||||
* @return QueryBuilder
|
|
||||||
*/
|
|
||||||
protected function createListQueryBuilder(string $searchTerm = null, array $tags = []): QueryBuilder
|
|
||||||
{
|
{
|
||||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||||
$qb->from(ShortUrl::class, 's');
|
$qb->from(ShortUrl::class, 's');
|
||||||
@ -131,30 +116,25 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||||||
return $qb;
|
return $qb;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $shortCode
|
|
||||||
* @return ShortUrl|null
|
|
||||||
*/
|
|
||||||
public function findOneByShortCode(string $shortCode): ?ShortUrl
|
public function findOneByShortCode(string $shortCode): ?ShortUrl
|
||||||
{
|
{
|
||||||
$now = Chronos::now();
|
$dql= <<<DQL
|
||||||
|
SELECT s
|
||||||
|
FROM Shlinkio\Shlink\Core\Entity\ShortUrl AS s
|
||||||
|
WHERE s.shortCode = :shortCode
|
||||||
|
AND (s.validSince <= :now OR s.validSince IS NULL)
|
||||||
|
AND (s.validUntil >= :now OR s.validUntil IS NULL)
|
||||||
|
DQL;
|
||||||
|
|
||||||
$qb = $this->createQueryBuilder('s');
|
$query = $this->getEntityManager()->createQuery($dql);
|
||||||
$qb->where($qb->expr()->eq('s.shortCode', ':shortCode'))
|
$query->setMaxResults(1)
|
||||||
->setParameter('shortCode', $shortCode)
|
->setParameters([
|
||||||
->andWhere($qb->expr()->orX(
|
'shortCode' => $shortCode,
|
||||||
$qb->expr()->lte('s.validSince', ':now'),
|
'now' => Chronos::now(),
|
||||||
$qb->expr()->isNull('s.validSince')
|
]);
|
||||||
))
|
|
||||||
->andWhere($qb->expr()->orX(
|
|
||||||
$qb->expr()->gte('s.validUntil', ':now'),
|
|
||||||
$qb->expr()->isNull('s.validUntil')
|
|
||||||
))
|
|
||||||
->setParameter('now', $now)
|
|
||||||
->setMaxResults(1);
|
|
||||||
|
|
||||||
/** @var ShortUrl|null $result */
|
/** @var ShortUrl|null $result */
|
||||||
$result = $qb->getQuery()->getOneOrNullResult();
|
$result = $query->getOneOrNullResult();
|
||||||
return $result === null || $result->maxVisitsReached() ? null : $result;
|
return $result === null || $result->maxVisitsReached() ? null : $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,5 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|||||||
|
|
||||||
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface
|
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
public function findOneByShortCode(string $shortCode): ?ShortUrl;
|
||||||
* @param string $shortCode
|
|
||||||
* @return ShortUrl|null
|
|
||||||
*/
|
|
||||||
public function findOneByShortCode(string $shortCode);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user