Completed
Push — sf2.7 ( 758063...e98806 )
by Laurent
03:16
created

ZoneStorageController::createDeleteForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * ZoneStorageController controller des zones de stockage.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller\Settings\Divers;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\ZoneStorage;
23
use AppBundle\Form\Type\ZoneStorageType;
24
25
/**
26
 * ZoneStorage controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/settings/divers/zonestorage")
31
 */
32 View Code Duplication
class ZoneStorageController extends AbstractController
1 ignored issue
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...
33
{
34
    /**
35
     * Lists all ZoneStorage entities.
36
     *
37
     * @Route("/", name="admin_zonestorage")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:ZoneStorage')->findAll();
45
        
46
        return array(
47
            'entities'  => $entities,
48
        );
49
    }
50
51
    /**
52
     * Finds and displays a ZoneStorage entity.
53
     *
54
     * @Route("/{slug}/show", name="admin_zonestorage_show")
55
     * @Method("GET")
56
     * @Template()
57
     */
58
    public function showAction(ZoneStorage $zonestorage)
59
    {
60
        $deleteForm = $this->createDeleteForm($zonestorage->getId(), 'admin_zonestorage_delete');
61
62
        return array(
63
            'zonestorage' => $zonestorage,
64
            'delete_form' => $deleteForm->createView(),
65
        );
66
    }
67
68
    /**
69
     * Displays a form to create a new ZoneStorage entity.
70
     *
71
     * @Route("/new", name="admin_zonestorage_new")
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function newAction()
76
    {
77
        $zonestorage = new ZoneStorage();
78
        $form = $this->createForm(new ZoneStorageType(), $zonestorage);
79
80
        return array(
81
            'zonestorage' => $zonestorage,
82
            'form'   => $form->createView(),
83
        );
84
    }
85
86
    /**
87
     * Creates a new ZoneStorage entity.
88
     *
89
     * @Route("/create", name="admin_zonestorage_create")
90
     * @Method("POST")
91
     * @Template("AppBundle:Settings/Divers/ZoneStorage:new.html.twig")
92
     */
93
    public function createAction(Request $request)
94
    {
95
        $zonestorage = new ZoneStorage();
96
        $form = $this->createForm(new ZoneStorageType(), $zonestorage);
97
        if ($form->handleRequest($request)->isValid()) {
98
            $em = $this->getDoctrine()->getManager();
99
            $em->persist($zonestorage);
100
            $em->flush();
101
102
            if ($form->get('save')->isSubmitted()) {
103
                $url = $this->redirectToRoute('admin_zonestorage_show', array('slug' => $zonestorage->getSlug()));
104
            } elseif ($form->get('addmore')->isSubmitted()) {
105
                $this->addFlash('info', 'gestock.settings.add_ok');
106
                $url = $this->redirect($this->generateUrl('admin_zonestorage_new'));
107
            }
108
            return $url;
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
109
        }
110
111
        return array(
112
            'zonestorage' => $zonestorage,
113
            'form'   => $form->createView(),
114
        );
115
    }
116
117
    /**
118
     * Displays a form to edit an existing ZoneStorage entity.
119
     *
120
     * @Route("/{slug}/edit", name="admin_zonestorage_edit")
121
     * @Method("GET")
122
     * @Template()
123
     */
124
    public function editAction(ZoneStorage $zonestorage = null)
125
    {
126
        $editForm = $this->createForm(new ZoneStorageType(), $zonestorage, array(
127
            'action' => $this->generateUrl('admin_zonestorage_update', array('slug' => $zonestorage->getSlug())),
0 ignored issues
show
Bug introduced by
It seems like $zonestorage is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
128
            'method' => 'PUT',
129
        ));
130
        $deleteForm = $this->createDeleteForm($zonestorage->getId(), 'admin_zonestorage_delete');
131
132
        return array(
133
            'zonestorage' => $zonestorage,
134
            'edit_form'   => $editForm->createView(),
135
            'delete_form' => $deleteForm->createView(),
136
        );
137
    }
138
139
    /**
140
     * Edits an existing ZoneStorage entity.
141
     *
142
     * @Route("/{slug}/update", name="admin_zonestorage_update")
143
     * @Method("PUT")
144
     * @Template("AppBundle:Settings/Divers/ZoneStorage:edit.html.twig")
145
     */
146
    public function updateAction(Request $request, ZoneStorage $zonestorage = null)
147
    {
148
        $editForm = $this->createForm(new ZoneStorageType(), $zonestorage, array(
149
            'action' => $this->generateUrl('admin_zonestorage_update', array('slug' => $zonestorage->getSlug())),
0 ignored issues
show
Bug introduced by
It seems like $zonestorage is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
150
            'method' => 'PUT',
151
        ));
152
        if ($editForm->handleRequest($request)->isValid()) {
153
            $this->getDoctrine()->getManager()->flush();
154
            $this->addFlash('info', 'gestock.settings.edit_ok');
155
156
            return $this->redirectToRoute('admin_zonestorage_edit', array('slug' => $zonestorage->getSlug()));
157
        }
158
        $deleteForm = $this->createDeleteForm($zonestorage->getId(), 'admin_zonestorage_delete');
159
160
        return array(
161
            'zonestorage' => $zonestorage,
162
            'edit_form'   => $editForm->createView(),
163
            'delete_form' => $deleteForm->createView(),
164
        );
165
    }
166
167
    /**
168
     * Deletes a ZoneStorage entity.
169
     *
170
     * @Route("/{id}/delete", name="admin_zonestorage_delete", requirements={"id"="\d+"})
171
     * @Method("DELETE")
172
     */
173
    public function deleteAction(Request $request, ZoneStorage $zonestorage = null)
174
    {
175
        $form = $this->createDeleteForm($zonestorage->getId(), 'admin_zonestorage_delete');
0 ignored issues
show
Bug introduced by
It seems like $zonestorage is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
176
        if ($form->handleRequest($request)->isValid()) {
177
            $em = $this->getDoctrine()->getManager();
178
            $em->remove($zonestorage);
179
            $em->flush();
180
        }
181
182
        return $this->redirectToRoute('admin_zonestorage');
183
    }
184
}
185