Completed
Pull Request — master (#2)
by Andrew
02:42
created

AdminCommentController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 30.49 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 8
c 2
b 0
f 2
lcom 1
cbo 7
dl 25
loc 82
ccs 0
cts 49
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 12 12 1
A showCommentAction() 0 6 1
A enableCommentAction() 0 8 1
A deleteCommentAction() 13 13 3
A deleteForm() 0 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
use Symfony\Component\HttpFoundation\Response;
20
21
22
/**
23
 * @Security("has_role('ROLE_MANAGER')")
24
 * @Route("/admin")
25
 */
26
class AdminCommentController extends Controller
27
{
28
    /**
29
     * @Route("/comments", name="admin_comments")
30
     */
31 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...
32
    {
33
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
34
        $paginator = $this->get('knp_paginator');
35
        $pagination = $paginator->paginate(
36
            $comments,
37
            $request->query->getInt('page', 1),
38
            20
39
        );
40
        return $this->render('AppBundle::admin/comment/comments.html.twig', array('pagination' => $pagination));
41
42
    }
43
44
    /**
45
     * @Route("/comment/show/{id}", name="admin_comment_show")
46
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
47
     */
48
    public function showCommentAction(Request $request, Comment $comment)
49
    {
50
        $form = $this->deleteForm($comment);
51
        return $this->render("AppBundle:admin/comment:show_comment.html.twig", array("comment" => $comment,
52
            'delete_form' => $form->createView(),));
53
    }
54
55
    /**
56
     * @Route("/comment_enable/{id}", name="admin_enable_comment")
57
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
58
     * @Security("has_role('ROLE_ADMIN')")
59
     */
60
    public function enableCommentAction(Request $request, Comment $comment)
61
    {
62
        $comment->setEnabled(true);
63
       // $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
        $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...
65
        $em->flush();
66
        return $this->redirectToRoute('admin_comments');
67
    }
68
69
    /**
70
     * @Route("/comment/delete/{id}", name="admin_comment_delete")
71
     * @Method("DELETE")
72
     * @Security("has_role('ROLE_ADMIN')")
73
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
74
     */
75 View Code Duplication
    public function deleteCommentAction(Request $request, 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...
76
    {
77
        //$comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
79
        $form = $this->deleteForm($comment);
80
        $form->handleRequest($request);
81
        if ($form->isSubmitted() && $form->isValid()) {
82
            $entityManager = $this->getDoctrine()->getManager();
83
            $entityManager->remove($comment);
84
            $entityManager->flush();
85
        }
86
        return $this->redirectToRoute('admin_comments');
87
    }
88
89
    private function deleteForm(Comment $comment)
90
    {
91
        return $this->createFormBuilder()
92
            ->setAction($this->generateUrl('admin_comment_delete', array('id' => $comment->getId())))
93
            ->setMethod('DELETE')
94
            ->getForm()
95
            ;
96
    }
97
98
    /**
99
     * @Route ("/comment/disables", name="count_disables_comments")
100
     */
101
    public function countDisablesCommentsAction(Request $request)
102
    {
103
        $comments = $this->getDoctrine()->getRepository('AppBundle:Comment')->getDisabledComments();
104
        return $this->render("AppBundle:admin/comment:count_disables_comment.html.twig",
105
            array("count_disables_comments" => count($comments)));
106
    }
107
}