]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
Added configuration documentation for mobile apps
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Repository / AnnotationRepository.php
1 <?php
2
3 namespace Wallabag\AnnotationBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6
7 /**
8 * AnnotationRepository.
9 */
10 class AnnotationRepository extends EntityRepository
11 {
12 /**
13 * Return a query builder to used by other getBuilderFor* method.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 private function getBuilderByUser($userId)
20 {
21 return $this->createQueryBuilder('a')
22 ->leftJoin('a.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('a.id', 'desc')
25 ;
26 }
27
28 /**
29 * Retrieves all annotations for a user.
30 *
31 * @param int $userId
32 *
33 * @return QueryBuilder
34 */
35 public function getBuilderForAllByUser($userId)
36 {
37 return $this
38 ->getBuilderByUser($userId)
39 ;
40 }
41
42 /**
43 * Get annotation for this id.
44 *
45 * @param int $annotationId
46 *
47 * @return array
48 */
49 public function findAnnotationById($annotationId)
50 {
51 return $this->createQueryBuilder('a')
52 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
53 ->getQuery()->getSingleResult()
54 ;
55 }
56
57 /**
58 * Find annotations for entry id.
59 *
60 * @param int $entryId
61 * @param int $userId
62 *
63 * @return array
64 */
65 public function findAnnotationsByPageId($entryId, $userId)
66 {
67 return $this->createQueryBuilder('a')
68 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
69 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
70 ->getQuery()->getResult()
71 ;
72 }
73
74 /**
75 * Find last annotation for a given entry id. Used only for tests.
76 *
77 * @param int $entryId
78 *
79 * @return array
80 */
81 public function findLastAnnotationByPageId($entryId, $userId)
82 {
83 return $this->createQueryBuilder('a')
84 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
85 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
86 ->orderBy('a.id', 'DESC')
87 ->setMaxResults(1)
88 ->getQuery()
89 ->getOneOrNullResult();
90 }
91
92 /**
93 * Used only in test case to get the right annotation associated to the right user.
94 *
95 * @param string $username
96 *
97 * @return Annotation
98 */
99 public function findOneByUsername($username)
100 {
101 return $this->createQueryBuilder('a')
102 ->leftJoin('a.user', 'u')
103 ->where('u.username = :username')->setParameter('username', $username)
104 ->orderBy('a.id', 'DESC')
105 ->setMaxResults(1)
106 ->getQuery()
107 ->getSingleResult();
108 }
109 }