Completed
Push — sf2.7 ( 2bdc17...fc4620 )
by Laurent
03:07
created

UnitStorageController::updateAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
namespace AppBundle\Controller\Settings\Divers;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use AppBundle\Entity\UnitStorage;
11
use AppBundle\Form\Type\UnitStorageType;
12
13
/**
14
 * UnitStorage controller.
15
 *
16
 * @Route("/admin/settings/divers/unitstorage")
17
 */
18
class UnitStorageController extends Controller
19
{
20
    /**
21
     * Lists all UnitStorage entities.
22
     *
23
     * @Route("/", name="admin_unitstorage")
24
     * @Method("GET")
25
     * @Template()
26
     */
27 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...
28
    {
29
        $em = $this->getDoctrine()->getManager();
30
$qb = $em->getRepository('AppBundle:UnitStorage')->createQueryBuilder('u');
31
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 20);
32
        return array(
33
            'paginator' => $paginator,
34
        );
35
    }
36
37
    /**
38
     * Finds and displays a UnitStorage entity.
39
     *
40
     * @Route("/{slug}/show", name="admin_unitstorage_show")
41
     * @Method("GET")
42
     * @Template()
43
     */
44
    public function showAction(UnitStorage $unitstorage)
45
    {
46
        $deleteForm = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
47
48
        return array(
49
            'unitstorage' => $unitstorage,
50
            'delete_form' => $deleteForm->createView(),
51
        );
52
    }
53
54
    /**
55
     * Displays a form to create a new UnitStorage entity.
56
     *
57
     * @Route("/new", name="admin_unitstorage_new")
58
     * @Method("GET")
59
     * @Template()
60
     */
61
    public function newAction()
62
    {
63
        $unitstorage = new UnitStorage();
64
        $form = $this->createForm(new UnitStorageType(), $unitstorage);
65
66
        return array(
67
            'unitstorage' => $unitstorage,
68
            'form'   => $form->createView(),
69
        );
70
    }
71
72
    /**
73
     * Creates a new UnitStorage entity.
74
     *
75
     * @Route("/create", name="admin_unitstorage_create")
76
     * @Method("POST")
77
     * @Template("AppBundle:Settings/Divers/UnitStorage:new.html.twig")
78
     */
79
    public function createAction(Request $request)
80
    {
81
        $unitstorage = new UnitStorage();
82
        $form = $this->createForm(new UnitStorageType(), $unitstorage);
83
        if ($form->handleRequest($request)->isValid()) {
84
            $em = $this->getDoctrine()->getManager();
85
            $em->persist($unitstorage);
86
            $em->flush();
87
88
            if ($form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
89
                $url = $this->redirectToRoute('admin_unitstorage_show', array('slug' => $unitstorage->getSlug()));
90
            } elseif ($form->get('addmore')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
91
                $this->addFlash('info', 'gestock.settings.add_ok');
92
                $url = $this->redirect($this->generateUrl('admin_unitstorage_new'));
93
            }
94
            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...
95
        }
96
97
        return array(
98
            'unitstorage' => $unitstorage,
99
            'form'   => $form->createView(),
100
        );
101
    }
102
103
    /**
104
     * Displays a form to edit an existing UnitStorage entity.
105
     *
106
     * @Route("/{slug}/edit", name="admin_unitstorage_edit")
107
     * @Method("GET")
108
     * @Template()
109
     */
110
    public function editAction(UnitStorage $unitstorage)
111
    {
112
        $editForm = $this->createForm(new UnitStorageType(), $unitstorage, array(
113
            'action' => $this->generateUrl('admin_unitstorage_update', array('slug' => $unitstorage->getSlug())),
114
            'method' => 'PUT',
115
        ));
116
        $deleteForm = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
117
118
        return array(
119
            'unitstorage' => $unitstorage,
120
            'edit_form'   => $editForm->createView(),
121
            'delete_form' => $deleteForm->createView(),
122
        );
123
    }
124
125
    /**
126
     * Edits an existing UnitStorage entity.
127
     *
128
     * @Route("/{slug}/update", name="admin_unitstorage_update")
129
     * @Method("PUT")
130
     * @Template("AppBundle:Settings/Divers/UnitStorage:edit.html.twig")
131
     */
132
    public function updateAction(UnitStorage $unitstorage, Request $request)
133
    {
134
        $editForm = $this->createForm(new UnitStorageType(), $unitstorage, array(
135
            'action' => $this->generateUrl('admin_unitstorage_update', array('slug' => $unitstorage->getSlug())),
136
            'method' => 'PUT',
137
        ));
138
        if ($editForm->handleRequest($request)->isValid()) {
139
            $this->getDoctrine()->getManager()->flush();
140
141
            return $this->redirectToRoute('admin_unitstorage_edit', array('slug' => $unitstorage->getSlug()));
142
        }
143
        $deleteForm = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
144
145
        return array(
146
            'unitstorage' => $unitstorage,
147
            'edit_form'   => $editForm->createView(),
148
            'delete_form' => $deleteForm->createView(),
149
        );
150
    }
151
152
    /**
153
     * Deletes a UnitStorage entity.
154
     *
155
     * @Route("/{id}/delete", name="admin_unitstorage_delete", requirements={"id"="\d+"})
156
     * @Method("DELETE")
157
     */
158
    public function deleteAction(UnitStorage $unitstorage, Request $request)
159
    {
160
        $form = $this->createDeleteForm($unitstorage->getId(), 'admin_unitstorage_delete');
161
        if ($form->handleRequest($request)->isValid()) {
162
            $em = $this->getDoctrine()->getManager();
163
            $em->remove($unitstorage);
164
            $em->flush();
165
        }
166
167
        return $this->redirectToRoute('admin_unitstorage');
168
    }
169
170
    /**
171
     * Create Delete form
172
     *
173
     * @param integer                       $id
174
     * @param string                        $route
175
     * @return \Symfony\Component\Form\Form
176
     */
177
    protected function createDeleteForm($id, $route)
178
    {
179
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
180
            ->setAction($this->generateUrl($route, array('id' => $id)))
181
            ->setMethod('DELETE')
182
            ->getForm()
183
        ;
184
    }
185
}
186