]> 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 a8085ac9cbeb01bdc4c7bb6331ef1b72f6380765..2286317c177097f53ff8cf389e1dd05a72dfc0a5 100644 (file)
@@ -3,89 +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)
-            ->orderBy('e.createdAt', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
-
-        return $paginator;
+            ->andWhere('u.id = :userId')->setParameter('userId', $userId)
+            ->orderBy('e.id', 'desc')
+        ;
     }
 
     /**
-     * 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.createdAt', '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.createdAt', '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
@@ -109,14 +108,14 @@ class EntryRepository extends EntityRepository
         }
 
         if ('created' === $sort) {
-            $qb->orderBy('e.createdAt', $order);
+            $qb->orderBy('e.id', $order);
         } elseif ('updated' === $sort) {
             $qb->orderBy('e.updatedAt', $order);
         }
 
-        return $qb
-            ->getQuery()
-            ->getResult();
+        $pagerAdapter = new DoctrineORMAdapter($qb);
+
+        return new Pagerfanta($pagerAdapter);
     }
 
     /**
@@ -128,13 +127,56 @@ class EntryRepository extends EntityRepository
     {
         $qb = $this->createQueryBuilder('e')
             ->innerJoin('e.tags', 't')
-            ->addSelect('t')
-            ->where('t.user=:userId')->setParameter('userId', 1);
+            ->innerJoin('e.user', 'u')
+            ->addSelect('t', 'u')
+            ->where('e.user=:userId')->setParameter('userId', $userId)
+        ;
+
+        return $qb->getQuery()->getResult();
+    }
 
-        return $qb->getQuery()->getOneOrNullResult();
+    /**
+     * 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();
 
-        return $qb
+        $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()
-            ->getOneOrNullResult();
+            ->getSingleResult();
     }
 }