public function getBuilderForAllByUser($userId)
{
return $this
- ->getBuilderByUser($userId)
+ ->getSortedQueryBuilderByUser($userId)
;
}
public function getBuilderForUnreadByUser($userId)
{
return $this
- ->getBuilderByUser($userId)
+ ->getSortedQueryBuilderByUser($userId)
->andWhere('e.isArchived = false')
;
}
public function getBuilderForArchiveByUser($userId)
{
return $this
- ->getBuilderByUser($userId)
+ ->getSortedQueryBuilderByUser($userId)
->andWhere('e.isArchived = true')
;
}
public function getBuilderForStarredByUser($userId)
{
return $this
- ->getBuilderByUser($userId, 'starredAt', 'desc')
+ ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc')
->andWhere('e.isStarred = true')
;
}
public function getBuilderForSearchByUser($userId, $term, $currentRoute)
{
$qb = $this
- ->getBuilderByUser($userId);
+ ->getSortedQueryBuilderByUser($userId);
if ('starred' === $currentRoute) {
$qb->andWhere('e.isStarred = true');
public function getBuilderForUntaggedByUser($userId)
{
return $this
- ->getBuilderByUser($userId)
+ ->getSortedQueryBuilderByUser($userId)
->andWhere('size(e.tags) = 0');
}
*/
public function removeTag($userId, Tag $tag)
{
- $entries = $this->getBuilderByUser($userId)
+ $entries = $this->getSortedQueryBuilderByUser($userId)
->innerJoin('e.tags', 't')
->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
->getQuery()
*/
public function findAllByTagId($userId, $tagId)
{
- return $this->getBuilderByUser($userId)
+ return $this->getSortedQueryBuilderByUser($userId)
->innerJoin('e.tags', 't')
->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
->getQuery()
}
/**
- * Return a query builder to used by other getBuilderFor* method.
+ * Return a query builder to be used by other getBuilderFor* method.
+ *
+ * @param int $userId
+ *
+ * @return QueryBuilder
+ */
+ private function getQueryBuilderByUser($userId)
+ {
+ return $this->createQueryBuilder('e')
+ ->andWhere('e.user = :userId')->setParameter('userId', $userId);
+ }
+
+ /**
+ * Return a sorted query builder to be used by other getBuilderFor* method.
*
* @param int $userId
* @param string $sortBy
*
* @return QueryBuilder
*/
- private function getBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc')
+ private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc')
{
- return $this->createQueryBuilder('e')
- ->andWhere('e.user = :userId')->setParameter('userId', $userId)
+ return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId));
+ }
+
+ /**
+ * Return the given QueryBuilder with an orderBy() call
+ *
+ * @param QueryBuilder $qb
+ * @param string $sortBy
+ * @param string $direction
+ *
+ * @return QueryBuilder
+ */
+ private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
+ {
+ return $qb
->orderBy(sprintf('e.%s', $sortBy), $direction);
}
}