Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
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
use Kunstmaan\AdminBundle\Controller\BaseSettingsController;
7
use Kunstmaan\AdminBundle\Entity\Group;
8
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
9
use Kunstmaan\AdminBundle\Form\GroupType;
10
use Kunstmaan\AdminListBundle\AdminList\AdminList;
11
use Kunstmaan\UserManagementBundle\AdminList\GroupAdminListConfigurator;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
18
/**
19
 * Settings controller handling everything related to creating, editing, deleting and listing groups in an admin list
20
 */
21 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...
22
{
23
    /**
24
     * List groups
25
     *
26
     * @Route("/", name="KunstmaanUserManagementBundle_settings_groups")
27
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
28
     *
29
     * @throws AccessDeniedException
30
     *
31
     * @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...
32
     */
33
    public function listAction(Request $request)
34
    {
35
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
36
37
        /* @var $em EntityManager */
38
        $em = $this->getDoctrine()->getManager();
39
        /* @var AdminList $adminlist */
40
        $adminlist = $this->container->get('kunstmaan_adminlist.factory')->createList(new GroupAdminListConfigurator($em));
41
        $adminlist->bindRequest($request);
42
43
        return array(
44
            'adminlist' => $adminlist,
45
        );
46
    }
47
48
    /**
49
     * Add a group
50
     *
51
     * @Route("/add", name="KunstmaanUserManagementBundle_settings_groups_add", methods={"GET", "POST"})
52
     * @Template("@KunstmaanUserManagement/Groups/add.html.twig")
53
     *
54
     * @throws AccessDeniedException
55
     *
56
     * @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...
57
     */
58
    public function addAction(Request $request)
59
    {
60
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
61
62
        /* @var $em EntityManager */
63
        $em = $this->getDoctrine()->getManager();
64
        $group = new Group();
65
        $form = $this->createForm(GroupType::class, $group);
66
67
        if ($request->isMethod('POST')) {
68
            $form->handleRequest($request);
69
            if ($form->isSubmitted() && $form->isValid()) {
70
                $em->persist($group);
71
                $em->flush();
72
73
                $this->addFlash(
74
                    FlashTypes::SUCCESS,
75
                    $this->container->get('translator')->trans('kuma_user.group.add.flash.success', array(
76
                        '%groupname%' => $group->getName(),
77
                    ))
78
                );
79
80
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
81
            }
82
        }
83
84
        return array(
85
            'form' => $form->createView(),
86
        );
87
    }
88
89
    /**
90
     * Edit a group
91
     *
92
     * @param int $id
93
     *
94
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_edit", methods={"GET", "POST"})
95
     * @Template("@KunstmaanUserManagement/Groups/edit.html.twig")
96
     *
97
     * @throws AccessDeniedException
98
     *
99
     * @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...
100
     */
101
    public function editAction(Request $request, $id)
102
    {
103
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
104
105
        /* @var $em EntityManager */
106
        $em = $this->getDoctrine()->getManager();
107
        /* @var Group $group */
108
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
109
        $form = $this->createForm(GroupType::class, $group);
110
111
        if ($request->isMethod('POST')) {
112
            $form->handleRequest($request);
113
            if ($form->isSubmitted() && $form->isValid()) {
114
                $em->persist($group);
115
                $em->flush();
116
117
                $this->addFlash(
118
                    FlashTypes::SUCCESS,
119
                    $this->container->get('translator')->trans('kuma_user.group.edit.flash.success', array(
120
                        '%groupname%' => $group->getName(),
121
                    ))
122
                );
123
124
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
125
            }
126
        }
127
128
        return array(
129
            'form' => $form->createView(),
130
            'group' => $group,
131
        );
132
    }
133
134
    /**
135
     * Delete a group
136
     *
137
     * @param int $id
138
     *
139
     * @Route("/{id}/delete", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_delete", methods={"GET", "POST"})
140
     *
141
     * @throws AccessDeniedException
142
     *
143
     * @return RedirectResponse
144
     */
145
    public function deleteAction($id)
146
    {
147
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
148
149
        /* @var $em EntityManager */
150
        $em = $this->getDoctrine()->getManager();
151
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
152
        if (!is_null($group)) {
153
            $em->remove($group);
154
            $em->flush();
155
156
            $this->addFlash(
157
                FlashTypes::SUCCESS,
158
                $this->container->get('translator')->trans('kuma_user.group.delete.flash.success', array(
159
                    '%groupname%' => $group->getName(),
160
                ))
161
            );
162
        }
163
164
        return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
165
    }
166
}
167