Completed
Pull Request — master (#2)
by Andrew
05:56
created

AdminCommentController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 37.96 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 88.46%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 10
c 3
b 0
f 3
lcom 1
cbo 7
dl 41
loc 108
ccs 46
cts 52
cp 0.8846
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 11 11 1
A allCommentsAction() 11 11 1
A publishedCommentsAction() 11 11 1
A showCommentAction() 0 6 1
A enableCommentAction() 0 7 1
A deleteCommentAction() 0 11 3
A deleteForm() 8 8 1
A countDisablesCommentsAction() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kate
5
 * Date: 04.03.16
6
 * Time: 22:24
7
 */
8
9
namespace AppBundle\Controller\Admin;
10
11
use AppBundle\Entity\Comment;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\Request;
17
use AppBundle\Utils;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
19
20
21
/**
22
 * @Security("has_role('ROLE_MANAGER')")
23
 * @Route("/admin")
24
 */
25
class AdminCommentController extends Controller
26
{
27
    /**
28
     * @Route("/comments", name="admin_comments")
29
     */
30 1 View Code Duplication
    public function indexAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32 1
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
33 1
        $paginator = $this->get('knp_paginator');
34 1
        $pagination = $paginator->paginate(
35 1
            $comments,
36 1
            $request->query->getInt('page', 1),
37
            20
38 1
        );
39 1
        return $this->render('AppBundle::admin/comment/comments.html.twig', array('pagination' => $pagination));
40
    }
41
42
    /**
43
     * @Route("/comments/all", name="admin_all_comments")
44
     */
45 1 View Code Duplication
    public function allCommentsAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47 1
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->findAll();
48 1
        $paginator = $this->get('knp_paginator');
49 1
        $pagination = $paginator->paginate(
50 1
            $comments,
51 1
            $request->query->getInt('page', 1),
52
            20
53 1
        );
54 1
        return $this->render('AppBundle::admin/comment/all_comments.html.twig', array('pagination' => $pagination));
55
    }
56
57
    /**
58
     * @Route("/comments/published", name="admin_published_comments")
59
     */
60 1 View Code Duplication
    public function publishedCommentsAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62 1
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getEnabledComments();
63 1
        $paginator = $this->get('knp_paginator');
64 1
        $pagination = $paginator->paginate(
65 1
            $comments,
66 1
            $request->query->getInt('page', 1),
67
            20
68 1
        );
69 1
        return $this->render('AppBundle:admin/comment:published_comments.html.twig', array('pagination' => $pagination));
70
    }
71
72
    /**
73
     * @Route("/comment/show/{id}", name="admin_comment_show")
74
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
75
     */
76 1
    public function showCommentAction(Request $request, Comment $comment)
77
    {
78 1
        $form = $this->deleteForm($comment);
79 1
        return $this->render("AppBundle:admin/comment:show_comment.html.twig", array("comment" => $comment,
80 1
            'delete_form' => $form->createView(),));
81
    }
82
83
    /**
84
     * @Route("/comment_enable/{id}", name="admin_enable_comment")
85
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
86
     * @Security("has_role('ROLE_ADMIN')")
87
     */
88 1
    public function enableCommentAction(Request $request, Comment $comment)
89
    {
90 1
        $comment->setEnabled(true);
91 1
        $em = $this->getDoctrine()->getEntityManager();
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Bundle\Doctrine...try::getEntityManager() has been deprecated.

This method has been deprecated.

Loading history...
92 1
        $em->flush();
93 1
        return $this->redirectToRoute('admin_comments');
94
    }
95
96
    /**
97
     * @Route("/comment/delete/{id}", name="admin_comment_delete")
98
     * @Method("DELETE")
99
     * @Security("has_role('ROLE_ADMIN')")
100
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
101
     */
102 1
    public function deleteCommentAction(Request $request, Comment $comment)
103
    {
104
        $form = $this->deleteForm($comment);
105
        $form->handleRequest($request);
106
        if ($form->isSubmitted() && $form->isValid()) {
107 1
            $entityManager = $this->getDoctrine()->getManager();
108
            $entityManager->remove($comment);
109
            $entityManager->flush();
110 1
        }
111
        return $this->redirectToRoute('admin_comments');
112
    }
113
114 2 View Code Duplication
    private function deleteForm(Comment $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116 1
        return $this->createFormBuilder()
117 1
            ->setAction($this->generateUrl('admin_comment_delete', array('id' => $comment->getId())))
118 2
            ->setMethod('DELETE')
119 1
            ->getForm()
120 1
            ;
121
    }
122
123
    /**
124
     * @Route (name="count_disables_comments")
125
     */
126 15
    public function countDisablesCommentsAction(Request $request)
127
    {
128 15
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
129 15
        return $this->render("AppBundle:admin/comment:count_disables_comment.html.twig",
130 15
            array("count_disables_comments" => count($comments)));
131
    }
132
}