From 0ab7404f93670ed161b67528c1a61c5bfa92d42b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:10:06 +0200 Subject: Refactorize the way to retrieve entries One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain. --- .../CoreBundle/Repository/EntryRepository.php | 47 ++++++++++++++-------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f885ee94..5538ae82 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -9,19 +9,34 @@ use Pagerfanta\Pagerfanta; class EntryRepository extends EntityRepository { /** - * Retrieves unread entries for a user. + * Return a query builder to used by other getBuilderFor* method. * * @param int $userId * * @return QueryBuilder */ - public function findUnreadByUser($userId) + private function getBuilderByUser($userId) { return $this->createQueryBuilder('e') ->leftJoin('e.user', 'u') - ->where('e.isArchived = false') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + ->andWhere('u.id = :userId')->setParameter('userId', $userId) + ->orderBy('e.id', 'desc') + ; + } + + /** + * Retrieves unread entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForUnreadByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = false') + ; } /** @@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findArchiveByUser($userId) + public function getBuilderForArchiveByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isArchived = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = true') + ; } /** @@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findStarredByUser($userId) + public function getBuilderForStarredByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isStarred = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isStarred = true') + ; } /** -- cgit v1.2.3