Completed
Push — master ( 64ecaf...80f2c6 )
by Laurent
02:52
created

GroupController::addRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
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
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
25
26
/**
27
 * Group controller.
28
 *
29
 * @category Controller
30
 *
31
 * @Route("/admin/group")
32
 */
33
class GroupController extends AbstractController
34
{
35
    /**
36
     * Lists all Group entities.
37
     *
38
     * @Route("/", name="group")
39
     * @Method("GET")
40
     * @Template()
41
     *
42
     * @return array
43
     */
44
    public function indexAction()
45
    {
46
        $return = $this->abstractIndexAction('Group', null);
47
48
        return $return;
49
    }
50
51
    /**
52
     * Finds and displays a Group entity.
53
     *
54
     * @Route("/{id}/show", name="group_show", requirements={"id"="\d+"})
55
     * @Method("GET")
56
     * @Template()
57
     *
58
     * @param \AppBundle\Entity\Group $group Group to display
59
     * @return array
60
     */
61
    public function showAction(Group $group)
62
    {
63
        $return = $this->abstractShowAction($group, 'group');
64
65
        return $return;
66
    }
67
68
    /**
69
     * Displays a form to create a new Group entity.
70
     *
71
     * @Route("/new", name="group_new")
72
     * @Method("GET")
73
     * @Template()
74
     *
75
     * @return array
76
     */
77
    public function newAction()
78
    {
79
        $group = new Group();
80
        $form = $this->createForm(GroupType::class, $group);
81
        $this->addRoles($form, $group);
0 ignored issues
show
Bug introduced by
The method addRoles() cannot be called from this context as it is declared private in class AppBundle\Controller\AbstractController.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
82
83
        return array(
84
            'group' => $group,
85
            'form'   => $form->createView(),
86
        );
87
    }
88
89
    /**
90
     * Creates a new Group entity.
91
     *
92
     * @Route("/create", name="group_create")
93
     * @Method("POST")
94
     * @Template("AppBundle:Group:new.html.twig")
95
     *
96
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
97
     * @return array
98
     */
99
    public function createAction(Request $request)
100
    {
101
        $group = new Group();
102
        $form = $this->createForm(GroupType::class, $group);
103
        $this->addRoles($form, $group);
0 ignored issues
show
Bug introduced by
The method addRoles() cannot be called from this context as it is declared private in class AppBundle\Controller\AbstractController.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
104
        $form->handleRequest($request);
105
        $return = ['group' => $group, 'form' => $form->createView(),];
106
107
        if ($form->isValid()) {
108
            $etm = $this->getDoctrine()->getManager();
109
            $etm->persist($group);
110
            $etm->flush();
111
            $this->addFlash('info', 'gestock.create.ok');
112
113
            $return = $this->redirectToRoute('group_show', array('id' => $group->getId()));
114
        }
115
116
        return $return;
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
     * @param \AppBundle\Entity\Group $group Group item to edit
127
     * @return array
128
     */
129
    public function editAction(Group $group)
130
    {
131
        $return = $this->abstractEditAction($group, 'group', GroupType::class);
132
//        $editForm = $this->createForm(GroupType::class, $group, array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
133
//            'action' => $this->generateUrl('group_update', array('id' => $group->getId())),
134
//            'method' => 'PUT',
135
//        ));
136
//        $this->addRoles($editForm, $group);
137
//
138
//        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
139
//
140
//        return array(
141
//            'group' => $group,
142
//            'edit_form'   => $editForm->createView(),
143
//            'delete_form' => $deleteForm->createView(),
144
//        );
145
146
        return $return;
147
    }
148
149
    /**
150
     * Edits an existing Group entity.
151
     *
152
     * @Route("/{id}/update", name="group_update", requirements={"id"="\d+"})
153
     * @Method("PUT")
154
     * @Template("AppBundle:Group:edit.html.twig")
155
     *
156
     * @param \AppBundle\Entity\Group                   $group   Group item to update
157
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
158
     * @return array
159
     */
160
    public function updateAction(Group $group, Request $request)
161
    {
162
        $return = $this->abstractUpdateAction(
163
            $group,
164
            $request,
165
            'group',
166
            GroupType::class
167
        );
168
169
//        $editForm = $this->createForm(GroupType::class, $group, array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
170
//            'action' => $this->generateUrl('group_update', array('id' => $group->getId())),
171
//            'method' => 'PUT',
172
//        ));
173
//        $this->addRoles($editForm, $group);
174
//
175
//        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
176
//
177
//        $return = array(
178
//            'group' => $group,
179
//            'edit_form'   => $editForm->createView(),
180
//            'delete_form' => $deleteForm->createView(),
181
//        );
182
//
183
//        if ($editForm->handleRequest($request)->isValid()) {
184
//            $this->getDoctrine()->getManager()->flush();
185
//            $this->addFlash('info', 'gestock.edit.ok');
186
//
187
//            $return = $this->redirectToRoute('group_edit', array('id' => $group->getId()));
188
//        }
189
190
        return $return;
191
    }
192
193
    /**
194
     * Deletes a Group entity.
195
     *
196
     * @Route("/{id}/delete", name="group_delete", requirements={"id"="\d+"})
197
     * @Method("DELETE")
198
     *
199
     * @param \AppBundle\Entity\Group                   $group   Group item to delete
200
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
201
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
202
     */
203
    public function deleteAction(Group $group, Request $request)
204
    {
205
        $form = $this->createDeleteForm($group->getId(), 'group_delete');
206
        
207
        $etm = $this->getDoctrine()->getManager();
208
        $users = $group->getUsers();
209
        foreach ($users as $user) {
210
            $user->getGroups()->removeElement($group);
211
        }
212
        $etm->flush();
213
214
        $this->get('fos_user.group_manager')->deleteGroup($group);
215
216
        if ($form->handleRequest($request)->isValid()) {
217
            $etm = $this->getDoctrine()->getManager();
218
            $etm->remove($group);
219
            $etm->flush();
220
        }
221
222
        return $this->redirectToRoute('group');
223
    }
224
}
225