]> 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 f4c803f991c2e38fb41d510aa5f44ff3e197e1ae..2286317c177097f53ff8cf389e1dd05a72dfc0a5 100644 (file)
 
 namespace Wallabag\CoreBundle\Repository;
 
-use Doctrine\ORM\Query;
 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 $userId
-     * @param $firstResult
-     * @param  int       $maxResults
-     * @return Paginator
+     * @param int $userId
+     *
+     * @return QueryBuilder
      */
-    public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
+    private function getBuilderByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->where('e.isArchived = false')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->orderBy('e.createdAt', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
-
-        return $paginator;
+        return $this->createQueryBuilder('e')
+            ->leftJoin('e.user', 'u')
+            ->andWhere('u.id = :userId')->setParameter('userId', $userId)
+            ->orderBy('e.id', 'desc')
+        ;
     }
 
     /**
-     * Retrieves read entries for a user
+     * Retrieves all entries for a user.
      *
-     * @param $userId
-     * @param $firstResult
-     * @param  int       $maxResults
-     * @return Paginator
+     * @param int $userId
+     *
+     * @return QueryBuilder
      */
-    public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
+    public function getBuilderForAllByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->where('e.isArchived = true')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->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 $userId
-     * @param $firstResult
-     * @param  int       $maxResults
-     * @return Paginator
+     * @param int $userId
+     *
+     * @return QueryBuilder
      */
-    public function findStarredByUser($userId, $firstResult, $maxResults = 12)
+    public function getBuilderForUnreadByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->where('e.isStarred = true')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->orderBy('e.createdAt', 'desc')
-            ->getQuery();
-
-        $paginator = new Paginator($qb);
-
-        return $paginator;
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isArchived = false')
+        ;
     }
 
-    public function findEntries($userId, $isArchived, $isStarred, $isDeleted, $sort, $order)
+    /**
+     * Retrieves read entries for a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    public function getBuilderForArchiveByUser($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->where('e.userId =:userId')->setParameter('userId', $userId);
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isArchived = true')
+        ;
+    }
 
-        if (!is_null($isArchived)) {
-            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', $isArchived);
-        }
+    /**
+     * Retrieves starred entries for a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    public function getBuilderForStarredByUser($userId)
+    {
+        return $this
+            ->getBuilderByUser($userId)
+            ->andWhere('e.isStarred = true')
+        ;
+    }
 
-        if (!is_null($isStarred)) {
-            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', $isStarred);
+    /**
+     * Find Entries.
+     *
+     * @param int    $userId
+     * @param bool   $isArchived
+     * @param bool   $isStarred
+     * @param string $sort
+     * @param string $order
+     *
+     * @return array
+     */
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
+    {
+        $qb = $this->createQueryBuilder('e')
+            ->where('e.user =:userId')->setParameter('userId', $userId);
+
+        if (null !== $isArchived) {
+            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
         }
 
-        if (!is_null($isDeleted)) {
-            $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', $isDeleted);
+        if (null !== $isStarred) {
+            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
         }
 
         if ('created' === $sort) {
-            $qb->orderBy('e.createdAt', $order);
+            $qb->orderBy('e.id', $order);
         } elseif ('updated' === $sort) {
             $qb->orderBy('e.updatedAt', $order);
         }
 
-        return $qb
+        $pagerAdapter = new DoctrineORMAdapter($qb);
+
+        return new Pagerfanta($pagerAdapter);
+    }
+
+    /**
+     * Fetch an entry with a tag. Only used for tests.
+     *
+     * @return Entry
+     */
+    public function findOneWithTags($userId)
+    {
+        $qb = $this->createQueryBuilder('e')
+            ->innerJoin('e.tags', 't')
+            ->innerJoin('e.user', 'u')
+            ->addSelect('t', 'u')
+            ->where('e.user=:userId')->setParameter('userId', $userId)
+        ;
+
+        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()
-            ->getResult(Query::HYDRATE_ARRAY);
+            ->getSingleResult();
     }
 }