ArticleController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 34.88 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 10.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 9
dl 45
loc 129
ccs 6
cts 56
cp 0.1071
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B deleteArticleAction() 0 37 3
A roleAction() 10 10 1
B editArticleAction() 35 35 4

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
namespace AppBundle\Controller\Admin;
4
5
use AppBundle\Entity\Article;
6
use AppBundle\Form\Type\ArticleType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Class UserController
16
 * @package AppBundle\Controller\Admin
17
 * @Route("/admin")
18
 */
19
class ArticleController extends Controller
20
{
21
    /**
22
     * @param Request $request
23
     * @param $page
24
     * @Method({"GET", "POST"})
25
     * @Route("/articles/{pager}/{page}", name="articlesAdmin",
26
     *     defaults={"pager": "page", "page": 1},
27
     *     requirements={
28
     *          "pager": "page",
29
     *          "page": "[1-9]\d*",
30
     *     })
31
     * @Template("AppBundle:admin:articles.html.twig")
32
     *
33
     * @return Response
34
     */
35 1 View Code Duplication
    public function roleAction(Request $request, $page = 1)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
36
    {
37 1
        $em = $this->getDoctrine()->getManager();
38 1
        $articles = $em->getRepository("AppBundle:Article")
39 1
            ->getArticlesWithCountComment($page);
40
41
        return [
42 1
            'articles'  => $articles,
43 1
        ];
44
    }
45
46
    /**
47
     * @param $id
48
     * @param $action
49
     * @param Request $request
50
     * @Route("/article/{action}/{id}", name="articleEdit",
51
     *     defaults={"id": 0},
52
     *     requirements={
53
     *      "action": "new|edit",
54
     *      "id": "\d+"
55
     *     })
56
     * @Method({"GET", "POST"})
57
     * @Template("AppBundle:admin/form:article.html.twig")
58
     *
59
     * @return Response
60
     */
61 View Code Duplication
    public function editArticleAction($id, $action, 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...
62
    {
63
        $em = $this->getDoctrine()->getManager();
64
        if ($action == "edit") {
65
            $article = $em->getRepository('AppBundle:Article')
66
                ->find($id);
67
            $title = 'Edit article id: '.$id;
68
        }
69
        else {
70
            $article = new Article();
71
            $title = 'Create new article';
72
        }
73
74
        $form = $this->createForm(ArticleType::class, $article, [
75
            'em'        => $em,
76
            'action'    => $this->generateUrl('articleEdit', ['action' => $action, 'id' => $id]),
77
            'method'    => Request::METHOD_POST,
78
        ])
79
            ->add('save', SubmitType::class, array('label' => 'Save'));
80
81
        if ($request->getMethod() == 'POST') {
82
            $form->handleRequest($request);
83
            if ($form->isValid()) {
84
                $em->persist($article);
85
                $em->flush();
86
87
                return $this->redirectToRoute('articlesAdmin');
88
            }
89
        }
90
91
        return [
92
            'title' => $title,
93
            'form'  => $form->createView(),
94
        ];
95
    }
96
97
    /**
98
     * @param $id
99
     * @param Request $request
100
     * @Route("/article/delete/{id}", name="articleDelete",
101
     *     requirements={
102
     *      "id": "\d+"
103
     *     })
104
     * @Method({"GET", "POST"})
105
     * @Template("AppBundle:admin/form:delete.html.twig")
106
     *
107
     * @return Response
108
     */
109
    public function deleteArticleAction($id, Request $request)
110
    {
111
        $em = $this->getDoctrine()->getManager();
112
        $article = $em->getRepository('AppBundle:Article')
113
            ->find($id);
114
        $message = 'You want to delete article "' . $article->getTitle() . '" (id: ' . $id . '). ';
115
        $message .= 'Related records will be deleted: comments (count: ' . count($article->getComments()) . '). ';
116
        $message .= 'Are you sure, you want to continue?';
117
118
119
        $form = $this->createFormBuilder($article)
120
            ->setAction($this->generateUrl('articleDelete', ['id' => $id]))
121
            ->setMethod('POST')
122
            ->add('delete', SubmitType::class, array(
123
                    'label'     => 'Continue',
124
                    'attr'      => [
125
                        'class' => 'btn btn-default'
126
                    ],
127
                )
128
            )
129
            ->getForm();
130
131
        if ($request->getMethod() == 'POST') {
132
            $form->handleRequest($request);
133
            if ($form->isValid()) {
134
                $em->remove($article);
135
                $em->flush();
136
137
                return $this->redirectToRoute('articlesAdmin');
138
            }
139
        }
140
141
        return [
142
            'message' => $message,
143
            'form'  => $form->createView(),
144
        ];
145
    }
146
147
}
148