CategoryController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 29.63 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.36%

Importance

Changes 4
Bugs 0 Features 4
Metric Value
wmc 8
c 4
b 0
f 4
lcom 1
cbo 8
dl 32
loc 108
ccs 42
cts 47
cp 0.8936
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createCategoryAction() 0 18 2
A editCategoryAction() 18 18 2
A removeCategoryAction() 0 13 1
A createFormDelete() 14 14 1
A showCategoryAction() 0 19 2

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\Category;
6
use AppBundle\Form\CategoryType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Class CategoryController
16
 * @package AppBundle\Controller\Admin
17
 */
18
class CategoryController extends Controller
19
{
20
    /**
21
     * @Route("/admin/category/new", name="create_category")
22
     * @Template("@App/admin/category/createCategory.html.twig")
23
     */
24 1
    public function createCategoryAction(Request $request)
25
    {
26 1
        $em = $this->getDoctrine()->getManager();
27
28 1
        $category = new Category();
29 1
        $form = $this->createForm(CategoryType::class, $category);
30
31 1
        $form->handleRequest($request);
32
33 1
        if ($form->isValid()) {
34
            $em->persist($category);
35
            $em->flush();
36
37
            return $this->redirectToRoute('show_category');
38
        }
39
40 1
        return ['form' => $form->createView()];
41
    }
42
43
    /**
44
     * @Route("/admin/category/edit/{id}", name="edit_category")
45
     * @Template("@App/admin/category/editCategory.html.twig")
46
     */
47 1 View Code Duplication
    public function editCategoryAction(Request $request, $id)
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...
48
    {
49 1
        $em = $this->getDoctrine()->getManager();
50
51 1
        $category = $em->getRepository('AppBundle:Category')
52 1
            ->find($id);
53 1
        $form = $this->createForm(CategoryType::class, $category);
54
55 1
        $form->handleRequest($request);
56
57 1
        if ($form->isValid()) {
58
            $em->flush();
59
60
            return $this->redirectToRoute('show_category');
61
        }
62
63 1
        return ['form' => $form->createView()];
64
    }
65
66
    /**
67
     * @Route("/admin/category/remove/{id}", name="remove_category")
68
     * @Method("DELETE")
69
     */
70 1
    public function removeCategoryAction($id)
71
    {
72 1
        $em = $this->getDoctrine()->getManager();
73
74 1
        $category = $em->getRepository('AppBundle:Category')
75 1
            ->find($id);
76
77 1
        $em->remove($category);
78 1
        $em->flush();
79
80 1
        return $this->redirectToRoute('show_category');
81
82
    }
83
84
    /**
85
     * @Route("/admin/category", name="show_category")
86
     * @Template("@App/admin/category/showCategory.html.twig")
87
     */
88 1
    public function showCategoryAction()
89
    {
90 1
        $em = $this->getDoctrine()->getManager();
91
92 1
        $category = $em->getRepository('AppBundle:Category')
93 1
            ->findBy([], ['title' => 'ASC']);
94
95 1
        $form_delete = [];
96
97 1
        foreach ($category as $item) {
98 1
            $form_delete[$item->getId()] = $this->createFormDelete($item->getId())->createView();
99 1
        }
100
101
        return [
102 1
            'categories' => $category,
103
            'form_remove' => $form_delete
104 1
        ];
105
106
    }
107
108
    /**
109
     * @return \Symfony\Component\Form\Form
110
     */
111 1 View Code Duplication
    private function createFormDelete($id)
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...
112
    {
113 1
        return $this->createFormBuilder()
114 1
            ->setAction($this->generateUrl('remove_category', ['id' => $id]))
115 1
            ->setMethod('DELETE')
116 1
            ->add('submit', SubmitType::class, [
117 1
                'label' => ' ',
118
                'attr' => [
119 1
                    'class' => 'glyphicon glyphicon-remove btn-link',
120
                    'onclick' => 'return confirm("Are you sure?")'
121 1
                ]
122 1
            ])
123 1
            ->getForm();
124
    }
125
}
126
127