]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/RssController.php
Convert array + phpDoc
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
index 8428dce0029799efead316ab0c12d693de4ef41e..2118885c16f7607e7cbcd4fd8b452da4c9306df6 100644 (file)
@@ -2,11 +2,13 @@
 
 namespace Wallabag\CoreBundle\Controller;
 
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Pagerfanta\Adapter\DoctrineORMAdapter;
+use Pagerfanta\Pagerfanta;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-use Wallabag\CoreBundle\Entity\User;
 use Wallabag\CoreBundle\Entity\Entry;
+use Wallabag\UserBundle\Entity\User;
 
 class RssController extends Controller
 {
@@ -14,74 +16,80 @@ class RssController extends Controller
      * Shows unread entries for current user.
      *
      * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
-     * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
+     * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
     public function showUnreadAction(User $user)
     {
-        $entries = $this->getDoctrine()
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findUnreadByUser(
-                $user->getId()
-            );
-
-        $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
-        $entries->setMaxPerPage($perPage);
-
-        return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
-            'type' => 'unread',
-            'entries' => $entries,
-        ));
+        return $this->showEntries('unread', $user);
     }
 
     /**
      * Shows read entries for current user.
      *
      * @Route("/{username}/{token}/archive.xml", name="archive_rss")
-     * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
+     * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
     public function showArchiveAction(User $user)
     {
-        $entries = $this->getDoctrine()
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findArchiveByUser(
-                $user->getId()
-            );
-
-        $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
-        $entries->setMaxPerPage($perPage);
-
-        return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
-            'type' => 'archive',
-            'entries' => $entries,
-        ));
+        return $this->showEntries('archive', $user);
     }
 
     /**
      * Shows starred entries for current user.
      *
      * @Route("/{username}/{token}/starred.xml", name="starred_rss")
-     * @ParamConverter("user", class="WallabagCoreBundle:User", converter="username_rsstoken_converter")
+     * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
     public function showStarredAction(User $user)
     {
-        $entries = $this->getDoctrine()
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findStarredByUser(
-                $user->getId()
-            );
+        return $this->showEntries('starred', $user);
+    }
+
+    /**
+     * Global method to retrieve entries depending on the given type
+     * It returns the response to be send.
+     *
+     * @param string $type Entries type: unread, starred or archive
+     * @param User   $user
+     *
+     * @return \Symfony\Component\HttpFoundation\Response
+     */
+    private function showEntries($type, User $user)
+    {
+        $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
+
+        switch ($type) {
+            case 'starred':
+                $qb = $repository->getBuilderForStarredByUser($user->getId());
+                break;
+
+            case 'archive':
+                $qb = $repository->getBuilderForArchiveByUser($user->getId());
+                break;
+
+            case 'unread':
+                $qb = $repository->getBuilderForUnreadByUser($user->getId());
+                break;
+
+            default:
+                throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
+        }
+
+        $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
+        $entries = new Pagerfanta($pagerAdapter);
 
-        $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
+        $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
         $entries->setMaxPerPage($perPage);
 
-        return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
-            'type' => 'starred',
+        return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', [
+            'type' => $type,
             'entries' => $entries,
-        ));
+        ]);
     }
 }