]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #1386 from wallabag/v2-refactor
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Pagerfanta;
8
9 class EntryRepository extends EntityRepository
10 {
11 /**
12 * Return a query builder to used by other getBuilderFor* method.
13 *
14 * @param int $userId
15 *
16 * @return QueryBuilder
17 */
18 private function getBuilderByUser($userId)
19 {
20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u')
22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->orderBy('e.id', 'desc')
24 ;
25 }
26
27 /**
28 * Retrieves unread entries for a user.
29 *
30 * @param int $userId
31 *
32 * @return QueryBuilder
33 */
34 public function getBuilderForUnreadByUser($userId)
35 {
36 return $this
37 ->getBuilderByUser($userId)
38 ->andWhere('e.isArchived = false')
39 ;
40 }
41
42 /**
43 * Retrieves read entries for a user.
44 *
45 * @param int $userId
46 *
47 * @return QueryBuilder
48 */
49 public function getBuilderForArchiveByUser($userId)
50 {
51 return $this
52 ->getBuilderByUser($userId)
53 ->andWhere('e.isArchived = true')
54 ;
55 }
56
57 /**
58 * Retrieves starred entries for a user.
59 *
60 * @param int $userId
61 *
62 * @return QueryBuilder
63 */
64 public function getBuilderForStarredByUser($userId)
65 {
66 return $this
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isStarred = true')
69 ;
70 }
71
72 /**
73 * Find Entries.
74 *
75 * @param int $userId
76 * @param bool $isArchived
77 * @param bool $isStarred
78 * @param string $sort
79 * @param string $order
80 *
81 * @return array
82 */
83 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
84 {
85 $qb = $this->createQueryBuilder('e')
86 ->where('e.user =:userId')->setParameter('userId', $userId);
87
88 if (null !== $isArchived) {
89 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
90 }
91
92 if (null !== $isStarred) {
93 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
94 }
95
96 if ('created' === $sort) {
97 $qb->orderBy('e.id', $order);
98 } elseif ('updated' === $sort) {
99 $qb->orderBy('e.updatedAt', $order);
100 }
101
102 $pagerAdapter = new DoctrineORMAdapter($qb);
103
104 return new Pagerfanta($pagerAdapter);
105 }
106
107 /**
108 * Fetch an entry with a tag. Only used for tests.
109 *
110 * @return Entry
111 */
112 public function findOneWithTags($userId)
113 {
114 $qb = $this->createQueryBuilder('e')
115 ->innerJoin('e.tags', 't')
116 ->innerJoin('e.user', 'u')
117 ->addSelect('t', 'u')
118 ->where('e.user=:userId')->setParameter('userId', $userId)
119 ;
120
121 return $qb->getQuery()->getResult();
122 }
123 }