]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
091bafeb | 6 | use Doctrine\ORM\NoResultException; |
52b84c11 | 7 | use Doctrine\ORM\QueryBuilder; |
bcf53ab7 WD |
8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
9 | use Pagerfanta\Pagerfanta; | |
52b84c11 | 10 | use Wallabag\CoreBundle\Entity\Entry; |
fc732227 | 11 | use Wallabag\CoreBundle\Entity\Tag; |
4a551637 | 12 | use Wallabag\CoreBundle\Helper\UrlHasher; |
9d50517c | 13 | |
be463487 | 14 | class EntryRepository extends EntityRepository |
9d50517c | 15 | { |
2b7a4889 NL |
16 | /** |
17 | * Retrieves all entries for a user. | |
18 | * | |
19 | * @param int $userId | |
20 | * | |
21 | * @return QueryBuilder | |
22 | */ | |
8d2527ec | 23 | public function getBuilderForAllByUser($userId, $sortBy = 'id', $direction = 'DESC') |
2b7a4889 NL |
24 | { |
25 | return $this | |
8d2527ec | 26 | ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) |
2b7a4889 NL |
27 | ; |
28 | } | |
29 | ||
0ab7404f JB |
30 | /** |
31 | * Retrieves unread entries for a user. | |
32 | * | |
33 | * @param int $userId | |
34 | * | |
35 | * @return QueryBuilder | |
36 | */ | |
8d2527ec | 37 | public function getBuilderForUnreadByUser($userId, $sortBy = 'id', $direction = 'DESC') |
0ab7404f JB |
38 | { |
39 | return $this | |
b7c5fda5 | 40 | ->getSortedQueryBuilderByUser($userId) |
0ab7404f | 41 | ->andWhere('e.isArchived = false') |
8d2527ec | 42 | ->orderBy('e.'.$sortBy, $direction) |
0ab7404f | 43 | ; |
9d50517c | 44 | } |
bd9f0815 | 45 | |
b84a8055 | 46 | /** |
4346a860 | 47 | * Retrieves read entries for a user. |
b84a8055 | 48 | * |
3b815d2d | 49 | * @param int $userId |
3b815d2d | 50 | * |
26864574 | 51 | * @return QueryBuilder |
b84a8055 | 52 | */ |
8d2527ec | 53 | public function getBuilderForArchiveByUser($userId, $sortBy = 'id', $direction = 'DESC') |
bd9f0815 | 54 | { |
0ab7404f | 55 | return $this |
9007fe00 | 56 | ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc') |
0ab7404f | 57 | ->andWhere('e.isArchived = true') |
8d2527ec | 58 | ->orderBy('e.'.$sortBy, $direction) |
0ab7404f | 59 | ; |
bd9f0815 NL |
60 | } |
61 | ||
b84a8055 | 62 | /** |
4346a860 | 63 | * Retrieves starred entries for a user. |
b84a8055 | 64 | * |
3b815d2d | 65 | * @param int $userId |
3b815d2d | 66 | * |
26864574 | 67 | * @return QueryBuilder |
b84a8055 | 68 | */ |
8d2527ec | 69 | public function getBuilderForStarredByUser($userId, $sortBy = 'id', $direction = 'DESC') |
bd9f0815 | 70 | { |
0ab7404f | 71 | return $this |
b7c5fda5 | 72 | ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc') |
0ab7404f | 73 | ->andWhere('e.isStarred = true') |
8d2527ec | 74 | ->orderBy('e.'.$sortBy, $direction) |
0ab7404f | 75 | ; |
bd9f0815 | 76 | } |
a8c90c5c | 77 | |
ee122a75 NL |
78 | /** |
79 | * Retrieves entries filtered with a search term for a user. | |
80 | * | |
81 | * @param int $userId | |
82 | * @param string $term | |
52b84c11 | 83 | * @param string $currentRoute |
ee122a75 NL |
84 | * |
85 | * @return QueryBuilder | |
86 | */ | |
49b042df | 87 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
ee122a75 | 88 | { |
49b042df | 89 | $qb = $this |
b7c5fda5 | 90 | ->getSortedQueryBuilderByUser($userId); |
49b042df NL |
91 | |
92 | if ('starred' === $currentRoute) { | |
93 | $qb->andWhere('e.isStarred = true'); | |
94 | } elseif ('unread' === $currentRoute) { | |
95 | $qb->andWhere('e.isArchived = false'); | |
96 | } elseif ('archive' === $currentRoute) { | |
97 | $qb->andWhere('e.isArchived = true'); | |
98 | } | |
99 | ||
eac09b48 | 100 | // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive |
49b042df | 101 | $qb |
f808b016 | 102 | ->andWhere('lower(e.content) LIKE lower(:term) OR lower(e.title) LIKE lower(:term) OR lower(e.url) LIKE lower(:term)')->setParameter('term', '%' . $term . '%') |
ee122a75 | 103 | ->leftJoin('e.tags', 't') |
32f455c1 | 104 | ->groupBy('e.id'); |
49b042df NL |
105 | |
106 | return $qb; | |
ee122a75 NL |
107 | } |
108 | ||
b6520f0b | 109 | /** |
06366972 | 110 | * Retrieve a sorted list of untagged entries for a user. |
b6520f0b NL |
111 | * |
112 | * @param int $userId | |
113 | * | |
114 | * @return QueryBuilder | |
115 | */ | |
116 | public function getBuilderForUntaggedByUser($userId) | |
117 | { | |
09ef25c3 | 118 | return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
06366972 KD |
119 | } |
120 | ||
121 | /** | |
122 | * Retrieve untagged entries for a user. | |
b8115ff4 | 123 | * |
06366972 | 124 | * @param int $userId |
b8115ff4 | 125 | * |
06366972 KD |
126 | * @return QueryBuilder |
127 | */ | |
128 | public function getRawBuilderForUntaggedByUser($userId) | |
129 | { | |
130 | return $this->getQueryBuilderByUser($userId) | |
131 | ->leftJoin('e.tags', 't') | |
132 | ->andWhere('t.id is null'); | |
b6520f0b NL |
133 | } |
134 | ||
ad51743e KD |
135 | /** |
136 | * Retrieve the number of untagged entries for a user. | |
0f2d24fe | 137 | * |
ad51743e | 138 | * @param int $userId |
0f2d24fe | 139 | * |
ad51743e KD |
140 | * @return int |
141 | */ | |
142 | public function countUntaggedEntriesByUser($userId) | |
143 | { | |
0f2d24fe | 144 | return (int) $this->getRawBuilderForUntaggedByUser($userId) |
ad51743e | 145 | ->select('count(e.id)') |
0f2d24fe | 146 | ->getQuery() |
ad51743e KD |
147 | ->getSingleScalarResult(); |
148 | } | |
149 | ||
3b815d2d | 150 | /** |
4346a860 | 151 | * Find Entries. |
3b815d2d | 152 | * |
2a94b1d1 NL |
153 | * @param int $userId |
154 | * @param bool $isArchived | |
155 | * @param bool $isStarred | |
1112e547 | 156 | * @param bool $isPublic |
2a94b1d1 NL |
157 | * @param string $sort |
158 | * @param string $order | |
8cb869ea TC |
159 | * @param int $since |
160 | * @param string $tags | |
2c290747 KD |
161 | * @param string $detail 'metadata' or 'full'. Include content field if 'full' |
162 | * | |
163 | * @todo Breaking change: replace default detail=full by detail=metadata in a future version | |
3b815d2d | 164 | * |
52b84c11 | 165 | * @return Pagerfanta |
3b815d2d | 166 | */ |
2c290747 | 167 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full') |
a8c90c5c | 168 | { |
2c290747 KD |
169 | if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) { |
170 | throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata'); | |
171 | } | |
172 | ||
a8c90c5c | 173 | $qb = $this->createQueryBuilder('e') |
28803f10 | 174 | ->leftJoin('e.tags', 't') |
7c04b739 | 175 | ->where('e.user = :userId')->setParameter('userId', $userId); |
6e334aba | 176 | |
2c290747 KD |
177 | if ('metadata' === $detail) { |
178 | $fieldNames = $this->getClassMetadata()->getFieldNames(); | |
179 | $fields = array_filter($fieldNames, function ($k) { | |
180 | return 'content' !== $k; | |
181 | }); | |
182 | $qb->select(sprintf('partial e.{%s}', implode(',', $fields))); | |
183 | } | |
184 | ||
3b815d2d | 185 | if (null !== $isArchived) { |
1112e547 | 186 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
6e334aba NL |
187 | } |
188 | ||
3b815d2d | 189 | if (null !== $isStarred) { |
1112e547 JB |
190 | $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred); |
191 | } | |
192 | ||
193 | if (null !== $isPublic) { | |
f808b016 | 194 | $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL'); |
6e334aba NL |
195 | } |
196 | ||
c3f8b428 | 197 | if ($since > 0) { |
e5fb89e5 | 198 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
4f0558a0 TC |
199 | } |
200 | ||
2a1ceb67 | 201 | if (\is_string($tags) && '' !== $tags) { |
7c04b739 JB |
202 | foreach (explode(',', $tags) as $i => $tag) { |
203 | $entryAlias = 'e' . $i; | |
204 | $tagAlias = 't' . $i; | |
205 | ||
33264c2d | 206 | // Complexe queries to ensure multiple tags are associated to an entry |
7c04b739 JB |
207 | // https://stackoverflow.com/a/6638146/569101 |
208 | $qb->andWhere($qb->expr()->in( | |
209 | 'e.id', | |
210 | $this->createQueryBuilder($entryAlias) | |
211 | ->select($entryAlias . '.id') | |
212 | ->leftJoin($entryAlias . '.tags', $tagAlias) | |
213 | ->where($tagAlias . '.label = :label' . $i) | |
214 | ->getDQL() | |
215 | )); | |
216 | ||
217 | // bound parameter to the main query builder | |
218 | $qb->setParameter('label' . $i, $tag); | |
28803f10 | 219 | } |
e5fb89e5 TC |
220 | } |
221 | ||
78e3fafa JB |
222 | if (!\in_array(strtolower($order), ['asc', 'desc'], true)) { |
223 | throw new \Exception('Order "' . $order . '" parameter is wrong, allowed: asc or desc'); | |
224 | } | |
225 | ||
bc782eaa | 226 | if ('created' === $sort) { |
2385f891 | 227 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
228 | } elseif ('updated' === $sort) { |
229 | $qb->orderBy('e.updatedAt', $order); | |
7c0d6826 | 230 | } elseif ('archived' === $sort) { |
0e70e812 | 231 | $qb->orderBy('e.archivedAt', $order); |
bc782eaa NL |
232 | } |
233 | ||
5a5da369 | 234 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
235 | |
236 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 237 | } |
46bbd8d3 | 238 | |
a36737f4 NL |
239 | /** |
240 | * Fetch an entry with a tag. Only used for tests. | |
241 | * | |
5c072d2b NL |
242 | * @param int $userId |
243 | * | |
52b84c11 | 244 | * @return array |
a36737f4 | 245 | */ |
092ca707 | 246 | public function findOneWithTags($userId) |
46bbd8d3 NL |
247 | { |
248 | $qb = $this->createQueryBuilder('e') | |
249 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
250 | ->innerJoin('e.user', 'u') |
251 | ->addSelect('t', 'u') | |
7c04b739 | 252 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 253 | ; |
092ca707 | 254 | |
0ca374e6 | 255 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 256 | } |
d4ebe5c5 JB |
257 | |
258 | /** | |
259 | * Find distinct language for a given user. | |
260 | * Used to build the filter language list. | |
261 | * | |
262 | * @param int $userId User id | |
263 | * | |
264 | * @return array | |
265 | */ | |
266 | public function findDistinctLanguageByUser($userId) | |
267 | { | |
268 | $results = $this->createQueryBuilder('e') | |
269 | ->select('e.language') | |
270 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
271 | ->andWhere('e.language IS NOT NULL') | |
272 | ->groupBy('e.language') | |
273 | ->orderBy('e.language', ' ASC') | |
274 | ->getQuery() | |
275 | ->getResult(); | |
276 | ||
4094ea47 | 277 | $languages = []; |
d4ebe5c5 JB |
278 | foreach ($results as $result) { |
279 | $languages[$result['language']] = $result['language']; | |
280 | } | |
281 | ||
282 | return $languages; | |
283 | } | |
159986c4 | 284 | |
159986c4 | 285 | /** |
cfb28c9d | 286 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 287 | * |
cfb28c9d | 288 | * @param string $username |
159986c4 JB |
289 | * |
290 | * @return Entry | |
291 | */ | |
292 | public function findOneByUsernameAndNotArchived($username) | |
293 | { | |
294 | return $this->createQueryBuilder('e') | |
295 | ->leftJoin('e.user', 'u') | |
296 | ->where('u.username = :username')->setParameter('username', $username) | |
297 | ->andWhere('e.isArchived = false') | |
298 | ->setMaxResults(1) | |
299 | ->getQuery() | |
300 | ->getSingleResult(); | |
301 | } | |
fc732227 JB |
302 | |
303 | /** | |
304 | * Remove a tag from all user entries. | |
4059a061 JB |
305 | * |
306 | * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation. | |
6be97501 JB |
307 | * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix: |
308 | * | |
309 | * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId | |
fc732227 JB |
310 | * |
311 | * @param int $userId | |
fc732227 JB |
312 | */ |
313 | public function removeTag($userId, Tag $tag) | |
314 | { | |
b7c5fda5 | 315 | $entries = $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
316 | ->innerJoin('e.tags', 't') |
317 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
318 | ->getQuery() | |
319 | ->getResult(); | |
320 | ||
321 | foreach ($entries as $entry) { | |
322 | $entry->removeTag($tag); | |
323 | } | |
324 | ||
325 | $this->getEntityManager()->flush(); | |
4059a061 JB |
326 | } |
327 | ||
4da01f49 | 328 | /** |
9bf83f1f | 329 | * Remove tags from all user entries. |
4da01f49 | 330 | * |
9bf83f1f | 331 | * @param int $userId |
4da01f49 TC |
332 | * @param Array<Tag> $tags |
333 | */ | |
9bf83f1f TC |
334 | public function removeTags($userId, $tags) |
335 | { | |
4da01f49 TC |
336 | foreach ($tags as $tag) { |
337 | $this->removeTag($userId, $tag); | |
338 | } | |
339 | } | |
340 | ||
4059a061 JB |
341 | /** |
342 | * Find all entries that are attached to a give tag id. | |
343 | * | |
344 | * @param int $userId | |
345 | * @param int $tagId | |
346 | * | |
347 | * @return array | |
348 | */ | |
349 | public function findAllByTagId($userId, $tagId) | |
350 | { | |
b7c5fda5 | 351 | return $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
352 | ->innerJoin('e.tags', 't') |
353 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
354 | ->getQuery() | |
355 | ->getResult(); | |
fc732227 | 356 | } |
303768df NL |
357 | |
358 | /** | |
359 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 360 | * If it exists, return the entry otherwise return false. |
303768df | 361 | * |
50f35f0d JB |
362 | * @param string $url |
363 | * @param int $userId | |
303768df | 364 | * |
52e8d932 | 365 | * @return Entry|false |
303768df | 366 | */ |
78833672 | 367 | public function findByUrlAndUserId($url, $userId) |
303768df | 368 | { |
d5744bf0 | 369 | return $this->findByHashedUrlAndUserId( |
4a551637 | 370 | UrlHasher::hashUrl($url), |
0132ccd2 JB |
371 | $userId |
372 | ); | |
303768df | 373 | } |
5c072d2b | 374 | |
9c2b2aae JB |
375 | /** |
376 | * Find an entry by its hashed url and its owner. | |
377 | * If it exists, return the entry otherwise return false. | |
378 | * | |
c579ce23 JB |
379 | * @param string $hashedUrl Url hashed using sha1 |
380 | * @param int $userId | |
9c2b2aae | 381 | * |
52e8d932 | 382 | * @return Entry|false |
9c2b2aae JB |
383 | */ |
384 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | |
385 | { | |
70df4c33 | 386 | // try first using hashed_url (to use the database index) |
9c2b2aae | 387 | $res = $this->createQueryBuilder('e') |
c579ce23 | 388 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) |
9c2b2aae JB |
389 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
390 | ->getQuery() | |
391 | ->getResult(); | |
392 | ||
393 | if (\count($res)) { | |
394 | return current($res); | |
395 | } | |
396 | ||
70df4c33 JB |
397 | // then try using hashed_given_url (to use the database index) |
398 | $res = $this->createQueryBuilder('e') | |
399 | ->where('e.hashedGivenUrl = :hashed_given_url')->setParameter('hashed_given_url', $hashedUrl) | |
9c2b2aae JB |
400 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
401 | ->getQuery() | |
402 | ->getResult(); | |
403 | ||
404 | if (\count($res)) { | |
405 | return current($res); | |
406 | } | |
407 | ||
408 | return false; | |
409 | } | |
410 | ||
5c072d2b NL |
411 | /** |
412 | * Count all entries for a user. | |
413 | * | |
414 | * @param int $userId | |
415 | * | |
e678c475 | 416 | * @return int |
5c072d2b | 417 | */ |
273b6f06 | 418 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
419 | { |
420 | $qb = $this->createQueryBuilder('e') | |
421 | ->select('count(e)') | |
7c04b739 JB |
422 | ->where('e.user = :userId')->setParameter('userId', $userId) |
423 | ; | |
424 | ||
425 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
426 | } | |
427 | ||
191564b7 JB |
428 | /** |
429 | * Remove all entries for a user id. | |
8c61fd12 | 430 | * Used when a user want to reset all informations. |
191564b7 | 431 | * |
8c61fd12 | 432 | * @param int $userId |
191564b7 JB |
433 | */ |
434 | public function removeAllByUserId($userId) | |
435 | { | |
436 | $this->getEntityManager() | |
b0de88f7 JB |
437 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
438 | ->setParameter('userId', $userId) | |
191564b7 JB |
439 | ->execute(); |
440 | } | |
6da1aebc TC |
441 | |
442 | public function removeArchivedByUserId($userId) | |
443 | { | |
444 | $this->getEntityManager() | |
445 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
446 | ->setParameter('userId', $userId) | |
447 | ->execute(); | |
448 | } | |
e2f3800c TC |
449 | |
450 | /** | |
451 | * Get id and url from all entries | |
452 | * Used for the clean-duplicates command. | |
453 | */ | |
dbf1188c | 454 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
455 | { |
456 | $qb = $this->createQueryBuilder('e') | |
457 | ->select('e.id, e.url') | |
458 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
459 | ||
460 | return $qb->getQuery()->getArrayResult(); | |
461 | } | |
d09fe4d2 | 462 | |
511f1ce1 NH |
463 | /** |
464 | * @param int $userId | |
465 | * | |
466 | * @return array | |
467 | */ | |
215409a8 | 468 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
469 | { |
470 | $qb = $this->createQueryBuilder('e') | |
471 | ->select('e.id'); | |
472 | ||
473 | if (null !== $userId) { | |
474 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
475 | } | |
476 | ||
477 | return $qb->getQuery()->getArrayResult(); | |
478 | } | |
479 | ||
d09fe4d2 NL |
480 | /** |
481 | * Find all entries by url and owner. | |
482 | * | |
50f35f0d JB |
483 | * @param string $url |
484 | * @param int $userId | |
d09fe4d2 NL |
485 | * |
486 | * @return array | |
487 | */ | |
488 | public function findAllByUrlAndUserId($url, $userId) | |
489 | { | |
89f108b4 | 490 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
491 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
492 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
493 | ->getQuery() | |
494 | ->getResult(); | |
d09fe4d2 | 495 | } |
f808b016 | 496 | |
09ef25c3 NL |
497 | /** |
498 | * Returns a random entry, filtering by status. | |
499 | * | |
50f35f0d JB |
500 | * @param int $userId |
501 | * @param string $type Can be unread, archive, starred, etc | |
09ef25c3 | 502 | * |
091bafeb | 503 | * @throws NoResultException |
062fad43 | 504 | * |
09ef25c3 NL |
505 | * @return Entry |
506 | */ | |
9a57653a | 507 | public function getRandomEntry($userId, $type = '') |
09ef25c3 | 508 | { |
062fad43 | 509 | $qb = $this->getQueryBuilderByUser($userId) |
50f35f0d | 510 | ->select('e.id'); |
09ef25c3 | 511 | |
9a57653a JB |
512 | switch ($type) { |
513 | case 'unread': | |
514 | $qb->andWhere('e.isArchived = false'); | |
515 | break; | |
516 | case 'archive': | |
517 | $qb->andWhere('e.isArchived = true'); | |
518 | break; | |
519 | case 'starred': | |
520 | $qb->andWhere('e.isStarred = true'); | |
521 | break; | |
522 | case 'untagged': | |
523 | $qb->leftJoin('e.tags', 't'); | |
524 | $qb->andWhere('t.id is null'); | |
525 | break; | |
f85d220c JB |
526 | } |
527 | ||
50f35f0d | 528 | $ids = $qb->getQuery()->getArrayResult(); |
062fad43 | 529 | |
091bafeb JB |
530 | if (empty($ids)) { |
531 | throw new NoResultException(); | |
532 | } | |
533 | ||
50f35f0d JB |
534 | // random select one in the list |
535 | $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; | |
536 | ||
537 | return $this->find($randomId); | |
09ef25c3 NL |
538 | } |
539 | ||
f808b016 | 540 | /** |
b7c5fda5 KD |
541 | * Return a query builder to be used by other getBuilderFor* method. |
542 | * | |
b8115ff4 | 543 | * @param int $userId |
b7c5fda5 KD |
544 | * |
545 | * @return QueryBuilder | |
546 | */ | |
547 | private function getQueryBuilderByUser($userId) | |
548 | { | |
549 | return $this->createQueryBuilder('e') | |
550 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | |
551 | } | |
552 | ||
553 | /** | |
554 | * Return a sorted query builder to be used by other getBuilderFor* method. | |
f808b016 | 555 | * |
a991c46e F |
556 | * @param int $userId |
557 | * @param string $sortBy | |
558 | * @param string $direction | |
f808b016 JB |
559 | * |
560 | * @return QueryBuilder | |
561 | */ | |
b7c5fda5 | 562 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 | 563 | { |
17476f4d | 564 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); |
b7c5fda5 KD |
565 | } |
566 | ||
567 | /** | |
b8115ff4 KD |
568 | * Return the given QueryBuilder with an orderBy() call. |
569 | * | |
8d4ed0df JB |
570 | * @param string $sortBy |
571 | * @param string $direction | |
b7c5fda5 KD |
572 | * |
573 | * @return QueryBuilder | |
574 | */ | |
575 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | |
576 | { | |
09ef25c3 | 577 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 578 | } |
9d50517c | 579 | } |