]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/RssController.php
Merge pull request #1386 from wallabag/v2-refactor
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
7 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8 use Wallabag\CoreBundle\Entity\User;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Pagerfanta\Adapter\DoctrineORMAdapter;
11 use Pagerfanta\Pagerfanta;
12
13 class RssController extends Controller
14 {
15 /**
16 * Shows unread entries for current user.
17 *
18 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
19 * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
20 *
21 * @return \Symfony\Component\HttpFoundation\Response
22 */
23 public function showUnreadAction(User $user)
24 {
25 return $this->showEntries('unread', $user);
26 }
27
28 /**
29 * Shows read entries for current user.
30 *
31 * @Route("/{username}/{token}/archive.xml", name="archive_rss")
32 * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
33 *
34 * @return \Symfony\Component\HttpFoundation\Response
35 */
36 public function showArchiveAction(User $user)
37 {
38 return $this->showEntries('archive', $user);
39 }
40
41 /**
42 * Shows starred entries for current user.
43 *
44 * @Route("/{username}/{token}/starred.xml", name="starred_rss")
45 * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
46 *
47 * @return \Symfony\Component\HttpFoundation\Response
48 */
49 public function showStarredAction(User $user)
50 {
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 }
83
84 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
85 $entries = new Pagerfanta($pagerAdapter);
86
87 $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
88 $entries->setMaxPerPage($perPage);
89
90 return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
91 'type' => $type,
92 'entries' => $entries,
93 ));
94 }
95 }