Completed
Push — master ( f7fcb6...53e30c )
by Laurent
49:10
created

UserController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 17
c 5
b 0
f 0
lcom 1
cbo 3
dl 0
loc 222
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A showAction() 0 6 1
A indexAction() 0 6 1
A sortAction() 0 6 1
A getRoles() 0 13 3
A validUser() 0 19 4
1
<?php
2
/**
3
 * UserController controller des utilisateurs.
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;
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
23
use AppBundle\Entity\User;
24
use AppBundle\Form\Type\UserType;
25
26
/**
27
 * User controller.
28
 *
29
 * @category Controller
30
 *
31
 * @Route("/admin/user")
32
 */
33
class UserController extends AbstractController
34
{
35
    /**
36
     * Lists all User entities.
37
     *
38
     * @Route("/", name="user")
39
     * @Method("GET")
40
     * @Template()
41
     *
42
     * @param \Symfony\Component\HttpFoundation\Request $request Paginate|Sort request
43
     * @return array
44
     */
45
    public function indexAction(Request $request)
46
    {
47
        $return = $this->abstractIndexAction('User', $request);
48
        
49
        return $return;
50
    }
51
52
    /**
53
     * Finds and displays a User entity.
54
     *
55
     * @Route("/{id}/show", name="user_show", requirements={"id"="\d+"})
56
     * @Method("GET")
57
     * @Template()
58
     *
59
     * @param \AppBundle\Entity\User $user User item to display
60
     * @return array
61
     */
62
    public function showAction(User $user)
63
    {
64
        $return = $this->abstractShowAction($user, 'user');
65
66
        return $return;
67
    }
68
69
    /**
70
     * Displays a form to create a new User entity.
71
     *
72
     * @Route("/new", name="user_new")
73
     * @Method("GET")
74
     * @Template()
75
     *
76
     * @return array
77
     */
78
    public function newAction()
79
    {
80
        $return = $this->abstractNewAction(
81
            'User',
82
            'AppBundle\Entity\User',
83
            UserType::class
84
        );
85
86
        return $return;
87
    }
88
89
    /**
90
     * Creates a new User entity.
91
     *
92
     * @Route("/create", name="user_create")
93
     * @Method("POST")
94
     * @Template("AppBundle:User:new.html.twig")
95
     *
96
     * @param \Symfony\Component\HttpFoundation\Request $request
97
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
98
     */
99
    public function createAction(Request $request)
100
    {
101
        $user = new User();
102
        $form = $this->createForm(UserType::class, $user, ['action' => $this->generateUrl('user_create'),]);
103
        $form->handleRequest($request);
104
        $return = ['user' => $user, 'form'   => $form->createView(),];
105
106
        if ($form->isValid()) {
107
            $return = $this->validUser($user, 'create');
108
        }
109
110
        return $return;
111
    }
112
113
    /**
114
     * Displays a form to edit an existing User entity.
115
     *
116
     * @Route("/{id}/edit", name="user_edit", requirements={"id"="\d+"})
117
     * @Method("GET")
118
     * @Template()
119
     *
120
     * @param \AppBundle\Entity\User $user User item to edit
121
     * @return array
122
     */
123
    public function editAction(User $user)
124
    {
125
        $editForm = $this->createForm(UserType::class, $user, array(
126
            'action' => $this->generateUrl('user_update', array('id' => $user->getId())),
127
            'method' => 'PUT',
128
            'passwordRequired' => false,
129
            'lockedRequired' => true
130
        ));
131
        $deleteForm = $this->createDeleteForm($user->getId(), 'user_delete');
132
 
133
        return array(
134
            'user' => $user,
135
            'edit_form'   => $editForm->createView(),
136
            'delete_form' => $deleteForm->createView(),
137
        );
138
    }
139
140
    /**
141
     * Edits an existing User entity.
142
     *
143
     * @Route("/{id}/update", name="user_update", requirements={"id"="\d+"})
144
     * @Method("PUT")
145
     * @Template("AppBundle:User:edit.html.twig")
146
     *
147
     * @param \AppBundle\Entity\User                    $user    User item to update
148
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
149
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
150
     */
151
    public function updateAction(User $user, Request $request)
152
    {
153
        $editForm = $this->createForm(UserType::class, $user, array(
154
            'action' => $this->generateUrl('user_update', array('id' => $user->getId())),
155
            'method' => 'PUT',
156
            'passwordRequired' => false,
157
            'lockedRequired' => true
158
        ));
159
        $deleteForm = $this->createDeleteForm($user->getId(), 'user_delete');
160
161
        $editForm->handleRequest($request);
162
        $return = ['user' => $user,
163
            'edit_form' => $editForm->createView(),
164
            'delete_form' => $deleteForm->createView(),];
165
166
        if ($editForm->isValid()) {
167
            $return = $this->validUser($user, 'edit');
168
        }
169
170
        return $return;
171
    }
172
173
    /**
174
     * Save order.
175
     *
176
     * @Route("/order/{entity}/{field}/{type}", name="user_sort")
177
     *
178
     * @param string $entity Entity of the field to sort
179
     * @param string $field  Field to sort
180
     * @param string $type   type of sort
181
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
182
     */
183
    public function sortAction($entity, $field, $type)
184
    {
185
        $this->get('app.helper.controller')->setOrder('article', $entity, $field, $type);
186
187
        return $this->redirectToRoute('user');
188
    }
189
190
    /**
191
     * Deletes a User entity.
192
     *
193
     * @Security("has_role('ROLE_SUPER_ADMIN')")
194
     * @Route("/{id}/delete", name="user_delete", requirements={"id"="\d+"})
195
     * @Method("DELETE")
196
     *
197
     * @param \AppBundle\Entity\User                    $user    User item to delete
198
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
199
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
200
     */
201
    public function deleteAction(User $user, Request $request)
202
    {
203
        $this->abstractDeleteAction($user, $request, 'user');
204
205
        return $this->redirectToRoute('user');
206
    }
207
208
    /**
209
     * Get user roles.
210
     *
211
     * @param \AppBundle\Entity\User $user
212
     * @return \AppBundle\Entity\User
213
     */
214
    private function getRoles(User $user)
215
    {
216
        $roles = '';
217
        foreach ($user->getGroups() as $key => $group) {
218
            if ($key === 0) {
219
                $roles = $group->getRoles();
220
            }
221
        }
222
223
        $user->setRoles($roles);
224
        
225
        return $user;
226
    }
227
228
    /**
229
     * Valid the user.
230
     *
231
     * @param \AppBundle\Entity\User $user
232
     * @param string                 $action
233
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
234
     */
235
    private function validUser(User $user, $action)
236
    {
237
        $user = $this->getRoles($user);
238
        if ($action === 'create') {
239
            $user->setEnabled(true);
240
        }
241
        $userManager = $this->get('fos_user.user_manager');
242
        $userManager->updateUser($user);
243
        $this->addFlash('info', 'gestock.' . $action . '.ok');
244
245
        if ($action === 'create') {
246
            $return = $this->redirectToRoute('user_show', ['id' => $user->getId()]);
247
        }
248
        if ($action === 'edit') {
249
            $return = $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
250
        }
251
252
        return $return;
0 ignored issues
show
Bug introduced by
The variable $return 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...
253
    }
254
}
255