]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #1386 from wallabag/v2-refactor
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index a8c138a97670b1e3f830060d58d28a50bfd2b4e7..5538ae82b40081101d2482271a7b0b290039bfbf 100644 (file)
@@ -3,91 +3,74 @@
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
-use Doctrine\ORM\Tools\Pagination\Paginator;
 use Pagerfanta\Adapter\DoctrineORMAdapter;
 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
-     * @param int $firstResult
-     * @param int $maxResults
      *
-     * @return Paginator
+     * @return QueryBuilder
      */
-    public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
+    private function getBuilderByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
+        return $this->createQueryBuilder('e')
             ->leftJoin('e.user', 'u')
-            ->where('e.isArchived = false')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
+            ->andWhere('u.id = :userId')->setParameter('userId', $userId)
             ->orderBy('e.id', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
-
-        return $paginator;
+        ;
     }
 
     /**
-     * Retrieves read entries for a user
+     * Retrieves unread entries for a user.
      *
      * @param int $userId
-     * @param int $firstResult
-     * @param int $maxResults
      *
-     * @return Paginator
+     * @return QueryBuilder
      */
-    public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
+    public function getBuilderForUnreadByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->leftJoin('e.user', 'u')
-            ->where('e.isArchived = true')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.id', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
-
-        return $paginator;
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isArchived = false')
+        ;
     }
 
     /**
-     * Retrieves starred entries for a user
+     * Retrieves read entries for a user.
      *
      * @param int $userId
-     * @param int $firstResult
-     * @param int $maxResults
      *
-     * @return Paginator
+     * @return QueryBuilder
      */
-    public function findStarredByUser($userId, $firstResult, $maxResults = 12)
+    public function getBuilderForArchiveByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->leftJoin('e.user', 'u')
-            ->where('e.isStarred = true')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.id', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isArchived = true')
+        ;
+    }
 
-        return $paginator;
+    /**
+     * Retrieves starred entries for a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    public function getBuilderForStarredByUser($userId)
+    {
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isStarred = true')
+        ;
     }
 
     /**
-     * Find Entries
+     * Find Entries.
      *
      * @param int    $userId
      * @param bool   $isArchived