]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Fix tests for all
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index a8c138a97670b1e3f830060d58d28a50bfd2b4e7..2286317c177097f53ff8cf389e1dd05a72dfc0a5 100644 (file)
@@ -3,91 +3,88 @@
 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 all 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 getBuilderForAllByUser($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)
+        ;
     }
 
     /**
-     * Retrieves starred 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 findStarredByUser($userId, $firstResult, $maxResults = 12)
+    public function getBuilderForUnreadByUser($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();
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isArchived = false')
+        ;
+    }
 
-        $paginator = new Paginator($qb);
+    /**
+     * Retrieves read entries for a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    public function getBuilderForArchiveByUser($userId)
+    {
+        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
@@ -137,4 +134,49 @@ class EntryRepository extends EntityRepository
 
         return $qb->getQuery()->getResult();
     }
+
+    /**
+     * Find distinct language for a given user.
+     * Used to build the filter language list.
+     *
+     * @param int $userId User id
+     *
+     * @return array
+     */
+    public function findDistinctLanguageByUser($userId)
+    {
+        $results = $this->createQueryBuilder('e')
+            ->select('e.language')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->andWhere('e.language IS NOT NULL')
+            ->groupBy('e.language')
+            ->orderBy('e.language', ' ASC')
+            ->getQuery()
+            ->getResult();
+
+        $languages = array();
+        foreach ($results as $result) {
+            $languages[$result['language']] = $result['language'];
+        }
+
+        return $languages;
+    }
+
+    /**
+     * Used only in test case to get the right entry associated to the right user
+     *
+     * @param  string $username
+     *
+     * @return Entry
+     */
+    public function findOneByUsernameAndNotArchived($username)
+    {
+        return $this->createQueryBuilder('e')
+            ->leftJoin('e.user', 'u')
+            ->where('u.username = :username')->setParameter('username', $username)
+            ->andWhere('e.isArchived = false')
+            ->setMaxResults(1)
+            ->getQuery()
+            ->getSingleResult();
+    }
 }