CountryMappingController::handleUpdate()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 2
nop 3
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Controller;
4
5
use Loevgaard\PakkelabelsBundle\Entity\CountryMapping;
6
use Loevgaard\PakkelabelsBundle\Form\CountryMappingType;
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 View Code Duplication
    public function indexAction(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...
29
    {
30
        $repos = $this->get('loevgaard_pakkelabels.country_mapping_repository');
31
32
        /** @var CountryMapping[] $countryMappings */
33
        $countryMappings = $repos->findAllWithPaging($request->query->getInt('page', 1));
34
35
        return $this->render('@LoevgaardPakkelabels/country_mapping/index.html.twig', [
36
            'countryMappings' => $countryMappings,
37
        ]);
38
    }
39
40
    /**
41
     * @Method("GET")
42
     * @Route("/{id}/show", name="loevgaard_pakkelabels_country_mapping_show")
43
     *
44
     * @param CountryMapping $countryMapping
45
     *
46
     * @return Response
47
     */
48
    public function showAction(CountryMapping $countryMapping)
49
    {
50
        return $this->render('@LoevgaardPakkelabels/country_mapping/show.html.twig', [
51
            'countryMapping' => $countryMapping,
52
        ]);
53
    }
54
55
    /**
56
     * @Method({"GET", "POST"})
57
     * @Route("/new", name="loevgaard_pakkelabels_country_mapping_new")
58
     *
59
     * @param Request $request
60
     *
61
     * @return Response
62
     */
63 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...
64
    {
65
        $countryMapping = new CountryMapping();
66
        $form = $this->getForm($countryMapping);
67
        $res = $this->handleUpdate($form, $countryMapping, $request);
68
        if ($res) {
69
            return $res;
70
        }
71
72
        return $this->updateResponse($countryMapping, $form);
73
    }
74
75
    /**
76
     * @Method({"GET", "POST"})
77
     * @Route("/{id}/edit", name="loevgaard_pakkelabels_country_mapping_edit")
78
     *
79
     * @param CountryMapping $countryMapping
80
     * @param Request        $request
81
     *
82
     * @return Response
83
     */
84 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...
85
    {
86
        $form = $this->getForm($countryMapping);
87
        $res = $this->handleUpdate($form, $countryMapping, $request);
88
        if ($res) {
89
            return $res;
90
        }
91
92
        return $this->updateResponse($countryMapping, $form);
93
    }
94
95
    /**
96
     * @param Form           $form
97
     * @param CountryMapping $countryMapping
98
     * @param Request        $request
99
     *
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
     *
134
     * @return Response
135
     */
136
    private function updateResponse(CountryMapping $countryMapping, Form $form): Response
137
    {
138
        return $this->render('@LoevgaardPakkelabels/country_mapping/edit.html.twig', [
139
            'countryMapping' => $countryMapping,
140
            'form' => $form->createView(),
141
        ]);
142
    }
143
144
    /**
145
     * @param CountryMapping $countryMapping
146
     *
147
     * @return Form
148
     */
149
    private function getForm(CountryMapping $countryMapping): Form
150
    {
151
        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...
152
    }
153
}
154