Complex classes like AdminController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class AdminController extends BaseController |
||
24 | { |
||
25 | /** |
||
26 | * @Route("/admin/paywall_plugin") |
||
27 | * @Route("/admin/paywall_plugin/update/{id}", name="newscoop_paywall_admin_update", options={"expose"=true}) |
||
28 | * @Template() |
||
29 | */ |
||
30 | public function adminAction(Request $request, $id = null) |
||
31 | { |
||
32 | $em = $this->getDoctrine()->getManager(); |
||
33 | if ($id) { |
||
34 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
35 | $subscription = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
36 | ->findActiveOneBy($id); |
||
37 | |||
38 | if (!$subscription) { |
||
39 | return $this->redirect($this->generateUrl('newscoop_paywall_managesubscriptions_manage')); |
||
40 | } |
||
41 | |||
42 | $specification = $em->getRepository('Newscoop\PaywallBundle\Entity\SubscriptionSpecification') |
||
43 | ->findOneBy(array( |
||
44 | 'subscription' => $subscription, |
||
45 | )); |
||
46 | } else { |
||
47 | $subscription = new Subscription(); |
||
48 | $specification = new SubscriptionSpecification(); |
||
49 | } |
||
50 | |||
51 | $currencyProvider = $this->get('newscoop_paywall.currency_provider'); |
||
52 | $defaultCurrency = $currencyProvider->getDefaultCurrency(); |
||
53 | if ($defaultCurrency) { |
||
54 | $defaultCurrency = $defaultCurrency->getCode(); |
||
55 | } |
||
56 | $form = $this->createForm('subscriptionconf', $subscription); |
||
57 | $subscription->setCurrency($defaultCurrency); |
||
58 | $formSpecification = $this->createForm('specificationForm', $specification); |
||
59 | $durationForm = $this->createForm(new DurationType()); |
||
60 | if ($request->isMethod('POST')) { |
||
61 | $this->hasPermission(Permissions::SUBSCRIPTION_ADD); |
||
62 | $form->bind($request); |
||
63 | if ($form->isValid()) { |
||
64 | $em = $this->getDoctrine()->getManager(); |
||
65 | if (!$id) { |
||
66 | $em->persist($subscription); |
||
67 | } |
||
68 | $em->flush(); |
||
69 | |||
70 | if ($request->isXmlHttpRequest()) { |
||
71 | return new JsonResponse(array('status' => true)); |
||
72 | } |
||
73 | |||
74 | return $this->redirect($this->generateUrl('newscoop_paywall_managesubscriptions_manage')); |
||
75 | } else { |
||
76 | if ($request->isXmlHttpRequest()) { |
||
77 | return new JsonResponse(array( |
||
78 | 'status' => false, |
||
79 | 'errors' => $this->getErrorMessages($form), |
||
80 | )); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return array( |
||
86 | 'form' => $form->createView(), |
||
87 | 'formSpecification' => $formSpecification->createView(), |
||
88 | 'subscription_id' => $subscription->getId(), |
||
89 | 'ranges' => $subscription->getRanges()->toArray(), |
||
90 | 'formDuration' => $durationForm->createView(), |
||
91 | 'defaultCurrency' => $defaultCurrency, |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @Route("/admin/paywall_plugin/duration/update/{id}", name="newscoop_paywall_admin_duration") |
||
97 | * |
||
98 | * @Method("POST") |
||
99 | */ |
||
100 | public function durationAction(Request $request, $id = null) |
||
101 | { |
||
102 | $em = $this->getDoctrine()->getManager(); |
||
103 | $duration = new Duration(); |
||
104 | $form = $this->createForm(new DurationType(), $duration); |
||
105 | $form->handleRequest($request); |
||
106 | $errors = array(); |
||
107 | try { |
||
108 | if ($form->isValid()) { |
||
109 | if (is_null($id)) { |
||
110 | $this->hasPermission(Permissions::SUBSCRIPTION_ADD); |
||
111 | $subscription = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
112 | ->findOneBy(array( |
||
113 | 'name' => $request->get('subscriptionName'), |
||
114 | 'is_active' => true, |
||
115 | )); |
||
116 | |||
117 | $id = $subscription->getId(); |
||
118 | } |
||
119 | |||
120 | $durationEntity = $em->getRepository('Newscoop\PaywallBundle\Entity\Duration') |
||
121 | ->findOneBy(array( |
||
122 | 'value' => $duration->getValue(), |
||
123 | 'subscription' => $id, |
||
124 | )); |
||
125 | |||
126 | if (!$durationEntity) { |
||
127 | $subscription = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
128 | ->findOneBy(array( |
||
129 | 'id' => $id, |
||
130 | 'is_active' => true, |
||
131 | )); |
||
132 | |||
133 | $duration->setSubscription($subscription); |
||
134 | $em->persist($duration); |
||
135 | $em->flush(); |
||
136 | |||
137 | $discount = array(); |
||
138 | if ($duration->getDiscount()) { |
||
139 | $discount = array( |
||
140 | 'name' => $duration->getDiscount()->getName(), |
||
141 | 'value' => $duration->getDiscount()->getValue(), |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | return new JsonResponse(array( |
||
146 | 'status' => true, |
||
147 | 'duration' => array( |
||
148 | 'id' => $duration->getId(), |
||
149 | 'value' => $duration->getValue(), |
||
150 | 'attribute' => $duration->getAttribute(), |
||
151 | 'discount' => $discount, |
||
152 | ), |
||
153 | )); |
||
154 | } |
||
155 | } else { |
||
156 | $errors = $this->getErrorMessages($form); |
||
157 | } |
||
158 | } catch (\Exception $e) { |
||
159 | //return false status |
||
160 | } |
||
161 | |||
162 | return new JsonResponse(array( |
||
163 | 'status' => false, |
||
164 | 'errors' => $errors, |
||
165 | )); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @Route("/admin/paywall_plugin/duration/remove/{id}", name="newscoop_paywall_admin_duration_remove") |
||
170 | * |
||
171 | * @Method("POST") |
||
172 | */ |
||
173 | public function durationRemoveAction(Request $request, $id = null) |
||
174 | { |
||
175 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
176 | $em = $this->getDoctrine()->getManager(); |
||
177 | $duration = $em->getRepository('Newscoop\PaywallBundle\Entity\Duration') |
||
178 | ->findOneById($id); |
||
179 | |||
180 | try { |
||
181 | if ($duration) { |
||
182 | $em->remove($duration); |
||
183 | $em->flush(); |
||
184 | |||
185 | return new JsonResponse(array( |
||
186 | 'status' => true, |
||
187 | )); |
||
188 | } |
||
189 | |||
190 | return new JsonResponse(array( |
||
191 | 'status' => false, |
||
192 | )); |
||
193 | } catch (\Exception $e) { |
||
194 | return new JsonResponse(array( |
||
195 | 'status' => false, |
||
196 | 'message' => 'Cannot remove this period because its assigned for some user subscriptions', |
||
197 | )); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @Route("/admin/paywall_plugin/step2") |
||
203 | * @Route("/admin/paywall_plugin/step2/update/{id}", name="newscoop_paywall_admin_step2") |
||
204 | */ |
||
205 | public function step2Action(Request $request, $id = null) |
||
206 | { |
||
207 | $em = $this->getDoctrine()->getManager(); |
||
208 | $create = false; |
||
209 | if ($id) { |
||
210 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
211 | |||
212 | $specification = $em->getRepository('Newscoop\PaywallBundle\Entity\SubscriptionSpecification') |
||
213 | ->findOneBy(array( |
||
214 | 'subscription' => $id, |
||
215 | )); |
||
216 | if (!$specification) { |
||
217 | $specification = new SubscriptionSpecification(); |
||
218 | $create = true; |
||
219 | } |
||
220 | } else { |
||
221 | $this->hasPermission(Permissions::SUBSCRIPTION_ADD); |
||
222 | $specification = new SubscriptionSpecification(); |
||
223 | $create = true; |
||
224 | } |
||
225 | |||
226 | $formSpecification = $this->createForm('specificationForm', $specification); |
||
227 | if ($request->isMethod('POST')) { |
||
228 | $formSpecification->bind($request); |
||
229 | if ($formSpecification->isValid()) { |
||
230 | $subscription = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
231 | ->findOneBy(array( |
||
232 | 'id' => $id, |
||
233 | 'is_active' => true, |
||
234 | )); |
||
235 | |||
236 | if (null === $subscription) { |
||
237 | $subscription = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
238 | ->findOneBy(array( |
||
239 | 'name' => strtolower($request->request->get('subscriptionTitle')), |
||
240 | 'is_active' => true, |
||
241 | )); |
||
242 | } |
||
243 | |||
244 | $specification->setSubscription($subscription); |
||
245 | if (!$id || $create) { |
||
246 | $em->persist($specification); |
||
247 | } |
||
248 | $em->flush(); |
||
249 | |||
250 | return $this->redirect($this->generateUrl('newscoop_paywall_managesubscriptions_manage')); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @Route("/admin/paywall_plugin/check", options={"expose"=true}) |
||
257 | */ |
||
258 | public function checkAction(Request $request) |
||
259 | { |
||
260 | if ($request->isMethod('POST')) { |
||
261 | $em = $this->getDoctrine()->getManager(); |
||
262 | $entity = $em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
263 | ->findOneBy(array( |
||
264 | 'name' => strtolower($request->request->get('subscriptionName')), |
||
265 | 'is_active' => true, |
||
266 | )); |
||
267 | |||
268 | if (!$entity) { |
||
269 | return new Response(json_encode(array('status' => true))); |
||
270 | } |
||
271 | |||
272 | return new Response(json_encode(array('status' => false))); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @Route("/admin/paywall_plugin/getall", options={"expose"=true}) |
||
278 | */ |
||
279 | public function getAllAction(Request $request) |
||
280 | { |
||
281 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
282 | |||
283 | return new Response(json_encode($this->getAll($request, $this->getDoctrine()->getManager()))); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @Route("/admin/paywall_plugin/getpublications", options={"expose"=true}) |
||
288 | */ |
||
289 | public function getPublicationsAction(Request $request) |
||
290 | { |
||
291 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
292 | |||
293 | return new Response(json_encode($this->getPublication($this->getDoctrine()->getManager()))); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @Route("/admin/paywall_plugin/getissues", options={"expose"=true}) |
||
298 | */ |
||
299 | public function getIssuesAction(Request $request) |
||
300 | { |
||
301 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
302 | |||
303 | return new Response(json_encode($this->getIssue($request, $this->getDoctrine()->getManager()))); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @Route("/admin/paywall_plugin/getsections", options={"expose"=true}) |
||
308 | */ |
||
309 | public function getSectionsAction(Request $request) |
||
310 | { |
||
311 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
312 | |||
313 | return new Response(json_encode($this->getSection($request, $this->getDoctrine()->getManager()))); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @Route("/admin/paywall_plugin/getarticles", options={"expose"=true}) |
||
318 | */ |
||
319 | public function getArticlesAction(Request $request) |
||
320 | { |
||
321 | $this->hasPermission(Permissions::SUBSCRIPTIONS_MANAGE); |
||
322 | |||
323 | return new Response(json_encode($this->getArticle($request, $this->getDoctrine()->getManager()))); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Gets form errors. |
||
328 | * |
||
329 | * @param \Symfony\Component\Form\Form $form |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | private function getErrorMessages(\Symfony\Component\Form\Form $form) |
||
350 | |||
351 | /** |
||
352 | * Gets all publications. |
||
353 | * |
||
354 | * @param Doctrine\ORM\EntityManager $em |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | private function getPublication($em) |
||
359 | { |
||
360 | $publications = $em->getRepository('Newscoop\Entity\Publication') |
||
361 | ->createQueryBuilder('p') |
||
362 | ->select('p.id', 'p.name', 'l.code') |
||
363 | ->leftJoin('p.language', 'l') |
||
364 | ->groupBy('p.id') |
||
365 | ->getQuery() |
||
366 | ->getArrayResult(); |
||
367 | |||
368 | return $publications; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Gets all issues for given publication Id. |
||
373 | * |
||
374 | * @param Doctrine\ORM\EntityManager $em |
||
375 | * @param Symfony\Component\HttpFoundation\Request $request |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | private function getIssue($request, $em) |
||
380 | { |
||
381 | $issues = $em->getRepository('Newscoop\Entity\Issue') |
||
382 | ->createQueryBuilder('i') |
||
383 | ->select('i.number as id', 'i.name', 'l.code') |
||
384 | ->leftJoin('i.language', 'l') |
||
385 | ->where('i.publication = ?1') |
||
386 | ->setParameter(1, $request->get('publicationId')) |
||
387 | ->groupBy('i.number') |
||
388 | ->getQuery() |
||
389 | ->getArrayResult(); |
||
390 | |||
391 | return $issues; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Gets all sections for given publication and issue Id. |
||
396 | * |
||
397 | * @param Doctrine\ORM\EntityManager $em |
||
398 | * @param Symfony\Component\HttpFoundation\Request $request |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | private function getSection($request, $em) |
||
403 | { |
||
404 | $sections = $em->getRepository('Newscoop\Entity\Section') |
||
405 | ->createQueryBuilder('s') |
||
406 | ->select('s.number as id', 's.name', 'l.code') |
||
407 | ->innerJoin('s.issue', 'i', 'WITH', 'i.number = ?2') |
||
408 | ->leftJoin('s.language', 'l') |
||
409 | ->where('s.publication = ?1') |
||
410 | ->setParameter(1, $request->get('publicationId')) |
||
411 | ->setParameter(2, $request->get('issueId')) |
||
412 | ->groupBy('s.number') |
||
413 | ->getQuery() |
||
414 | ->getArrayResult(); |
||
415 | |||
416 | return $sections; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Gets all articles for given publication, issue, section Id. |
||
421 | * |
||
422 | * @param Doctrine\ORM\EntityManager $em |
||
423 | * @param Symfony\Component\HttpFoundation\Request $request |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | private function getArticle($request, $em) |
||
428 | { |
||
429 | $articles = $em->getRepository('Newscoop\Entity\Article') |
||
430 | ->createQueryBuilder('s') |
||
431 | ->select('s.number as id', 's.name', 'l.code') |
||
432 | ->leftJoin('s.issue', 'i') |
||
433 | ->leftJoin('s.publication', 'p') |
||
434 | ->leftJoin('s.section', 'ss') |
||
435 | ->leftJoin('s.language', 'l') |
||
436 | ->where('p.id = :publicationId AND ss.number = :sectionId AND i.number = :issueId') |
||
437 | ->setParameters(array( |
||
438 | 'publicationId' => $request->get('publicationId'), |
||
439 | 'issueId' => $request->get('issueId'), |
||
440 | 'sectionId' => $request->get('sectionId'), |
||
441 | )) |
||
442 | ->groupBy('s.number') |
||
443 | ->getQuery() |
||
444 | ->getArrayResult(); |
||
445 | |||
446 | return $articles; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Gets all publications, issues, sections, articles. |
||
451 | * |
||
452 | * @param Doctrine\ORM\EntityManager $em |
||
453 | * @param Symfony\Component\HttpFoundation\Request $request |
||
454 | * |
||
455 | * @return array |
||
456 | */ |
||
457 | private function getAll($request, $em) |
||
468 | } |
||
469 |