Refactored ShortUrlRepository to make it more readable

This commit is contained in:
Alejandro Celaya 2017-10-23 12:36:03 +02:00
parent a24688b92a
commit 9577a4da4b

View file

@ -50,22 +50,14 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
$fieldName = is_array($orderBy) ? key($orderBy) : $orderBy; $fieldName = is_array($orderBy) ? key($orderBy) : $orderBy;
$order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC'; $order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC';
if (in_array($fieldName, [ if (in_array($fieldName, ['visits', 'visitsCount', 'visitCount'], true)) {
'visits',
'visitsCount',
'visitCount',
], true)) {
$qb->addSelect('COUNT(v) AS totalVisits') $qb->addSelect('COUNT(v) AS totalVisits')
->leftJoin('s.visits', 'v') ->leftJoin('s.visits', 'v')
->groupBy('s') ->groupBy('s')
->orderBy('totalVisits', $order); ->orderBy('totalVisits', $order);
return array_column($qb->getQuery()->getResult(), 0); return array_column($qb->getQuery()->getResult(), 0);
} elseif (in_array($fieldName, [ } elseif (in_array($fieldName, ['originalUrl', 'shortCode', 'dateCreated'], true)) {
'originalUrl',
'shortCode',
'dateCreated',
], true)) {
$qb->orderBy('s.' . $fieldName, $order); $qb->orderBy('s.' . $fieldName, $order);
} }