Completed
Push — master ( dd5924...1cc36f )
by Joachim
62:06 queued 54:44
created

ShippingMethodMappingController::handleUpdate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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