Completed
Push — master ( 20e80f...9e4566 )
by Joachim
08:10
created

CountryMappingController::editAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Loevgaard\PakkelabelsBundle\Form\CountryMappingType;
7
use Loevgaard\PakkelabelsBundle\Entity\CountryMapping;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Form\Form;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * @Route("/country-mapping")
18
 */
19
class CountryMappingController extends Controller
20
{
21
    /**
22
     * @Method("GET")
23
     * @Route("", name="loevgaard_pakkelabels_country_mapping_index")
24
     *
25
     * @param Request $request
26
     *
27
     * @return Response
28
     */
29 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...
30
    {
31
        $paginator  = $this->get('knp_paginator');
32
33
        /** @var EntityManager $em */
34
        $em = $this->get('doctrine')->getManager();
35
36
        $qb = $em->createQueryBuilder();
37
        $qb->select('c')
38
            ->from('LoevgaardPakkelabelsBundle:CountryMapping', 'c')
39
            ->orderBy('c.source')
40
        ;
41
42
        /** @var CountryMapping[] $countryMappings */
43
        $countryMappings = $paginator->paginate(
44
            $qb,
45
            $request->query->getInt('page', 1),
46
            30
47
        );
48
49
        return $this->render('@LoevgaardPakkelabels/country_mapping/index.html.twig', [
50
            'countryMappings' => $countryMappings,
51
        ]);
52
    }
53
54
    /**
55
     * @Method("GET")
56
     * @Route("/{id}/show", name="loevgaard_pakkelabels_country_mapping_show")
57
     *
58
     * @param CountryMapping $countryMapping
59
     *
60
     * @return Response
61
     */
62
    public function showAction(CountryMapping $countryMapping)
63
    {
64
        return $this->render('@LoevgaardPakkelabels/country_mapping/show.html.twig', [
65
            'countryMapping' => $countryMapping,
66
        ]);
67
    }
68
69
    /**
70
     * @Method({"GET", "POST"})
71
     * @Route("/new", name="loevgaard_pakkelabels_country_mapping_new")
72
     *
73
     * @param Request $request
74
     *
75
     * @return Response
76
     */
77 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...
78
    {
79
        $countryMapping = new CountryMapping();
80
        $form = $this->getForm($countryMapping);
81
        $res = $this->handleUpdate($form, $countryMapping, $request);
82
        if($res) {
83
            return $res;
84
        }
85
86
        return $this->updateResponse($countryMapping, $form);
87
    }
88
89
    /**
90
     * @Method({"GET", "POST"})
91
     * @Route("/{id}/edit", name="loevgaard_pakkelabels_country_mapping_edit")
92
     *
93
     * @param CountryMapping $countryMapping
94
     * @param Request $request
95
     *
96
     * @return Response
97
     */
98 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...
99
    {
100
        $form = $this->getForm($countryMapping);
101
        $res = $this->handleUpdate($form, $countryMapping, $request);
102
        if($res) {
103
            return $res;
104
        }
105
106
        return $this->updateResponse($countryMapping, $form);
107
    }
108
109
    /**
110
     * @param Form $form
111
     * @param CountryMapping $countryMapping
112
     * @param Request $request
113
     * @return null|RedirectResponse
114
     */
115
    private function handleUpdate(Form $form, CountryMapping $countryMapping, Request $request)
116
    {
117
        $form->handleRequest($request);
118
119
        if ($form->isSubmitted() && $form->isValid()) {
120
            $new = is_null($countryMapping->getId());
121
122
            $em = $this->getDoctrine()->getManager();
123
            $em->persist($countryMapping);
124
            $em->flush();
125
126
            $translator = $this->get('translator');
127
128
            $this->addFlash(
129
                'success',
130
                $new ?
131
                    $translator->trans('country_mapping.new.created', [], 'LoevgaardPakkelabelsBundle') :
132
                    $translator->trans('country_mapping.edit.updated', [], 'LoevgaardPakkelabelsBundle')
133
            );
134
135
            return $this->redirectToRoute('loevgaard_pakkelabels_country_mapping_edit', [
136
                'id' => $countryMapping->getId()
137
            ]);
138
        }
139
140
        return null;
141
    }
142
143
    /**
144
     * @param CountryMapping $countryMapping
145
     * @param Form $form
146
     * @return Response
147
     */
148
    private function updateResponse(CountryMapping $countryMapping, Form $form) : Response
149
    {
150
        return $this->render('@LoevgaardPakkelabels/country_mapping/edit.html.twig', [
151
            'countryMapping' => $countryMapping,
152
            'form' => $form
153
        ]);
154
    }
155
156
    /**
157
     * @param CountryMapping $countryMapping
158
     * @return Form
159
     */
160
    private function getForm(CountryMapping $countryMapping) : Form
161
    {
162
        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...
163
    }
164
165
}
166