]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Repository / AnnotationRepository.php
CommitLineData
4dc87223
NL
1<?php
2
3namespace Wallabag\AnnotationBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
6
7/**
8 * AnnotationRepository.
9 */
10class AnnotationRepository extends EntityRepository
11{
4dc87223
NL
12 /**
13 * Retrieves all annotations for a user.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 public function getBuilderForAllByUser($userId)
20 {
21 return $this
22 ->getBuilderByUser($userId)
23 ;
24 }
25
26 /**
27 * Get annotation for this id.
28 *
29 * @param int $annotationId
30 *
31 * @return array
32 */
33 public function findAnnotationById($annotationId)
34 {
35 return $this->createQueryBuilder('a')
36 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
aa474109
JB
37 ->getQuery()
38 ->getSingleResult()
4dc87223
NL
39 ;
40 }
41
42 /**
43 * Find annotations for entry id.
44 *
45 * @param int $entryId
46 * @param int $userId
47 *
48 * @return array
49 */
50 public function findAnnotationsByPageId($entryId, $userId)
51 {
52 return $this->createQueryBuilder('a')
53 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
54 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
aa474109
JB
55 ->getQuery()
56 ->getResult()
4dc87223
NL
57 ;
58 }
59
60 /**
61 * Find last annotation for a given entry id. Used only for tests.
62 *
63 * @param int $entryId
64 *
65 * @return array
66 */
67 public function findLastAnnotationByPageId($entryId, $userId)
68 {
69 return $this->createQueryBuilder('a')
70 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
71 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
72 ->orderBy('a.id', 'DESC')
73 ->setMaxResults(1)
74 ->getQuery()
75 ->getOneOrNullResult();
76 }
09d8bb6f
JB
77
78 /**
79 * Used only in test case to get the right annotation associated to the right user.
80 *
81 * @param string $username
82 *
83 * @return Annotation
84 */
85 public function findOneByUsername($username)
86 {
87 return $this->createQueryBuilder('a')
88 ->leftJoin('a.user', 'u')
89 ->where('u.username = :username')->setParameter('username', $username)
b95ffda2 90 ->orderBy('a.id', 'DESC')
09d8bb6f
JB
91 ->setMaxResults(1)
92 ->getQuery()
93 ->getSingleResult();
94 }
191564b7
JB
95
96 /**
97 * Remove all annotations for a user id.
8c61fd12 98 * Used when a user want to reset all informations.
191564b7 99 *
8c61fd12 100 * @param int $userId
191564b7
JB
101 */
102 public function removeAllByUserId($userId)
103 {
104 $this->getEntityManager()
b0de88f7
JB
105 ->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = :userId')
106 ->setParameter('userId', $userId)
191564b7
JB
107 ->execute();
108 }
6da1aebc
TC
109
110 /**
9102851f 111 * Find all annotations related to archived entries.
6da1aebc
TC
112 *
113 * @param $userId
9102851f 114 *
6da1aebc
TC
115 * @return mixed
116 */
13a592a1 117 public function findAllArchivedEntriesByUser($userId)
6da1aebc
TC
118 {
119 return $this->createQueryBuilder('a')
120 ->leftJoin('a.entry', 'e')
121 ->where('a.user = :userid')->setParameter(':userid', $userId)
122 ->andWhere('e.isArchived = true')
123 ->getQuery()
124 ->getResult();
125 }
f808b016
JB
126
127 /**
128 * Return a query builder to used by other getBuilderFor* method.
129 *
130 * @param int $userId
131 *
132 * @return QueryBuilder
133 */
134 private function getBuilderByUser($userId)
135 {
136 return $this->createQueryBuilder('a')
137 ->leftJoin('a.user', 'u')
138 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
139 ->orderBy('a.id', 'desc')
140 ;
141 }
4dc87223 142}