aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-08-20 20:10:06 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-08-20 20:39:22 +0200
commit0ab7404f93670ed161b67528c1a61c5bfa92d42b (patch)
tree591b52d4c035b75ea59ee597101e998ba0f3e802
parent4fcb7eaf139a4d2cbd0f54574e6c51a0fe852ef1 (diff)
downloadwallabag-0ab7404f93670ed161b67528c1a61c5bfa92d42b.tar.gz
wallabag-0ab7404f93670ed161b67528c1a61c5bfa92d42b.tar.zst
wallabag-0ab7404f93670ed161b67528c1a61c5bfa92d42b.zip
Refactorize the way to retrieve entries
One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain.
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php105
-rw-r--r--src/Wallabag/CoreBundle/Controller/RssController.php73
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php47
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
113 */ 113 */
114 public function showUnreadAction(Request $request, $page) 114 public function showUnreadAction(Request $request, $page)
115 { 115 {
116 $form = $this->get('form.factory')->create(new EntryFilterType()); 116 return $this->showEntries('unread', $request, $page);
117
118 $filterBuilder = $this->getDoctrine()
119 ->getRepository('WallabagCoreBundle:Entry')
120 ->findUnreadByUser($this->getUser()->getId());
121
122 if ($request->query->has($form->getName())) {
123 // manually bind values from the request
124 $form->submit($request->query->get($form->getName()));
125
126 // build the query from the given form object
127 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
128 }
129
130 $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
131 $entries = new Pagerfanta($pagerAdapter);
132
133 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
134 $entries->setCurrentPage($page);
135
136 return $this->render(
137 'WallabagCoreBundle:Entry:entries.html.twig',
138 array(
139 'form' => $form->createView(),
140 'entries' => $entries,
141 'currentPage' => $page,
142 )
143 );
144 } 117 }
145 118
146 /** 119 /**
@@ -155,21 +128,66 @@ class EntryController extends Controller
155 */ 128 */
156 public function showArchiveAction(Request $request, $page) 129 public function showArchiveAction(Request $request, $page)
157 { 130 {
158 $form = $this->get('form.factory')->create(new EntryFilterType()); 131 return $this->showEntries('archive', $request, $page);
132 }
133
134 /**
135 * Shows starred entries for current user.
136 *
137 * @param Request $request
138 * @param int $page
139 *
140 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
141 *
142 * @return \Symfony\Component\HttpFoundation\Response
143 */
144 public function showStarredAction(Request $request, $page)
145 {
146 return $this->showEntries('starred', $request, $page);
147 }
159 148
160 $filterBuilder = $this->getDoctrine() 149 /**
161 ->getRepository('WallabagCoreBundle:Entry') 150 * Global method to retrieve entries depending on the given type
162 ->findArchiveByUser($this->getUser()->getId()); 151 * It returns the response to be send.
152 *
153 * @param string $type Entries type: unread, starred or archive
154 * @param Request $request
155 * @param int $page
156 *
157 * @return \Symfony\Component\HttpFoundation\Response
158 */
159 private function showEntries($type, Request $request, $page)
160 {
161 $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
162
163 switch ($type) {
164 case 'starred':
165 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
166 break;
167
168 case 'archive':
169 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
170 break;
171
172 case 'unread':
173 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
174 break;
175
176 default:
177 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
178 }
179
180 $form = $this->get('form.factory')->create(new EntryFilterType());
163 181
164 if ($request->query->has($form->getName())) { 182 if ($request->query->has($form->getName())) {
165 // manually bind values from the request 183 // manually bind values from the request
166 $form->submit($request->query->get($form->getName())); 184 $form->submit($request->query->get($form->getName()));
167 185
168 // build the query from the given form object 186 // build the query from the given form object
169 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); 187 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
170 } 188 }
171 189
172 $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); 190 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
173 $entries = new Pagerfanta($pagerAdapter); 191 $entries = new Pagerfanta($pagerAdapter);
174 192
175 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); 193 $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
@@ -183,25 +201,6 @@ class EntryController extends Controller
183 'currentPage' => $page, 201 'currentPage' => $page,
184 ) 202 )
185 ); 203 );
186 }
187
188 /**
189 * Shows starred entries for current user.
190 *
191 * @param Request $request
192 * @param int $page
193 *
194 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
195 *
196 * @return \Symfony\Component\HttpFoundation\Response
197 */
198 public function showStarredAction(Request $request, $page)
199 {
200 $form = $this->get('form.factory')->create(new EntryFilterType());
201
202 $filterBuilder = $this->getDoctrine()
203 ->getRepository('WallabagCoreBundle:Entry')
204 ->findStarredByUser($this->getUser()->getId());
205 204
206 if ($request->query->has($form->getName())) { 205 if ($request->query->has($form->getName())) {
207 // manually bind values from the request 206 // 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
22 */ 22 */
23 public function showUnreadAction(User $user) 23 public function showUnreadAction(User $user)
24 { 24 {
25 $qb = $this->getDoctrine() 25 return $this->showEntries('unread', $user);
26 ->getRepository('WallabagCoreBundle:Entry')
27 ->findUnreadByUser(
28 $user->getId()
29 );
30
31 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
32 $entries = new Pagerfanta($pagerAdapter);
33
34 $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
35 $entries->setMaxPerPage($perPage);
36
37 return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
38 'type' => 'unread',
39 'entries' => $entries,
40 ));
41 } 26 }
42 27
43 /** 28 /**
@@ -50,22 +35,7 @@ class RssController extends Controller
50 */ 35 */
51 public function showArchiveAction(User $user) 36 public function showArchiveAction(User $user)
52 { 37 {
53 $qb = $this->getDoctrine() 38 return $this->showEntries('archive', $user);
54 ->getRepository('WallabagCoreBundle:Entry')
55 ->findArchiveByUser(
56 $user->getId()
57 );
58
59 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
60 $entries = new Pagerfanta($pagerAdapter);
61
62 $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
63 $entries->setMaxPerPage($perPage);
64
65 return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
66 'type' => 'archive',
67 'entries' => $entries,
68 ));
69 } 39 }
70 40
71 /** 41 /**
@@ -78,11 +48,38 @@ class RssController extends Controller
78 */ 48 */
79 public function showStarredAction(User $user) 49 public function showStarredAction(User $user)
80 { 50 {
81 $qb = $this->getDoctrine() 51 return $this->showEntries('starred', $user);
82 ->getRepository('WallabagCoreBundle:Entry') 52 }
83 ->findStarredByUser( 53
84 $user->getId() 54 /**
85 ); 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 }
86 83
87 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); 84 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
88 $entries = new Pagerfanta($pagerAdapter); 85 $entries = new Pagerfanta($pagerAdapter);
@@ -91,7 +88,7 @@ class RssController extends Controller
91 $entries->setMaxPerPage($perPage); 88 $entries->setMaxPerPage($perPage);
92 89
93 return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array( 90 return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
94 'type' => 'starred', 91 'type' => $type,
95 'entries' => $entries, 92 'entries' => $entries,
96 )); 93 ));
97 } 94 }
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;
9class EntryRepository extends EntityRepository 9class EntryRepository extends EntityRepository
10{ 10{
11 /** 11 /**
12 * Retrieves unread entries for a user. 12 * Return a query builder to used by other getBuilderFor* method.
13 * 13 *
14 * @param int $userId 14 * @param int $userId
15 * 15 *
16 * @return QueryBuilder 16 * @return QueryBuilder
17 */ 17 */
18 public function findUnreadByUser($userId) 18 private function getBuilderByUser($userId)
19 { 19 {
20 return $this->createQueryBuilder('e') 20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u') 21 ->leftJoin('e.user', 'u')
22 ->where('e.isArchived = false') 22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 23 ->orderBy('e.id', 'desc')
24 ->orderBy('e.id', 'desc'); 24 ;
25 }
26
27 /**
28 * Retrieves unread entries for a user.
29 *
30 * @param int $userId
31 *
32 * @return QueryBuilder
33 */
34 public function getBuilderForUnreadByUser($userId)
35 {
36 return $this
37 ->getBuilderByUser($userId)
38 ->andWhere('e.isArchived = false')
39 ;
25 } 40 }
26 41
27 /** 42 /**
@@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository
31 * 46 *
32 * @return QueryBuilder 47 * @return QueryBuilder
33 */ 48 */
34 public function findArchiveByUser($userId) 49 public function getBuilderForArchiveByUser($userId)
35 { 50 {
36 return $this->createQueryBuilder('e') 51 return $this
37 ->leftJoin('e.user', 'u') 52 ->getBuilderByUser($userId)
38 ->where('e.isArchived = true') 53 ->andWhere('e.isArchived = true')
39 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 54 ;
40 ->orderBy('e.id', 'desc');
41 } 55 }
42 56
43 /** 57 /**
@@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository
47 * 61 *
48 * @return QueryBuilder 62 * @return QueryBuilder
49 */ 63 */
50 public function findStarredByUser($userId) 64 public function getBuilderForStarredByUser($userId)
51 { 65 {
52 return $this->createQueryBuilder('e') 66 return $this
53 ->leftJoin('e.user', 'u') 67 ->getBuilderByUser($userId)
54 ->where('e.isStarred = true') 68 ->andWhere('e.isStarred = true')
55 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 69 ;
56 ->orderBy('e.id', 'desc');
57 } 70 }
58 71
59 /** 72 /**