One place to retrieve entries in Entry & Rss controller.
More simple and easy to maintain.
*/
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);
}
/**
*/
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());
'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
*/
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);
}
/**
*/
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);
}
/**
*/
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);
$entries->setMaxPerPage($perPage);
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
- 'type' => 'starred',
+ 'type' => $type,
'entries' => $entries,
));
}
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')
+ ;
}
/**
*
* @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')
+ ;
}
/**
*
* @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')
+ ;
}
/**