aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/EntryRepository.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php58
1 files changed, 50 insertions, 8 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 8b6cf443..45366623 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -3,6 +3,7 @@
3namespace Wallabag\CoreBundle\Repository; 3namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\NoResultException;
6use Doctrine\ORM\QueryBuilder; 7use Doctrine\ORM\QueryBuilder;
7use Pagerfanta\Adapter\DoctrineORMAdapter; 8use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta; 9use Pagerfanta\Pagerfanta;
@@ -110,8 +111,7 @@ class EntryRepository extends EntityRepository
110 */ 111 */
111 public function getBuilderForUntaggedByUser($userId) 112 public function getBuilderForUntaggedByUser($userId)
112 { 113 {
113 return $this 114 return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
114 ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
115 } 115 }
116 116
117 /** 117 /**
@@ -326,8 +326,8 @@ class EntryRepository extends EntityRepository
326 * Find an entry by its url and its owner. 326 * Find an entry by its url and its owner.
327 * If it exists, return the entry otherwise return false. 327 * If it exists, return the entry otherwise return false.
328 * 328 *
329 * @param $url 329 * @param string $url
330 * @param $userId 330 * @param int $userId
331 * 331 *
332 * @return Entry|bool 332 * @return Entry|bool
333 */ 333 */
@@ -418,8 +418,8 @@ class EntryRepository extends EntityRepository
418 /** 418 /**
419 * Find all entries by url and owner. 419 * Find all entries by url and owner.
420 * 420 *
421 * @param $url 421 * @param string $url
422 * @param $userId 422 * @param int $userId
423 * 423 *
424 * @return array 424 * @return array
425 */ 425 */
@@ -433,6 +433,49 @@ class EntryRepository extends EntityRepository
433 } 433 }
434 434
435 /** 435 /**
436 * Returns a random entry, filtering by status.
437 *
438 * @param int $userId
439 * @param string $type Can be unread, archive, starred, etc
440 *
441 * @throws NoResultException
442 *
443 * @return Entry
444 */
445 public function getRandomEntry($userId, $type = '')
446 {
447 $qb = $this->getQueryBuilderByUser($userId)
448 ->select('e.id');
449
450 switch ($type) {
451 case 'unread':
452 $qb->andWhere('e.isArchived = false');
453 break;
454 case 'archive':
455 $qb->andWhere('e.isArchived = true');
456 break;
457 case 'starred':
458 $qb->andWhere('e.isStarred = true');
459 break;
460 case 'untagged':
461 $qb->leftJoin('e.tags', 't');
462 $qb->andWhere('t.id is null');
463 break;
464 }
465
466 $ids = $qb->getQuery()->getArrayResult();
467
468 if (empty($ids)) {
469 throw new NoResultException();
470 }
471
472 // random select one in the list
473 $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
474
475 return $this->find($randomId);
476 }
477
478 /**
436 * Return a query builder to be used by other getBuilderFor* method. 479 * Return a query builder to be used by other getBuilderFor* method.
437 * 480 *
438 * @param int $userId 481 * @param int $userId
@@ -470,7 +513,6 @@ class EntryRepository extends EntityRepository
470 */ 513 */
471 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') 514 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
472 { 515 {
473 return $qb 516 return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
474 ->orderBy(sprintf('e.%s', $sortBy), $direction);
475 } 517 }
476} 518}