Completed
Push — master ( 1a6243...05b324 )
by Alexandre
09:32 queued 07:17
created

AdminMenuController::mealEditAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
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/create-category", name="admin_menu_category_create")
60
     * @ParamConverter
61
     */
62
    public function categoryCreateAction(Request $request)
63
    {
64
        $form = $this->createForm(CategoryType::class);
65
66
        if ($form->handleRequest($request)->isValid()) {
67
            $em = $this->getDoctrine()->getManager();
68
69
            $category = $form->getData();
70
71
            $em->persist($category);
72
            $em->flush();
73
74
            $this->addFlash('success', sprintf(
75
                'Catégorie "%s" créée.',
76
                $category->getName()
77
            ));
78
79
            return $this->redirectToRoute('admin_menu_index');
80
        }
81
82
        return $this->render('admin_menu/category_create.html.twig', array(
83
            'form' => $form->createView()
84
        ));
85
    }
86
87
    /**
88
     * @Route(path="/admin/menu/category/{id}/delete", name="admin_menu_category_delete")
89
     * @ParamConverter
90
     */
91 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...
92
    {
93
        if ($request->isMethod('POST')) {
94
            $em = $this->getDoctrine()->getManager();
95
            $em->remove($category);
96
            $em->flush();
97
98
            $request->getSession()->getFlashBag()->add('success', 'La catégorie '.$category->getName().' a bien été supprimée.');
99
100
            return $this->redirectToRoute('admin_menu_index');
101
        }
102
103
        return $this->render('admin_menu/category_delete.html.twig', array(
104
            'category' => $category
105
        ));
106
    }
107
108
    /**
109
     * @Route(path="/admin/menu/meal/{id}", name="admin_menu_meal_edit")
110
     * @ParamConverter
111
     */
112 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...
113
    {
114
        $form = $this->createForm(MealType::class, $meal);
115
116
        if ($form->handleRequest($request)->isValid()) {
117
            $this->getDoctrine()->getManager()->flush();
118
119
            $this->addFlash('success', sprintf(
120
                'Plat "%s" mis à jour.',
121
                $meal->getName()
122
            ));
123
124
            return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId()));
125
        }
126
127
        return $this->render('admin_menu/meal_edit.html.twig', array(
128
            'meal' => $meal,
129
            'form' => $form->createView()
130
        ));
131
    }
132
133
    /**
134
     * @Route(path="/admin/menu/create-meal/{id}", name="admin_menu_meal_create")
135
     * @ParamConverter
136
     */
137
    public function mealCreateAction(Request $request, Category $category)
138
    {
139
        $form = $this->createForm(MealType::class);
140
141
        if ($form->handleRequest($request)->isValid()) {
142
            $em = $this->getDoctrine()->getManager();
143
            $meal = $form->getData();
144
145
            $meal->setCategory($category);
146
147
            $em->persist($meal);
148
            $em->flush();
149
150
            $this->addFlash('success', sprintf(
151
                'Plat "%s" créé.',
152
                $meal->getName()
153
            ));
154
155
            return $this->redirectToRoute('admin_menu_meal_edit', array('id' => $meal->getId()));
156
        }
157
158
        return $this->render('admin_menu/meal_create.html.twig', array(
159
            'form' => $form->createView(),
160
            'category' => $category
161
        ));
162
    }
163
164
    /**
165
     * @Route(path="/admin/menu/meal/{id}/delete", name="admin_menu_meal_delete")
166
     * @ParamConverter
167
     */
168 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...
169
    {
170
        if ($request->isMethod('POST')) {
171
            $em = $this->getDoctrine()->getManager();
172
            $em->remove($meal);
173
            $em->flush();
174
175
            $request->getSession()->getFlashBag()->add('success', 'Le plat '.$meal->getName().' a bien été supprimé.');
176
177
            return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId()));
178
        }
179
180
        return $this->render('admin_menu/meal_delete.html.twig', array(
181
            'meal' => $meal
182
        ));
183
    }
184
}
185