ShippingMethodMappingController::newAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Controller;
4
5
use Loevgaard\PakkelabelsBundle\Entity\ShippingMethodMapping;
6
use Loevgaard\PakkelabelsBundle\Form\CountrySelectorType;
7
use Loevgaard\PakkelabelsBundle\Form\ShippingMethodMappingType;
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("/shipping-method-mapping")
18
 */
19
class ShippingMethodMappingController extends Controller
20
{
21
    /**
22
     * @Method("GET")
23
     * @Route("", name="loevgaard_pakkelabels_shipping_method_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
        $repos = $this->get('loevgaard_pakkelabels.shipping_method_mapping_repository');
32
33
        /** @var ShippingMethodMapping[] $shippingMethodMappings */
34
        $shippingMethodMappings = $repos->findAllWithPaging($request->query->getInt('page', 1));
35
36
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/index.html.twig', [
37
            'shippingMethodMappings' => $shippingMethodMappings,
38
        ]);
39
    }
40
41
    /**
42
     * @Method("GET")
43
     * @Route("/{id}/show", name="loevgaard_pakkelabels_shipping_method_mapping_show")
44
     *
45
     * @param ShippingMethodMapping $shippingMethodMapping
46
     *
47
     * @return Response
48
     */
49
    public function showAction(ShippingMethodMapping $shippingMethodMapping)
50
    {
51
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/show.html.twig', [
52
            'shippingMethodMapping' => $shippingMethodMapping,
53
        ]);
54
    }
55
56
    /**
57
     * @Method({"GET", "POST"})
58
     * @Route("/new", name="loevgaard_pakkelabels_shipping_method_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
        $shippingMethodMapping = new ShippingMethodMapping();
67
        $form = $this->getForm($shippingMethodMapping);
68
        $res = $this->handleUpdate($form, $shippingMethodMapping, $request);
69
        if ($res) {
70
            return $res;
71
        }
72
73
        return $this->updateResponse($shippingMethodMapping, $form);
74
    }
75
76
    /**
77
     * @Method({"GET", "POST"})
78
     * @Route("/{id}/edit", name="loevgaard_pakkelabels_shipping_method_mapping_edit")
79
     *
80
     * @param ShippingMethodMapping $shippingMethodMapping
81
     * @param Request               $request
82
     *
83
     * @return Response
84
     */
85 View Code Duplication
    public function editAction(ShippingMethodMapping $shippingMethodMapping, 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($shippingMethodMapping);
88
        $res = $this->handleUpdate($form, $shippingMethodMapping, $request);
89
        if ($res) {
90
            return $res;
91
        }
92
93
        return $this->updateResponse($shippingMethodMapping, $form);
94
    }
95
96
    /**
97
     * @param Form                  $form
98
     * @param ShippingMethodMapping $shippingMethodMapping
99
     * @param Request               $request
100
     *
101
     * @return null|RedirectResponse
102
     */
103
    private function handleUpdate(Form $form, ShippingMethodMapping $shippingMethodMapping, Request $request)
104
    {
105
        $form->handleRequest($request);
106
107
        if ($form->isSubmitted() && $form->isValid()) {
108
            $em = $this->getDoctrine()->getManager();
109
            $em->persist($shippingMethodMapping);
110
            $em->flush();
111
112
            $translator = $this->get('translator');
113
114
            $this->addFlash(
115
                'success',
116
                $translator->trans('shipping_method_mapping.edit.saved', [], 'LoevgaardPakkelabelsBundle')
117
            );
118
119
            return $this->redirectToRoute('loevgaard_pakkelabels_shipping_method_mapping_edit', [
120
                'id' => $shippingMethodMapping->getId(),
121
            ]);
122
        }
123
124
        return null;
125
    }
126
127
    /**
128
     * @param ShippingMethodMapping $shippingMethodMapping
129
     * @param Form                  $form
130
     *
131
     * @return Response
132
     */
133
    private function updateResponse(ShippingMethodMapping $shippingMethodMapping, Form $form): Response
134
    {
135
        $countryForm = $this->getCountryForm();
136
137
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/edit.html.twig', [
138
            'shippingMethodMapping' => $shippingMethodMapping,
139
            'form' => $form->createView(),
140
            'countryForm' => $countryForm->createView(),
141
        ]);
142
    }
143
144
    /**
145
     * @param ShippingMethodMapping $shippingMethodMapping
146
     *
147
     * @return Form
148
     */
149
    private function getForm(ShippingMethodMapping $shippingMethodMapping): Form
150
    {
151
        return $form = $this->createForm(ShippingMethodMappingType::class, $shippingMethodMapping);
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
    /**
155
     * @return Form
156
     */
157
    private function getCountryForm(): Form
158
    {
159
        return $form = $this->createForm(CountrySelectorType::class);
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...
160
    }
161
}
162