CategoryController::showCategoryAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
crap 2
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