Completed
Pull Request — master (#2)
by Andrew
04:30
created

AdminMenuItemController::showItemAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
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
    public function showItemsAction(Request $request)
31
    {
32
        $em = $this->getDoctrine()->getManager();
33
        $items = $em->getRepository('AppBundle:MenuItem')->findAll();
34
        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
    public function addItemAction(Request $request)
41
    {
42
        $em = $this->getDoctrine()->getManager();
43
        $menu_item = new MenuItem();
44
        $form = $this->createForm(MenuItemType::class, $menu_item);
45
        $form->handleRequest($request);
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            $em->persist($menu_item);
48
            $em->flush();
49
            return $this->redirectToRoute('admin_items');
50
        }
51
        return $this->render('@App/admin/menu_item/new_item.html.twig', array(
52
            'item' => $menu_item,
53
            'form' => $form->createView(),
54
        ));
55
56
    }
57
58
    /**
59
     * @Route("/menu_item/show/{id}", name="admin_item_show")
60
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
61
     */
62
    public function showItemAction(Request $request, MenuItem $menuItem)
63
    {
64
        //for delete
65
        $form = $this->deleteForm($menuItem);
66
67
        return $this->render('@App/admin/menu_item/show_item.html.twig',
68
            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
    public function editItemAction(Request $request, MenuItem $menuItem)
78
    {
79
        $em = $this->getDoctrine()->getManager();
80
        $form = $this->createForm(MenuItemType::class, $menuItem, [
81
            'method' => 'PUT',
82
            'attr' => ['class' => 'horizontal']
83
        ])
84
            ->add('submit', SubmitType::class, ['label' => 'Edit',
85
                'attr' => ['class' => 'btn btn-raised btn-default']
86
            ]);
87
        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
        return $this->render('@App/admin/menu_item/edit_menu_item.html.twig',
97
            array('form' => $form->createView(),
98
            ));
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 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
        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
            ->setAction($this->generateUrl('admin_menu_item_delete',
123
                array('id' => $menuItem->getId())))
124
            ->setMethod('DELETE')
125
            ->getForm();
126
    }
127
}