Completed
Pull Request — master (#58)
by Laurent
03:26
created

GroupController::createAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 13
nc 2
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/group")
31
 */
32
class GroupController extends AbstractController
33
{
34
    /**
35
     * Lists all Group entities.
36
     *
37
     * @Route("/", name="group")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $return = $this->abstractIndexAction('Group');
44
45
        return $return;
46
    }
47
48
    /**
49
     * Finds and displays a Group entity.
50
     *
51
     * @Route("/{id}/show", name="group_show", requirements={"id"="\d+"})
52
     * @Method("GET")
53
     * @Template()
54
     */
55
    public function showAction(Group $group)
56
    {
57
        $return = $this->abstractShowAction($group, 'group');
58
59
        return $return;
60
    }
61
62
    /**
63
     * Displays a form to create a new Group entity.
64
     *
65
     * @Route("/new", name="group_new")
66
     * @Method("GET")
67
     * @Template()
68
     */
69
    public function newAction()
70
    {
71
        $group = new Group();
72
        $form = $this->createForm(new GroupType(), $group);
73
        $this->addRoles($form, $group);
0 ignored issues
show
Documentation introduced by
$form is of type object<Symfony\Component\Form\Form>, but the function expects a object<AppBundle\Controller\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
//        $form->add('roles', 'choice', array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
//            'choices' => $this->getExistingRoles(),
76
//            'data' => $group->getRoles(),
77
//            'label' => 'Roles',
78
//            'translation_domain' => 'admin',
79
//            'expanded' => true,
80
//            'multiple' => true,
81
//            'mapped' => true,
82
//        ));
83
84
85
        return array(
86
            'group' => $group,
87
            'form'   => $form->createView(),
88
        );
89
    }
90
91
    /**
92
     * Creates a new Group entity.
93
     *
94
     * @Route("/create", name="group_create")
95
     * @Method("POST")
96
     * @Template("AppBundle:Group:new.html.twig")
97
     */
98
    public function createAction(Request $request)
99
    {
100
        $group = new Group();
101
        $form = $this->createForm(new GroupType(), $group);
102
        $this->addRoles($form, $group);
0 ignored issues
show
Documentation introduced by
$form is of type object<Symfony\Component\Form\Form>, but the function expects a object<AppBundle\Controller\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
        if ($form->handleRequest($request)->isValid()) {
105
            $etm = $this->getDoctrine()->getManager();
106
            $etm->persist($group);
107
            $etm->flush();
108
            $this->addFlash('info', 'gestock.create.ok');
109
110
            return $this->redirectToRoute('group_show', array('id' => $group->getId()));
111
        }
112
113
        return array(
114
            'group' => $group,
115
            'form'   => $form->createView(),
116
        );
117
    }
118
119
    /**
120
     * Displays a form to edit an existing Group entity.
121
     *
122
     * @Route("/{id}/edit", name="group_edit", requirements={"id"="\d+"})
123
     * @Method("GET")
124
     * @Template()
125
     */
126 View Code Duplication
    public function editAction(Group $group)
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...
127
    {
128
        $editForm = $this->createForm(
129
            new GroupType(),
130
            $group,
131
            array(
132
                'action' => $this->generateUrl(
133
                    'group_update',
134
                    array('id' => $group->getId())
135
                ),
136
                'method' => 'PUT',
137
            )
138
        );
139
        $this->addRoles($editForm, $group);
0 ignored issues
show
Documentation introduced by
$editForm is of type object<Symfony\Component\Form\Form>, but the function expects a object<AppBundle\Controller\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
140
141
        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
142
143
        return array(
144
            'group' => $group,
145
            'edit_form'   => $editForm->createView(),
146
            'delete_form' => $deleteForm->createView(),
147
        );
148
    }
149
150
    /**
151
     * Edits an existing Group entity.
152
     *
153
     * @Route("/{id}/update", name="group_update", requirements={"id"="\d+"})
154
     * @Method("PUT")
155
     * @Template("AppBundle:Group:edit.html.twig")
156
     */
157 View Code Duplication
    public function updateAction(Group $group, 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...
158
    {
159
        $editForm = $this->createForm(new GroupType(), $group, array(
160
            'action' => $this->generateUrl('group_update', array('id' => $group->getId())),
161
            'method' => 'PUT',
162
        ));
163
        $this->addRoles($editForm, $group);
0 ignored issues
show
Documentation introduced by
$editForm is of type object<Symfony\Component\Form\Form>, but the function expects a object<AppBundle\Controller\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
165
        if ($editForm->handleRequest($request)->isValid()) {
166
            $this->getDoctrine()->getManager()->flush();
167
            $this->addFlash('info', 'gestock.edit.ok');
168
169
            return $this->redirectToRoute('group_edit', array('id' => $group->getId()));
170
        }
171
        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
172
173
        return array(
174
            'group' => $group,
175
            'edit_form'   => $editForm->createView(),
176
            'delete_form' => $deleteForm->createView(),
177
        );
178
    }
179
180
    /**
181
     * Deletes a Group entity.
182
     *
183
     * @Route("/{id}/delete", name="group_delete", requirements={"id"="\d+"})
184
     * @Method("DELETE")
185
     */
186
    public function deleteAction(Group $group, Request $request)
187
    {
188
        $form = $this->createDeleteForm($group->getId(), 'group_delete');
189
        
190
        $etm = $this->getDoctrine()->getManager();
191
        $users = $group->getUsers();
192
        foreach ($users as $user) {
193
            $user->getGroups()->removeElement($group);
194
        }
195
        $etm->flush();
196
197
        $this->get('fos_user.group_manager')->deleteGroup($group);
198
199
        if ($form->handleRequest($request)->isValid()) {
200
            $etm = $this->getDoctrine()->getManager();
201
            $etm->remove($group);
202
            $etm->flush();
203
        }
204
205
        return $this->redirectToRoute('group');
206
    }
207
208
    /**
209
     * Get the existing roles
210
     *
211
     * @return array Array of roles
212
     */
213
    private function getExistingRoles()
214
    {
215
        $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
216
        $roles = array_keys($roleHierarchy);
217
        $theRoles = array();
218
219
        foreach ($roles as $role) {
220
            $theRoles[$role] = $role;
221
        }
222
        return $theRoles;
223
    }
224
225
    /**
226
     * Add roles to form
227
     *
228
     * @param Form  $form  The form in which to insert the roles
229
     * @param Group $group The entity to deal
230
     * @return Form The form
231
     */
232
    private function addRoles($form, $group)
233
    {
234
        $form->add('roles', 'choice', array(
235
            'choices' => $this->getExistingRoles(),
236
            'data' => $group->getRoles(),
237
            'label' => 'Roles',
238
            'expanded' => true,
239
            'multiple' => true,
240
            'mapped' => true,
241
        ));
242
243
        return $form;
244
    }
245
}
246