Completed
Push — master ( e0d2a4...2983b4 )
by Joachim
15:21
created

CountryMappingController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 15.56 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 21
loc 135
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 12 1
A showAction() 0 6 1
A newAction() 11 11 2
A editAction() 10 10 2
B handleUpdate() 0 27 4
A updateResponse() 0 7 1
A getForm() 0 4 1

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 Loevgaard\DandomainAltapayBundle\Controller;
4
5
use Loevgaard\DandomainAltapayBundle\Form\CountryMappingType;
6
use Loevgaard\PakkelabelsBundle\Entity\CountryMapping;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Form\Form;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * @Route("/country-mapping")
17
 */
18
class CountryMappingController extends Controller
19
{
20
    /**
21
     * @Method("GET")
22
     * @Route("", name="loevgaard_pakkelabels_country_mapping_index")
23
     *
24
     * @param Request $request
25
     *
26
     * @return Response
27
     */
28
    public function indexAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        /** @var CountryMapping[] $countryMappings */
31
        $countryMappings = $this->get('doctrine')
32
            ->getManager()
33
            ->getRepository('LoevgaardPakkelabelsBundle:CountryMapping')
34
            ->findAll();
35
36
        return $this->render('@LoevgaardPakkelabels/country_mapping/index.html.twig', [
37
            'countryMappings' => $countryMappings,
38
        ]);
39
    }
40
41
    /**
42
     * @Method("GET")
43
     * @Route("/{id}/show", name="loevgaard_pakkelabels_country_mapping_show")
44
     *
45
     * @param CountryMapping $countryMapping
46
     *
47
     * @return Response
48
     */
49
    public function showAction(CountryMapping $countryMapping)
50
    {
51
        return $this->render('@LoevgaardPakkelabels/country_mapping/show.html.twig', [
52
            'countryMapping' => $countryMapping,
53
        ]);
54
    }
55
56
    /**
57
     * @Method({"GET", "POST"})
58
     * @Route("/new", name="loevgaard_pakkelabels_country_mapping_new")
59
     *
60
     * @param Request $request
61
     *
62
     * @return Response
63
     */
64 View Code Duplication
    public function newAction(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...
65
    {
66
        $countryMapping = new CountryMapping();
67
        $form = $this->getForm($countryMapping);
68
        $res = $this->handleUpdate($form, $countryMapping, $request);
69
        if($res) {
70
            return $res;
71
        }
72
73
        return $this->updateResponse($countryMapping, $form);
74
    }
75
76
    /**
77
     * @Method({"GET", "POST"})
78
     * @Route("/{id}/edit", name="loevgaard_pakkelabels_country_mapping_edit")
79
     *
80
     * @param CountryMapping $countryMapping
81
     * @param Request $request
82
     *
83
     * @return Response
84
     */
85 View Code Duplication
    public function editAction(CountryMapping $countryMapping, 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...
86
    {
87
        $form = $this->getForm($countryMapping);
88
        $res = $this->handleUpdate($form, $countryMapping, $request);
89
        if($res) {
90
            return $res;
91
        }
92
93
        return $this->updateResponse($countryMapping, $form);
94
    }
95
96
    /**
97
     * @param Form $form
98
     * @param CountryMapping $countryMapping
99
     * @param Request $request
100
     * @return null|RedirectResponse
101
     */
102
    private function handleUpdate(Form $form, CountryMapping $countryMapping, Request $request)
103
    {
104
        $form->handleRequest($request);
105
106
        if ($form->isSubmitted() && $form->isValid()) {
107
            $new = is_null($countryMapping->getId());
108
109
            $em = $this->getDoctrine()->getManager();
110
            $em->persist($countryMapping);
111
            $em->flush();
112
113
            $translator = $this->get('translator');
114
115
            $this->addFlash(
116
                'success',
117
                $new ?
118
                    $translator->trans('country_mapping.new.created', [], 'LoevgaardPakkelabelsBundle') :
119
                    $translator->trans('country_mapping.edit.updated', [], 'LoevgaardPakkelabelsBundle')
120
            );
121
122
            return $this->redirectToRoute('loevgaard_pakkelabels_country_mapping_edit', [
123
                'id' => $countryMapping->getId()
124
            ]);
125
        }
126
127
        return null;
128
    }
129
130
    /**
131
     * @param CountryMapping $countryMapping
132
     * @param Form $form
133
     * @return Response
134
     */
135
    private function updateResponse(CountryMapping $countryMapping, Form $form) : Response
136
    {
137
        return $this->render('@LoevgaardPakkelabels/country_mapping/edit.html.twig', [
138
            'countryMapping' => $countryMapping,
139
            'form' => $form
140
        ]);
141
    }
142
143
    /**
144
     * @param CountryMapping $countryMapping
145
     * @return Form
146
     */
147
    private function getForm(CountryMapping $countryMapping) : Form
148
    {
149
        return $form = $this->createForm(CountryMappingType::class, $countryMapping);
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
    }
151
152
}
153