CategoryController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 46
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C showAction() 28 28 8
A homeAction() 0 7 1
A addAction() 18 18 4
A delAction() 0 13 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 ClientBundle\Controller;
4
5
use ClientBundle\Entity\Category;
6
use ClientBundle\Form\Type\CategoryForm;
7
use Symfony\Component\HttpFoundation\Request;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
12
/**
13
 * Class CategoryController
14
 */
15
class CategoryController extends Controller
16
{
17
    /**
18
     * @param int     $id
19
     * @param Request $request
20
     * @Method({"GET", "POST"})
21
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
22
     */
23 View Code Duplication
    public function showAction($id, Request $request)
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...
24
    {
25
        $em = $this->getDoctrine()->getManager();
26
        $category = $em->getRepository('ClientBundle:Category')->find($id);
27
28
        if (!$category) {
29
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
30
        }
31
32
        $form = $this->createForm(CategoryForm::class, $category);
33
        if ($this->isGranted('ROLE_ADMIN')) {
34
            $form->add('delete', SubmitType::class);
35
        }
36
37
        $form->handleRequest($request);
38
39
        if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
40
            $category = $form->getData();
41
            if ($this->isGranted('ROLE_ADMIN') && $form->get('delete')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
                return $this->redirectToRoute('category_del', array('id' => $category->getId()));
43
            }
44
            $em->flush();
45
46
            return $this->redirectToRoute('category_home');
47
        }
48
49
        return $this->render('ClientBundle:Category:edit.html.twig', ['form' => $form->createView(), 'category' => $category]);
50
    }
51
52
    /**
53
     * @return \Symfony\Component\HttpFoundation\Response
54
     * @Method("GET")
55
     */
56
    public function homeAction()
57
    {
58
        $em = $this->getDoctrine()->getManager();
59
        $categories = $em->getRepository('ClientBundle:Category')->findAll();
60
61
        return $this->render('ClientBundle:Category:home.html.twig', ['categories' => $categories]);
62
    }
63
64
    /**
65
     * @param Request $request
66
     * @Method({"GET", "POST"})
67
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
68
     */
69 View Code Duplication
    public function addAction(Request $request)
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...
70
    {
71
        $em = $this->getDoctrine()->getManager();
72
        $category = new Category();
73
        $form = $this->createForm(CategoryForm::class, $category);
74
75
        $form->handleRequest($request);
76
77
        if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
78
            $category = $form->getData();
79
            $em->persist($category);
80
            $em->flush();
81
82
            return $this->redirectToRoute('category_home');
83
        }
84
85
        return $this->render('ClientBundle:Category:edit.html.twig', ['form' => $form->createView(), 'category' => $category]);
86
    }
87
88
    /**
89
     * @param int $id
90
     * @Method("GET")
91
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
92
     */
93
    public function delAction($id)
94
    {
95
        $em = $this->getDoctrine()->getManager();
96
        $category = $em->getRepository('ClientBundle:Category')->find($id);
97
        if (!$category) {
98
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
99
        } else {
100
            $em->remove($category);
101
            $em->flush();
102
        }
103
104
        return $this->redirectToRoute('category_home');
105
    }
106
}
107
//php bin/console doctrine:database:create
108
//php bin/console doctrine:schema:update --force
109