LangController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 46.88 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 45
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C showAction() 27 27 8
A homeAction() 0 7 1
A addAction() 18 18 4
A delAction() 0 18 3

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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9
use ClientBundle\Entity\Lang;
10
use ClientBundle\Form\Type\LangForm;
11
12
/**
13
 * Class LangController
14
 */
15
class LangController 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
        $lang = $em->getRepository('ClientBundle:Lang')->find($id);
27
        if (!$lang) {
28
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
29
        } else {
30
            $form = $this->createForm(LangForm::class, $lang);
31
            if ($this->isGranted('ROLE_ADMIN')) {
32
                $form->add('delete', SubmitType::class);
33
            }
34
35
            $form->handleRequest($request);
36
37
            if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
38
                $lang = $form->getData();
39
                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...
40
                    return $this->redirectToRoute('lang_del', array('id' => $lang->getId()));
41
                }
42
                $em->flush();
43
44
                return $this->redirectToRoute('lang_home');
45
            }
46
47
            return $this->render('ClientBundle:Lang:edit.html.twig', ['form' => $form->createView(), 'lang' => $lang]);
48
        }
49
    }
50
51
    /**
52
     * @Method("GET")
53
     * @return \Symfony\Component\HttpFoundation\Response
54
     */
55
    public function homeAction()
56
    {
57
        $em = $this->getDoctrine()->getManager();
58
        $langs = $em->getRepository('ClientBundle:Lang')->findAll();
59
60
        return $this->render('ClientBundle:Lang:home.html.twig', ['langs' => $langs]);
61
    }
62
63
    /**
64
     * @param Request $request
65
     * @Method({"GET", "POST"})
66
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
67
     */
68 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...
69
    {
70
        $em = $this->getDoctrine()->getManager();
71
        $lang = new Lang();
72
        $form = $this->createForm(LangForm::class, $lang);
73
74
        $form->handleRequest($request);
75
76
        if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
77
            $lang = $form->getData();
78
            $em->persist($lang);
79
            $em->flush();
80
81
            return $this->redirectToRoute('lang_home');
82
        }
83
84
        return $this->render('ClientBundle:Lang:edit.html.twig', ['form' => $form->createView(), 'lang' => $lang]);
85
    }
86
87
    /**
88
     * @param int $id
89
     * @Method({"GET", "POST"})
90
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
91
     */
92
    public function delAction($id)
93
    {
94
        $em = $this->getDoctrine()->getManager();
95
        $lang = $em->getRepository('ClientBundle:Lang')->find($id);
96
        if (!$lang) {
97
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
98
        } else {
99
            $em = $this->getDoctrine()->getManager();
100
            $clients = $em->getRepository('ClientBundle:Client')->findBy(array('language' => $id));
101
            foreach ($clients as $client) {
102
                $client->setLanguage();
103
            }
104
            $em->remove($lang);
105
            $em->flush();
106
        }
107
108
        return $this->redirectToRoute('lang_home');
109
    }
110
}
111