]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Handle no random result found
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index 0556307918c87a9fc6b27644a870ab25da37fc19..45366623d23204e8d8a1ffd3bfd9b1fd1c2ca832 100644 (file)
@@ -3,6 +3,7 @@
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
+use Doctrine\ORM\NoResultException;
 use Doctrine\ORM\QueryBuilder;
 use Pagerfanta\Adapter\DoctrineORMAdapter;
 use Pagerfanta\Pagerfanta;
@@ -325,8 +326,8 @@ class EntryRepository extends EntityRepository
      * Find an entry by its url and its owner.
      * If it exists, return the entry otherwise return false.
      *
-     * @param $url
-     * @param $userId
+     * @param string $url
+     * @param int    $userId
      *
      * @return Entry|bool
      */
@@ -417,8 +418,8 @@ class EntryRepository extends EntityRepository
     /**
      * Find all entries by url and owner.
      *
-     * @param $url
-     * @param $userId
+     * @param string $url
+     * @param int    $userId
      *
      * @return array
      */
@@ -434,50 +435,44 @@ class EntryRepository extends EntityRepository
     /**
      * 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
+     * @param int    $userId
+     * @param string $type   Can be unread, archive, starred, etc
      *
-     * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934
+     * @throws NoResultException
      *
      * @return Entry
      */
-    public function getRandomEntry($userId, $status = '')
+    public function getRandomEntry($userId, $type = '')
     {
         $qb = $this->getQueryBuilderByUser($userId)
-            ->select('MIN(e.id)', 'MAX(e.id)');
+            ->select('e.id');
 
-        if ('unread' === $status) {
-            $qb->andWhere('e.isArchived = false');
+        switch ($type) {
+            case 'unread':
+                $qb->andWhere('e.isArchived = false');
+                break;
+            case 'archive':
+                $qb->andWhere('e.isArchived = true');
+                break;
+            case 'starred':
+                $qb->andWhere('e.isStarred = true');
+                break;
+            case 'untagged':
+                $qb->leftJoin('e.tags', 't');
+                $qb->andWhere('t.id is null');
+                break;
         }
 
-        if ('archive' === $status) {
-            $qb->andWhere('e.isArchived = true');
-        }
+        $ids = $qb->getQuery()->getArrayResult();
 
-        if ('starred' === $status) {
-            $qb->andWhere('e.isStarred = true');
-        }
-
-        if ('untagged' === $status) {
-            $qb->leftJoin('e.tags', 't');
-            $qb->andWhere('t.id is null');
+        if (empty($ids)) {
+            throw new NoResultException();
         }
 
-        $idLimits = $qb
-            ->getQuery()
-            ->getOneOrNullResult();
-        $randomPossibleIds = rand($idLimits[1], $idLimits[2]);
+        // random select one in the list
+        $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
 
-        return $qb
-            ->select('e')
-            ->andWhere('e.id >= :random_id')
-            ->setParameter('random_id', $randomPossibleIds)
-            ->setMaxResults(1)
-            ->getQuery()
-            ->getSingleResult();
+        return $this->find($randomId);
     }
 
     /**