]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/RssController.php
Merge pull request #2418 from wallabag/add-twitter-cards
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
CommitLineData
0c83fd59
J
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
619cc453
JB
5use Pagerfanta\Adapter\DoctrineORMAdapter;
6use Pagerfanta\Pagerfanta;
0c83fd59 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
619cc453 8use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
0c83fd59 9use Symfony\Bundle\FrameworkBundle\Controller\Controller;
0c83fd59 10use Wallabag\CoreBundle\Entity\Entry;
619cc453 11use Wallabag\UserBundle\Entity\User;
0c83fd59
J
12
13class RssController extends Controller
14{
15 /**
4346a860 16 * Shows unread entries for current user.
0c83fd59
J
17 *
18 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
1210dae1 19 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
20 *
21 * @return \Symfony\Component\HttpFoundation\Response
22 */
23 public function showUnreadAction(User $user)
24 {
0ab7404f 25 return $this->showEntries('unread', $user);
0c83fd59
J
26 }
27
28 /**
4346a860 29 * Shows read entries for current user.
0c83fd59
J
30 *
31 * @Route("/{username}/{token}/archive.xml", name="archive_rss")
1210dae1 32 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
33 *
34 * @return \Symfony\Component\HttpFoundation\Response
35 */
36 public function showArchiveAction(User $user)
37 {
0ab7404f 38 return $this->showEntries('archive', $user);
0c83fd59
J
39 }
40
41 /**
4346a860 42 * Shows starred entries for current user.
0c83fd59
J
43 *
44 * @Route("/{username}/{token}/starred.xml", name="starred_rss")
1210dae1 45 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
0c83fd59
J
46 *
47 * @return \Symfony\Component\HttpFoundation\Response
48 */
49 public function showStarredAction(User $user)
50 {
0ab7404f
JB
51 return $this->showEntries('starred', $user);
52 }
53
54 /**
55 * Global method to retrieve entries depending on the given type
56 * It returns the response to be send.
57 *
58 * @param string $type Entries type: unread, starred or archive
59 * @param User $user
60 *
61 * @return \Symfony\Component\HttpFoundation\Response
62 */
63 private function showEntries($type, User $user)
64 {
65 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
66
67 switch ($type) {
68 case 'starred':
69 $qb = $repository->getBuilderForStarredByUser($user->getId());
70 break;
71
72 case 'archive':
73 $qb = $repository->getBuilderForArchiveByUser($user->getId());
74 break;
75
76 case 'unread':
77 $qb = $repository->getBuilderForUnreadByUser($user->getId());
78 break;
79
80 default:
81 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
82 }
0c83fd59 83
26864574
NL
84 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
85 $entries = new Pagerfanta($pagerAdapter);
86
67c99849 87 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
9fb6ac83
FG
88 $entries->setMaxPerPage($perPage);
89
2ff9991a 90 return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
0ab7404f 91 'type' => $type,
0c83fd59 92 'entries' => $entries,
4094ea47 93 ]);
0c83fd59
J
94 }
95}