From 0ab7404f93670ed161b67528c1a61c5bfa92d42b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:10:06 +0200 Subject: Refactorize the way to retrieve entries One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain. --- .../CoreBundle/Controller/EntryController.php | 105 ++++++++++----------- .../CoreBundle/Controller/RssController.php | 73 +++++++------- .../CoreBundle/Repository/EntryRepository.php | 47 +++++---- 3 files changed, 117 insertions(+), 108 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 006fa396..dc399b8a 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -113,34 +113,7 @@ class EntryController extends Controller */ public function showUnreadAction(Request $request, $page) { - $form = $this->get('form.factory')->create(new EntryFilterType()); - - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findUnreadByUser($this->getUser()->getId()); - - if ($request->query->has($form->getName())) { - // manually bind values from the request - $form->submit($request->query->get($form->getName())); - - // build the query from the given form object - $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); - } - - $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); - $entries->setCurrentPage($page); - - return $this->render( - 'WallabagCoreBundle:Entry:entries.html.twig', - array( - 'form' => $form->createView(), - 'entries' => $entries, - 'currentPage' => $page, - ) - ); + return $this->showEntries('unread', $request, $page); } /** @@ -155,21 +128,66 @@ class EntryController extends Controller */ public function showArchiveAction(Request $request, $page) { - $form = $this->get('form.factory')->create(new EntryFilterType()); + return $this->showEntries('archive', $request, $page); + } + + /** + * Shows starred entries for current user. + * + * @param Request $request + * @param int $page + * + * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showStarredAction(Request $request, $page) + { + return $this->showEntries('starred', $request, $page); + } - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findArchiveByUser($this->getUser()->getId()); + /** + * 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 Request $request + * @param int $page + * + * @return \Symfony\Component\HttpFoundation\Response + */ + private function showEntries($type, Request $request, $page) + { + $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); + + switch ($type) { + case 'starred': + $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); + break; + + case 'archive': + $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); + break; + + case 'unread': + $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); + break; + + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } + + $form = $this->get('form.factory')->create(new EntryFilterType()); if ($request->query->has($form->getName())) { // manually bind values from the request $form->submit($request->query->get($form->getName())); // build the query from the given form object - $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); + $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); } - $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); $entries = new Pagerfanta($pagerAdapter); $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); @@ -183,25 +201,6 @@ class EntryController extends Controller 'currentPage' => $page, ) ); - } - - /** - * Shows starred entries for current user. - * - * @param Request $request - * @param int $page - * - * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showStarredAction(Request $request, $page) - { - $form = $this->get('form.factory')->create(new EntryFilterType()); - - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findStarredByUser($this->getUser()->getId()); if ($request->query->has($form->getName())) { // manually bind values from the request diff --git a/src/Wallabag/CoreBundle/Controller/RssController.php b/src/Wallabag/CoreBundle/Controller/RssController.php index 0558c53b..6121f361 100644 --- a/src/Wallabag/CoreBundle/Controller/RssController.php +++ b/src/Wallabag/CoreBundle/Controller/RssController.php @@ -22,22 +22,7 @@ class RssController extends Controller */ public function showUnreadAction(User $user) { - $qb = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findUnreadByUser( - $user->getId() - ); - - $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $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); } /** @@ -50,22 +35,7 @@ class RssController extends Controller */ public function showArchiveAction(User $user) { - $qb = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findArchiveByUser( - $user->getId() - ); - - $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $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); } /** @@ -78,11 +48,38 @@ class RssController extends Controller */ public function showStarredAction(User $user) { - $qb = $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); @@ -91,7 +88,7 @@ class RssController extends Controller $entries->setMaxPerPage($perPage); return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array( - 'type' => 'starred', + 'type' => $type, 'entries' => $entries, )); } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f885ee94..5538ae82 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -9,19 +9,34 @@ use Pagerfanta\Pagerfanta; class EntryRepository extends EntityRepository { /** - * Retrieves unread entries for a user. + * Return a query builder to used by other getBuilderFor* method. * * @param int $userId * * @return QueryBuilder */ - public function findUnreadByUser($userId) + private function getBuilderByUser($userId) { return $this->createQueryBuilder('e') ->leftJoin('e.user', 'u') - ->where('e.isArchived = false') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + ->andWhere('u.id = :userId')->setParameter('userId', $userId) + ->orderBy('e.id', 'desc') + ; + } + + /** + * Retrieves unread entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForUnreadByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = false') + ; } /** @@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findArchiveByUser($userId) + public function getBuilderForArchiveByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isArchived = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = true') + ; } /** @@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findStarredByUser($userId) + public function getBuilderForStarredByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isStarred = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isStarred = true') + ; } /** -- cgit v1.2.3