Completed
Pull Request — master (#2)
by Andrew
05:50
created

AdminMenuItemController::editItemAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.5039

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
ccs 13
cts 19
cp 0.6842
rs 8.7972
cc 4
eloc 15
nc 3
nop 2
crap 4.5039
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kate
5
 * Date: 18.03.16
6
 * Time: 11:34
7
 */
8
9
namespace AppBundle\Controller\Admin;
10
11
use AppBundle\Entity\MenuItem;
12
use AppBundle\Form\MenuItemType;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * @Security("has_role('ROLE_MANAGER')")
23
 * @Route("/admin")
24
 */
25
class AdminMenuItemController extends Controller
26
{
27
    /**
28
     * @Route("/menu_items", name="admin_items")
29
     */
30 1
    public function showItemsAction(Request $request)
31
    {
32 1
        $em = $this->getDoctrine()->getManager();
33 1
        $items = $em->getRepository('AppBundle:MenuItem')->findAll();
34 1
        return $this->render('@App/admin/menu_item/items.html.twig', array('items' => $items));
35
    }
36
37
    /**
38
     * @Route("/menu_item/new", name="admin_add_menu_item")
39
     */
40 1
    public function addItemAction(Request $request)
41
    {
42 1
        $em = $this->getDoctrine()->getManager();
43 1
        $menu_item = new MenuItem();
44 1
        $form = $this->createForm(MenuItemType::class, $menu_item);
45 1
        $form->handleRequest($request);
46 1
        if ($form->isSubmitted() && $form->isValid()) {
47
            $em->persist($menu_item);
48
            $em->flush();
49
            return $this->redirectToRoute('admin_items');
50
        }
51 1
        return $this->render('@App/admin/menu_item/new_item.html.twig', array(
52 1
            'item' => $menu_item,
53 1
            'form' => $form->createView(),
54 1
        ));
55
56
    }
57
58
    /**
59
     * @Route("/menu_item/show/{id}", name="admin_item_show")
60
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
61
     */
62 1
    public function showItemAction(Request $request, MenuItem $menuItem)
63
    {
64
        //for delete
65 1
        $form = $this->deleteForm($menuItem);
66
67 1
        return $this->render('@App/admin/menu_item/show_item.html.twig',
68 1
            array('item' => $menuItem, 'delete_form' => $form->createView()));
69
    }
70
71
    /**
72
     * @Route("/menu_item/edit/{id}", name="admin_item_edit")
73
     * @Method({"GET", "PUT"})
74
     * @Security("has_role('ROLE_ADMIN')")
75
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
76
     */
77 1
    public function editItemAction(Request $request, MenuItem $menuItem)
78
    {
79 1
        $em = $this->getDoctrine()->getManager();
80 1
        $form = $this->createForm(MenuItemType::class, $menuItem, [
81 1
            'method' => 'PUT',
82 1
            'attr' => ['class' => 'horizontal']
83 1
        ])
84 1
            ->add('submit', SubmitType::class, ['label' => 'Edit',
85 1
                'attr' => ['class' => 'btn btn-raised btn-default']
86 1
            ]);
87 1
        if ($request->getMethod() == 'PUT') {
88
            $form->handleRequest($request);
89
            if ($form->isValid() && $form->isSubmitted()) {
90
                $em->persist($menuItem);
91
                $em->flush();
92
                return $this->redirectToRoute('admin_items');
93
            }
94
        }
95
96 1
        return $this->render('@App/admin/menu_item/edit_menu_item.html.twig',
97 1
            array('form' => $form->createView(),
98 1
            ));
99
    }
100
101
    /**
102
     * @Route("/menu_item/delete/{id}", name="admin_menu_item_delete")
103
     * @Method("DELETE")
104
     * @Security("has_role('ROLE_ADMIN')")
105
     * @ParamConverter("comment", options={"mapping": {"id": "id"}})
106
     */
107
    public function deleteCommentAction(Request $request, MenuItem $menuItem)
108
    {
109
        $form = $this->deleteForm($menuItem);
110
        $form->handleRequest($request);
111
        if ($form->isSubmitted() && $form->isValid()) {
112
            $entityManager = $this->getDoctrine()->getManager();
113
            $entityManager->remove($menuItem);
114
            $entityManager->flush();
115
        }
116
        return $this->redirectToRoute('admin_items');
117
    }
118
119 1 View Code Duplication
    private function deleteForm(MenuItem $menuItem)
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...
120
    {
121 1
        return $this->createFormBuilder()
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122 1
            ->setAction($this->generateUrl('admin_menu_item_delete',
123 1
                array('id' => $menuItem->getId())))
124 1
            ->setMethod('DELETE')
125 1
            ->getForm();
126
    }
127
}