]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Fix tests
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index a31f97b5a5c95660f9b99f49764d5e4d0c105645..71ed7b6d837a01f960184fd780e074c78bdb6aa4 100644 (file)
@@ -50,7 +50,7 @@ class EntryRepository extends EntityRepository
     public function getBuilderForArchiveByUser($userId)
     {
         return $this
-            ->getSortedQueryBuilderByUser($userId)
+            ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc')
             ->andWhere('e.isArchived = true')
         ;
     }
@@ -110,15 +110,14 @@ class EntryRepository extends EntityRepository
      */
     public function getBuilderForUntaggedByUser($userId)
     {
-        return $this
-            ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
+        return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
     }
 
     /**
      * Retrieve untagged entries for a user.
-     * 
+     *
      * @param int $userId
-     * 
+     *
      * @return QueryBuilder
      */
     public function getRawBuilderForUntaggedByUser($userId)
@@ -189,6 +188,8 @@ class EntryRepository extends EntityRepository
             $qb->orderBy('e.id', $order);
         } elseif ('updated' === $sort) {
             $qb->orderBy('e.updatedAt', $order);
+        } elseif ('archived' === $sort) {
+            $qb->orderBy('e.archivedAt', $order);
         }
 
         $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
@@ -426,10 +427,55 @@ class EntryRepository extends EntityRepository
             ->getResult();
     }
 
+    /**
+     * Returns a random entry, filtering by status.
+     *
+     * @param $userId
+     * @param string $status can be unread, archive or starred
+     *
+     * @throws \Doctrine\ORM\NoResultException
+     * @throws \Doctrine\ORM\NonUniqueResultException
+     *
+     * @return Entry
+     */
+    public function getRandomEntry($userId, $status = '')
+    {
+        $max = $this->getEntityManager()
+            ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
+            ->setParameter('userId', $userId)
+            ->getSingleScalarResult();
+
+        $qb = $this->createQueryBuilder('e')
+            ->where('e.user = :user_id')->setParameter('user_id', $userId);
+
+        if ('unread' === $status) {
+            $qb->andWhere('e.isArchived = false');
+        }
+
+        if ('archive' === $status) {
+            $qb->andWhere('e.isArchived = true');
+        }
+
+        if ('starred' === $status) {
+            $qb->andWhere('e.isStarred = true');
+        }
+
+        if ('untagged' === $status) {
+            $qb->leftJoin('e.tags', 't');
+            $qb->andWhere('t.id is null');
+        }
+
+        return $qb->andWhere('e.id >= :rand')
+            ->setParameter('rand', rand(0, $max))
+            ->setMaxResults(1)
+            ->getQuery()
+            ->getSingleResult();
+    }
+
     /**
      * Return a query builder to be used by other getBuilderFor* method.
      *
-     * @param int    $userId
+     * @param int $userId
      *
      * @return QueryBuilder
      */
@@ -450,21 +496,20 @@ class EntryRepository extends EntityRepository
      */
     private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc')
     {
-        return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId));
+        return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction);
     }
 
     /**
-     * Return the given QueryBuilder with an orderBy() call
-     * 
+     * Return the given QueryBuilder with an orderBy() call.
+     *
      * @param QueryBuilder $qb
-     * @param string $sortBy
-     * @param string $direction
+     * @param string       $sortBy
+     * @param string       $direction
      *
      * @return QueryBuilder
      */
     private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
     {
-        return $qb
-            ->orderBy(sprintf('e.%s', $sortBy), $direction);
+        return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
     }
 }