Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Controller/GroupsController.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\UserManagementBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Kunstmaan\AdminBundle\Controller\BaseSettingsController;
8
use Kunstmaan\AdminBundle\Entity\Group;
9
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
10
use Kunstmaan\AdminBundle\Form\GroupType;
11
use Kunstmaan\AdminListBundle\AdminList\AdminList;
12
13
use Kunstmaan\UserManagementBundle\AdminList\GroupAdminListConfigurator;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
21
22
/**
23
 * Settings controller handling everything related to creating, editing, deleting and listing groups in an admin list
24
 */
25 View Code Duplication
class GroupsController extends BaseSettingsController
0 ignored issues
show
This class 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...
26
{
27
    /**
28
     * List groups
29
     *
30
     * @Route("/", name="KunstmaanUserManagementBundle_settings_groups")
31
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
32
     *
33
     * @throws AccessDeniedException
34
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,AdminList>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
35
     */
36
    public function listAction(Request $request)
37
    {
38
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
39
40
        /* @var $em EntityManager */
41
        $em = $this->getDoctrine()->getManager();
42
        /* @var AdminList $adminlist */
43
        $adminlist = $this->container->get("kunstmaan_adminlist.factory")->createList(new GroupAdminListConfigurator($em));
44
        $adminlist->bindRequest($request);
45
46
        return array(
47
            'adminlist' => $adminlist,
48
        );
49
    }
50
51
    /**
52
     * Add a group
53
     *
54
     * @Route("/add", name="KunstmaanUserManagementBundle_settings_groups_add")
55
     * @Method({"GET", "POST"})
56
     * @Template("@KunstmaanUserManagement/Groups/add.html.twig")
57
     *
58
     * @throws AccessDeniedException
59
     * @return array
0 ignored issues
show
Should the return type not be RedirectResponse|array<s...omponent\Form\FormView>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61
    public function addAction(Request $request)
62
    {
63
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
64
65
        /* @var $em EntityManager */
66
        $em = $this->getDoctrine()->getManager();
67
        $group = new Group();
68
        $form = $this->createForm(GroupType::class, $group);
69
70
        if ($request->isMethod('POST')) {
71
            $form->handleRequest($request);
72
            if ($form->isSubmitted() && $form->isValid()) {
73
                $em->persist($group);
74
                $em->flush();
75
76
                $this->addFlash(
77
                    FlashTypes::SUCCESS,
78
                    $this->container->get('translator')->trans('kuma_user.group.add.flash.success', array(
79
                        '%groupname%' => $group->getName()
80
                    ))
81
                );
82
83
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
84
            }
85
        }
86
87
        return array(
88
            'form' => $form->createView(),
89
        );
90
    }
91
92
    /**
93
     * Edit a group
94
     *
95
     * @param int $id
96
     *
97
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_edit")
98
     * @Method({"GET", "POST"})
99
     * @Template("@KunstmaanUserManagement/Groups/edit.html.twig")
100
     *
101
     * @throws AccessDeniedException
102
     * @return array
0 ignored issues
show
Should the return type not be RedirectResponse|array<s...nt\Form\FormView|Group>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
     */
104
    public function editAction(Request $request, $id)
105
    {
106
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
107
108
        /* @var $em EntityManager */
109
        $em = $this->getDoctrine()->getManager();
110
        /* @var Group $group */
111
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
112
        $form = $this->createForm(GroupType::class, $group);
113
114
        if ($request->isMethod('POST')) {
115
            $form->handleRequest($request);
116
            if ($form->isSubmitted() && $form->isValid()) {
117
                $em->persist($group);
118
                $em->flush();
119
120
                $this->addFlash(
121
                    FlashTypes::SUCCESS,
122
                    $this->container->get('translator')->trans('kuma_user.group.edit.flash.success', array(
123
                        '%groupname%' => $group->getName()
124
                    ))
125
                );
126
127
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
128
            }
129
        }
130
131
        return array(
132
            'form'  => $form->createView(),
133
            'group' => $group
134
        );
135
    }
136
137
    /**
138
     * Delete a group
139
     *
140
     * @param int $id
141
     *
142
     * @Route("/{id}/delete", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_delete")
143
     * @Method({"GET", "POST"})
144
     *
145
     * @throws AccessDeniedException
146
     * @return RedirectResponse
147
     */
148
    public function deleteAction($id)
149
    {
150
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
151
152
        /* @var $em EntityManager */
153
        $em = $this->getDoctrine()->getManager();
154
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
155
        if (!is_null($group)) {
156
            $em->remove($group);
157
            $em->flush();
158
159
            $this->addFlash(
160
                FlashTypes::SUCCESS,
161
                $this->container->get('translator')->trans('kuma_user.group.delete.flash.success', array(
162
                    '%groupname%' => $group->getName()
163
                ))
164
            );
165
        }
166
167
        return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
168
    }
169
170
}
171