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

UnitStorageController::createDeleteForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * UnitStorageController controller des unités 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\UnitStorage;
23
use AppBundle\Form\Type\UnitStorageType;
24
25
/**
26
 * UnitStorage controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/settings/divers/unitstorage")
31
 */
32
class UnitStorageController extends AbstractController
33
{
34
    /**
35
     * Lists all UnitStorage entities.
36
     *
37
     * @Route("/", name="admin_unitstorage")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction(Request $request)
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $qb = $em->getRepository('AppBundle:UnitStorage')->createQueryBuilder('u');
45
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 20);
46
        return array(
47
            'paginator' => $paginator,
48
        );
49
    }
50
51
    /**
52
     * Finds and displays a UnitStorage entity.
53
     *
54
     * @Route("/{slug}/show", name="admin_unitstorage_show")
55
     * @Method("GET")
56
     * @Template()
57
     */
58
    public function showAction(UnitStorage $unitstorage)
59
    {
60
        $deleteForm = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
61
62
        return array(
63
            'unitstorage' => $unitstorage,
64
            'delete_form' => $deleteForm->createView(),
65
        );
66
    }
67
68
    /**
69
     * Displays a form to create a new UnitStorage entity.
70
     *
71
     * @Route("/new", name="admin_unitstorage_new")
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function newAction()
76
    {
77
        $unitstorage = new UnitStorage();
78
        $form = $this->createForm(new UnitStorageType(), $unitstorage);
79
80
        return array(
81
            'unitstorage' => $unitstorage,
82
            'form'   => $form->createView(),
83
        );
84
    }
85
86
    /**
87
     * Creates a new UnitStorage entity.
88
     *
89
     * @Route("/create", name="admin_unitstorage_create")
90
     * @Method("POST")
91
     * @Template("AppBundle:Settings/Divers/UnitStorage:new.html.twig")
92
     */
93
    public function createAction(Request $request)
94
    {
95
        $unitstorage = new UnitStorage();
96
        $form = $this->createForm(new UnitStorageType(), $unitstorage);
97
        if ($form->handleRequest($request)->isValid()) {
98
            $em = $this->getDoctrine()->getManager();
99
            $em->persist($unitstorage);
100
            $em->flush();
101
102
            if ($form->get('save')->isSubmitted()) {
103
                $url = $this->redirectToRoute('admin_unitstorage_show', array('slug' => $unitstorage->getSlug()));
104
            } elseif ($form->get('addmore')->isSubmitted()) {
105
                $this->addFlash('info', 'gestock.settings.add_ok');
106
                $url = $this->redirectToRoute('admin_unitstorage_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
            'unitstorage' => $unitstorage,
113
            'form'   => $form->createView(),
114
        );
115
    }
116
117
    /**
118
     * Displays a form to edit an existing UnitStorage entity.
119
     *
120
     * @Route("/{slug}/edit", name="admin_unitstorage_edit")
121
     * @Method("GET")
122
     * @Template()
123
     */
124
    public function editAction(UnitStorage $unitstorage = null)
125
    {
126
        $editForm = $this->createForm(new UnitStorageType(), $unitstorage, array(
127
            'action' => $this->generateUrl('admin_unitstorage_update', array('slug' => $unitstorage->getSlug())),
0 ignored issues
show
Bug introduced by
It seems like $unitstorage 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($unitstorage->getId(), 'admin_unitstorage_delete');
131
132
        return array(
133
            'unitstorage' => $unitstorage,
134
            'edit_form'   => $editForm->createView(),
135
            'delete_form' => $deleteForm->createView(),
136
        );
137
    }
138
139
    /**
140
     * Edits an existing UnitStorage entity.
141
     *
142
     * @Route("/{slug}/update", name="admin_unitstorage_update")
143
     * @Method("PUT")
144
     * @Template("AppBundle:Settings/Divers/UnitStorage:edit.html.twig")
145
     */
146
    public function updateAction(Request $request, UnitStorage $unitstorage = null)
147
    {
148
        $editForm = $this->createForm(new UnitStorageType(), $unitstorage, array(
149
            'action' => $this->generateUrl('admin_unitstorage_update', array('slug' => $unitstorage->getSlug())),
0 ignored issues
show
Bug introduced by
It seems like $unitstorage 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_unitstorage_edit', array('slug' => $unitstorage->getSlug()));
157
        }
158
        $deleteForm = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
159
160
        return array(
161
            'unitstorage' => $unitstorage,
162
            'edit_form'   => $editForm->createView(),
163
            'delete_form' => $deleteForm->createView(),
164
        );
165
    }
166
167
    /**
168
     * Deletes a UnitStorage entity.
169
     *
170
     * @Route("/{id}/delete", name="admin_unitstorage_delete", requirements={"id"="\d+"})
171
     * @Method("DELETE")
172
     */
173
    public function deleteAction(Request $request, UnitStorage $unitstorage = null)
174
    {
175
        $form = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
0 ignored issues
show
Bug introduced by
It seems like $unitstorage 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($unitstorage);
179
            $em->flush();
180
        }
181
182
        return $this->redirectToRoute('admin_unitstorage');
183
    }
184
}
185