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

FamilyLogController::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
 * FamilyLogController controller des familles logistiques.
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\FamilyLog;
23
use AppBundle\Form\Type\FamilyLogType;
24
25
/**
26
 * FamilyLog controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/settings/divers/familylog")
31
 */
32 View Code Duplication
class FamilyLogController 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 FamilyLog entities.
36
     *
37
     * @Route("/", name="admin_familylog")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:FamilyLog')->childrenHierarchy();
45
        
46
        return array(
47
            'entities'  => $entities,
48
        );
49
    }
50
51
    /**
52
     * Finds and displays a FamilyLog entity.
53
     *
54
     * @Route("/{slug}/show", name="admin_familylog_show")
55
     * @Method("GET")
56
     * @Template()
57
     */
58
    public function showAction(FamilyLog $familylog)
59
    {
60
        $deleteForm = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
61
62
        return array(
63
            'familylog' => $familylog,
64
            'delete_form' => $deleteForm->createView(),
65
        );
66
    }
67
68
    /**
69
     * Displays a form to create a new FamilyLog entity.
70
     *
71
     * @Route("/new", name="admin_familylog_new")
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function newAction()
76
    {
77
        $familylog = new FamilyLog();
78
        $form = $this->createForm(new FamilyLogType(), $familylog);
79
80
        return array(
81
            'familylog' => $familylog,
82
            'form'   => $form->createView(),
83
        );
84
    }
85
86
    /**
87
     * Creates a new FamilyLog entity.
88
     *
89
     * @Route("/create", name="admin_familylog_create")
90
     * @Method("POST")
91
     * @Template("AppBundle:FamilyLog:new.html.twig")
92
     */
93
    public function createAction(Request $request)
94
    {
95
        $familylog = new FamilyLog();
96
        $form = $this->createForm(new FamilyLogType(), $familylog);
97
        if ($form->handleRequest($request)->isValid()) {
98
            $em = $this->getDoctrine()->getManager();
99
            $em->persist($familylog);
100
            $em->flush();
101
102
            if ($form->get('save')->isSubmitted()) {
103
                $url = $this->redirect($this->generateUrl(
104
                    'admin_familylog_show',
105
                    array('slug' => $familylog->getSlug())
106
                ));
107
            } elseif ($form->get('addmore')->isSubmitted()) {
108
                $this->addFlash('info', 'gestock.settings.add_ok');
109
                $url = $this->redirectToRoute('admin_familylog_new');
110
            }
111
            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...
112
        }
113
114
        return array(
115
            'familylog' => $familylog,
116
            'form'   => $form->createView(),
117
        );
118
    }
119
120
    /**
121
     * Displays a form to edit an existing FamilyLog entity.
122
     *
123
     * @Route("/{slug}/edit", name="admin_familylog_edit")
124
     * @Method("GET")
125
     * @Template()
126
     */
127
    public function editAction(FamilyLog $familylog = null)
128
    {
129
        $editForm = $this->createForm(new FamilyLogType(), $familylog, array(
130
            'action' => $this->generateUrl(
131
                'admin_familylog_update',
132
                array('slug' => $familylog->getSlug())
0 ignored issues
show
Bug introduced by
It seems like $familylog 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...
133
            ),
134
            'method' => 'PUT',
135
        ));
136
        $deleteForm = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
137
138
        return array(
139
            'familylog' => $familylog,
140
            'edit_form'   => $editForm->createView(),
141
            'delete_form' => $deleteForm->createView(),
142
        );
143
    }
144
145
    /**
146
     * Edits an existing FamilyLog entity.
147
     *
148
     * @Route("/{slug}/update", name="admin_familylog_update")
149
     * @Method("PUT")
150
     * @Template("AppBundle:FamilyLog:edit.html.twig")
151
     */
152
    public function updateAction(Request $request, FamilyLog $famlog = null)
153
    {
154
        $editForm = $this->createForm(new FamilyLogType(), $famlog, array(
155
            'action' => $this->generateUrl(
156
                'admin_familylog_update',
157
                array('slug' => $famlog->getSlug())
0 ignored issues
show
Bug introduced by
It seems like $famlog 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...
158
            ),
159
            'method' => 'PUT',
160
        ));
161
        if ($editForm->handleRequest($request)->isValid()) {
162
            $this->getDoctrine()->getManager()->flush();
163
            $this->addFlash('info', 'gestock.settings.edit_ok');
164
165
            return $this->redirectToRoute('admin_familylog_edit', array('slug' => $famlog->getSlug()));
166
        }
167
        $deleteForm = $this->createDeleteForm($famlog->getId(), 'admin_familylog_delete');
168
169
        return array(
170
            'familylog' => $famlog,
171
            'edit_form'   => $editForm->createView(),
172
            'delete_form' => $deleteForm->createView(),
173
        );
174
    }
175
176
    /**
177
     * Deletes a FamilyLog entity.
178
     *
179
     * @Route("/{id}/delete", name="admin_familylog_delete", requirements={"id"="\d+"})
180
     * @Method("DELETE")
181
     */
182
    public function deleteAction(Request $request, FamilyLog $familylog = null)
183
    {
184
        $form = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
0 ignored issues
show
Bug introduced by
It seems like $familylog 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...
185
        if ($form->handleRequest($request)->isValid()) {
186
            $em = $this->getDoctrine()->getManager();
187
            $em->remove($familylog);
188
            $em->flush();
189
        }
190
191
        return $this->redirectToRoute('admin_familylog');
192
    }
193
}
194