Completed
Push — master ( 6a0206...a8e87e )
by Joachim
03:21
created

ShippingMethodMappingController::indexAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Loevgaard\PakkelabelsBundle\Entity\ShippingMethodMapping;
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 View Code Duplication
class ShippingMethodMappingController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
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
    public function indexAction(Request $request)
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:ShippingMethodMapping', 'c')
39
            ->orderBy('c.source')
40
        ;
41
42
        /** @var ShippingMethodMapping[] $shippingMethodMappings */
43
        $shippingMethodMappings = $paginator->paginate(
44
            $qb,
45
            $request->query->getInt('page', 1),
46
            30
47
        );
48
49
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/index.html.twig', [
50
            'shippingMethodMappings' => $shippingMethodMappings,
51
        ]);
52
    }
53
54
    /**
55
     * @Method("GET")
56
     * @Route("/{id}/show", name="loevgaard_pakkelabels_shipping_method_mapping_show")
57
     *
58
     * @param ShippingMethodMapping $shippingMethodMapping
59
     *
60
     * @return Response
61
     */
62
    public function showAction(ShippingMethodMapping $shippingMethodMapping)
63
    {
64
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/show.html.twig', [
65
            'shippingMethodMapping' => $shippingMethodMapping,
66
        ]);
67
    }
68
69
    /**
70
     * @Method({"GET", "POST"})
71
     * @Route("/new", name="loevgaard_pakkelabels_shipping_method_mapping_new")
72
     *
73
     * @param Request $request
74
     *
75
     * @return Response
76
     */
77
    public function newAction(Request $request)
78
    {
79
        $shippingMethodMapping = new ShippingMethodMapping();
80
        $form = $this->getForm($shippingMethodMapping);
81
        $res = $this->handleUpdate($form, $shippingMethodMapping, $request);
82
        if ($res) {
83
            return $res;
84
        }
85
86
        return $this->updateResponse($shippingMethodMapping, $form);
87
    }
88
89
    /**
90
     * @Method({"GET", "POST"})
91
     * @Route("/{id}/edit", name="loevgaard_pakkelabels_shipping_method_mapping_edit")
92
     *
93
     * @param ShippingMethodMapping $shippingMethodMapping
94
     * @param Request        $request
95
     *
96
     * @return Response
97
     */
98
    public function editAction(ShippingMethodMapping $shippingMethodMapping, Request $request)
99
    {
100
        $form = $this->getForm($shippingMethodMapping);
101
        $res = $this->handleUpdate($form, $shippingMethodMapping, $request);
102
        if ($res) {
103
            return $res;
104
        }
105
106
        return $this->updateResponse($shippingMethodMapping, $form);
107
    }
108
109
    /**
110
     * @param Form           $form
111
     * @param ShippingMethodMapping $shippingMethodMapping
112
     * @param Request        $request
113
     *
114
     * @return null|RedirectResponse
115
     */
116
    private function handleUpdate(Form $form, ShippingMethodMapping $shippingMethodMapping, Request $request)
117
    {
118
        $form->handleRequest($request);
119
120
        if ($form->isSubmitted() && $form->isValid()) {
121
            $new = is_null($shippingMethodMapping->getId());
122
123
            $em = $this->getDoctrine()->getManager();
124
            $em->persist($shippingMethodMapping);
125
            $em->flush();
126
127
            $translator = $this->get('translator');
128
129
            $this->addFlash(
130
                'success',
131
                $new ?
132
                    $translator->trans('shipping_method_mapping.new.created', [], 'LoevgaardPakkelabelsBundle') :
133
                    $translator->trans('shipping_method_mapping.edit.updated', [], 'LoevgaardPakkelabelsBundle')
134
            );
135
136
            return $this->redirectToRoute('loevgaard_pakkelabels_shipping_method_mapping_edit', [
137
                'id' => $shippingMethodMapping->getId(),
138
            ]);
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @param ShippingMethodMapping $shippingMethodMapping
146
     * @param Form           $form
147
     *
148
     * @return Response
149
     */
150
    private function updateResponse(ShippingMethodMapping $shippingMethodMapping, Form $form): Response
151
    {
152
        return $this->render('@LoevgaardPakkelabels/shipping_method_mapping/edit.html.twig', [
153
            'shippingMethodMapping' => $shippingMethodMapping,
154
            'form' => $form->createView(),
155
        ]);
156
    }
157
158
    /**
159
     * @param ShippingMethodMapping $shippingMethodMapping
160
     *
161
     * @return Form
162
     */
163
    private function getForm(ShippingMethodMapping $shippingMethodMapping): Form
164
    {
165
        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...
166
    }
167
}
168