Completed
Pull Request — master (#4)
by Alexandre
02:36
created

AdminMenuController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 65.45 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 7
dl 72
loc 110
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 12 1
A categoryEditAction() 20 20 2
A categoryDeleteAction() 16 16 2
A mealEditAction() 20 20 2
A mealDeleteAction() 16 16 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;
4
5
use AppBundle\Entity\Category;
6
use AppBundle\Entity\Meal;
7
use AppBundle\Form\Type\CategoryType;
8
use AppBundle\Form\Type\MealType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class AdminMenuController extends Controller
16
{
17
    /**
18
     * @Route(path="/admin/menu", name="admin_menu_index")
19
     */
20
    public function indexAction()
21
    {
22
        $categories = $this
23
            ->getDoctrine()
24
            ->getRepository(Category::class)
25
            ->findOrderedByPosition()
26
        ;
27
28
        return $this->render('admin_menu/index.html.twig', array(
29
            'categories' => $categories
30
        ));
31
    }
32
33
    /**
34
     * @Route(path="/admin/menu/category/{id}", name="admin_menu_category_edit")
35
     * @ParamConverter
36
     */
37 View Code Duplication
    public function categoryEditAction(Request $request, Category $category)
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...
38
    {
39
        $form = $this->createForm(CategoryType::class, $category);
40
41
        if ($form->handleRequest($request)->isValid()) {
42
            $this->getDoctrine()->getManager()->flush();
43
44
            $this->addFlash('success', sprintf(
45
                'Catégorie "%s" mise à jour.',
46
                $category->getName()
47
            ));
48
49
            return $this->redirectToRoute('admin_menu_index');
50
        }
51
52
        return $this->render('admin_menu/category_edit.html.twig', array(
53
            'category' => $category,
54
            'form' => $form->createView()
55
        ));
56
    }
57
58
    /**
59
     * @Route(path="/admin/menu/category/{id}/delete", name="admin_menu_category_delete")
60
     * @ParamConverter
61
     */
62 View Code Duplication
    public function categoryDeleteAction(Request $request, Category $category)
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...
63
    {
64
        if ($request->isMethod('POST')) {
65
            $em = $this->getDoctrine()->getManager();
66
            $em->remove($category);
67
            $em->flush();
68
69
            $request->getSession()->getFlashBag()->add('success', 'La catégorie '.$category->getName().' a bien été supprimée.');
70
71
            return $this->redirectToRoute('admin_menu_index');
72
        }
73
74
        return $this->render('admin_menu/category_delete.html.twig', array(
75
            'category' => $category
76
        ));
77
    }
78
79
    /**
80
     * @Route(path="/admin/menu/meal/{id}", name="admin_menu_meal_edit")
81
     * @ParamConverter
82
     */
83 View Code Duplication
    public function mealEditAction(Request $request, Meal $meal)
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...
84
    {
85
        $form = $this->createForm(MealType::class, $meal);
86
87
        if ($form->handleRequest($request)->isValid()) {
88
            $this->getDoctrine()->getManager()->flush();
89
90
            $this->addFlash('success', sprintf(
91
                'Plat "%s" mis à jour.',
92
                $meal->getName()
93
            ));
94
95
            return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId()));
96
        }
97
98
        return $this->render('admin_menu/meal_edit.html.twig', array(
99
            'meal' => $meal,
100
            'form' => $form->createView()
101
        ));
102
    }
103
104
    /**
105
     * @Route(path="/admin/menu/meal/{id}/delete", name="admin_menu_meal_delete")
106
     * @ParamConverter
107
     */
108 View Code Duplication
    public function mealDeleteAction(Request $request, Meal $meal)
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...
109
    {
110
        if ($request->isMethod('POST')) {
111
            $em = $this->getDoctrine()->getManager();
112
            $em->remove($meal);
113
            $em->flush();
114
115
            $request->getSession()->getFlashBag()->add('success', 'Le plat '.$meal->getName().' a bien été supprimé.');
116
117
            return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId()));
118
        }
119
120
        return $this->render('admin_menu/meal_delete.html.twig', array(
121
            'meal' => $meal
122
        ));
123
    }
124
}
125