Completed
Push — sf2.7 ( 8e4ec4...712e60 )
by Laurent
03:04
created

GroupController::editAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 9.2
nc 1
cc 1
eloc 16
nop 1
1
<?php
2
/**
3
 * GroupController controller des groupes d'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 AppBundle\Entity\Group;
23
use AppBundle\Form\Type\GroupType;
24
25
/**
26
 * Group controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/groups")
31
 */
32
class GroupController extends AbstractController
33
{
34
    /**
35
     * Lists all Group entities.
36
     *
37
     * @Route("/", name="admin_groups")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:Group')->findAll();
45
        
46
        return array('entities'  => $entities);
47
    }
48
49
    /**
50
     * Finds and displays a Group entity.
51
     *
52
     * @Route("/{id}/show", name="admin_groups_show", requirements={"id"="\d+"})
53
     * @Method("GET")
54
     * @Template()
55
     */
56
    public function showAction(Group $group)
57
    {
58
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
59
60
        return array(
61
            'group' => $group,
62
            'delete_form' => $deleteForm->createView(),
63
        );
64
    }
65
66
    /**
67
     * Displays a form to create a new Group entity.
68
     *
69
     * @Route("/new", name="admin_groups_new")
70
     * @Method("GET")
71
     * @Template()
72
     */
73
    public function newAction()
74
    {
75
        $group = new Group();
76
        $form = $this->createForm(new GroupType(), $group);
77
        $form->add('roles', 'choice', array(
78
            'choices' => $this->getExistingRoles(),
79
            'data' => $group->getRoles(),
80
            'label' => 'Roles',
81
            'translation_domain' => 'admin',
82
            'expanded' => true,
83
            'multiple' => true,
84
            'mapped' => true,
85
        ));
86
87
88
        return array(
89
            'group' => $group,
90
            'form'   => $form->createView(),
91
        );
92
    }
93
94
    /**
95
     * Creates a new Group entity.
96
     *
97
     * @Route("/create", name="admin_groups_create")
98
     * @Method("POST")
99
     * @Template("AppBundle:Group:new.html.twig")
100
     */
101 View Code Duplication
    public function createAction(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...
102
    {
103
        $group = new Group();
104
        $form = $this->createForm(new GroupType(), $group);
105
        $form->add('roles', 'choice', array(
106
            'choices' => $this->getExistingRoles(),
107
            'data' => $group->getRoles(),
108
            'label' => 'Roles',
109
            'expanded' => true,
110
            'multiple' => true,
111
            'mapped' => true,
112
        ));
113
        if ($form->handleRequest($request)->isValid()) {
114
            $em = $this->getDoctrine()->getManager();
115
            $em->persist($group);
116
            $em->flush();
117
118
            return $this->redirectToRoute('admin_groups_show', array('id', $group->getId()));
119
        }
120
121
        return array(
122
            'group' => $group,
123
            'form'   => $form->createView(),
124
        );
125
    }
126
127
    /**
128
     * Displays a form to edit an existing Group entity.
129
     *
130
     * @Route("/{id}/edit", name="admin_groups_edit", requirements={"id"="\d+"})
131
     * @Method("GET")
132
     * @Template()
133
     */
134
    public function editAction(Group $group=null)
135
    {
136
        $editForm = $this->createForm(new GroupType(), $group, array(
137
            'action' => $this->generateUrl('admin_groups_update', array('id' => $group->getId())),
0 ignored issues
show
Bug introduced by
It seems like $group 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...
138
            'method' => 'PUT',
139
        ));
140
        $editForm->add('roles', 'choice', array(
141
            'choices' => $this->getExistingRoles(),
142
            'data' => $group->getRoles(),
143
            'label' => 'Roles',
144
            'expanded' => true,
145
            'multiple' => true,
146
            'mapped' => true,
147
        ));
148
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
149
150
        return array(
151
            'group' => $group,
152
            'edit_form'   => $editForm->createView(),
153
            'delete_form' => $deleteForm->createView(),
154
        );
155
    }
156
157
    /**
158
     * Edits an existing Group entity.
159
     *
160
     * @Route("/{id}/update", name="admin_groups_update", requirements={"id"="\d+"})
161
     * @Method("PUT")
162
     * @Template("AppBundle:Group:edit.html.twig")
163
     */
164
    public function updateAction(Request $request, Group $group=null)
165
    {
166
        $editForm = $this->createForm(new GroupType(), $group, array(
167
            'action' => $this->generateUrl('admin_groups_update', array('id' => $group->getId())),
0 ignored issues
show
Bug introduced by
It seems like $group 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...
168
            'method' => 'PUT',
169
        ));
170
        $editForm->add('roles', 'choice', array(
171
            'choices' => $this->getExistingRoles(),
172
            'data' => $group->getRoles(),
173
            'label' => 'Roles',
174
            'expanded' => true,
175
            'multiple' => true,
176
            'mapped' => true,
177
        ));
178
        if ($editForm->handleRequest($request)->isValid()) {
179
            $this->getDoctrine()->getManager()->flush();
180
181
            return $this->redirectToRoute('admin_groups_edit', array('id' => $group->getId()));
182
        }
183
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
184
185
        return array(
186
            'group' => $group,
187
            'edit_form'   => $editForm->createView(),
188
            'delete_form' => $deleteForm->createView(),
189
        );
190
    }
191
192
    /**
193
     * Deletes a Group entity.
194
     *
195
     * @Route("/{id}/delete", name="admin_groups_delete", requirements={"id"="\d+"})
196
     * @Method("DELETE")
197
     */
198
    public function deleteAction(Request $request, Group $group=null)
199
    {
200
        $form = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
0 ignored issues
show
Bug introduced by
It seems like $group 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...
201
        
202
        $em = $this->getDoctrine()->getManager();
203
        $users = $group->getUsers();
204
        foreach ($users as $user) {
205
            $user->getGroups()->removeElement($group);
206
        }
207
        $em->flush();
208
209
        $this->get('fos_user.group_manager')->deleteGroup($group);
210
211
        if ($form->handleRequest($request)->isValid()) {
212
            $em = $this->getDoctrine()->getManager();
213
            $em->remove($group);
214
            $em->flush();
215
        }
216
217
        return $this->redirectToRoute('admin_groups');
218
    }
219
220
    /**
221
     * Get the existing roles
222
     *
223
     * @return array Array of roles
224
     */
225
    private function getExistingRoles()
226
    {
227
        $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
228
        $roles = array_keys($roleHierarchy);
229
        $theRoles = array();
230
231
        foreach ($roles as $role) {
232
            $theRoles[$role] = $role;
233
        }
234
        return $theRoles;
235
    }
236
}
237