Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Controller/RolesController.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\Role;
8
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
9
use Kunstmaan\AdminBundle\Form\RoleType;
10
use Kunstmaan\AdminListBundle\AdminList\AdminList;
11
use Kunstmaan\UserManagementBundle\AdminList\RoleAdminListConfigurator;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
18
/**
19
 * Settings controller handling everything related to creating, editing, deleting and listing roles in an admin list
20
 */
21 View Code Duplication
class RolesController 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 roles
25
     *
26
     * @Route("/", name="KunstmaanUserManagementBundle_settings_roles")
27
     * @Template("@KunstmaanAdminList/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
        $em = $this->getDoctrine()->getManager();
38
        /* @var AdminList $adminlist */
39
        $adminlist = $this->container->get('kunstmaan_adminlist.factory')->createList(new RoleAdminListConfigurator($em));
40
        $adminlist->bindRequest($request);
41
42
        return [
43
            'adminlist' => $adminlist,
44
        ];
45
    }
46
47
    /**
48
     * Add a role
49
     *
50
     * @Route("/add", name="KunstmaanUserManagementBundle_settings_roles_add", methods={"GET", "POST"})
51
     * @Template("@KunstmaanUserManagement/Roles/add.html.twig")
52
     *
53
     * @throws AccessDeniedException
54
     *
55
     * @return array|RedirectResponse
0 ignored issues
show
Consider making the return type a bit more specific; maybe use RedirectResponse|array<s...omponent\Form\FormView>.

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...
56
     */
57
    public function addAction(Request $request)
58
    {
59
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
60
61
        /* @var EntityManager $em */
62
        $em = $this->getDoctrine()->getManager();
63
        $role = new Role('');
64
        $form = $this->createForm(RoleType::class, $role);
65
66
        if ($request->isMethod('POST')) {
67
            $form->handleRequest($request);
68
            if ($form->isSubmitted() && $form->isValid()) {
69
                $em->persist($role);
70
                $em->flush();
71
72
                $this->addFlash(
73
                    FlashTypes::SUCCESS,
74
                    $this->container->get('translator')->trans('kuma_user.roles.add.flash.success.%role%', [
75
                        '%role%' => $role->getRole(),
76
                    ])
77
                );
78
79
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
80
            }
81
        }
82
83
        return [
84
            'form' => $form->createView(),
85
        ];
86
    }
87
88
    /**
89
     * Edit a role
90
     *
91
     * @param int $id
92
     *
93
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_roles_edit", methods={"GET", "POST"})
94
     * @Template("@KunstmaanUserManagement/Roles/edit.html.twig")
95
     *
96
     * @throws AccessDeniedException
97
     *
98
     * @return array|RedirectResponse
0 ignored issues
show
Consider making the return type a bit more specific; maybe use RedirectResponse|array<s...ent\Form\FormView|Role>.

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...
99
     */
100
    public function editAction(Request $request, $id)
101
    {
102
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
103
104
        /* @var EntityManager $em */
105
        $em = $this->getDoctrine()->getManager();
106
        /* @var Role $role */
107
        $role = $em->getRepository(Role::class)->find($id);
108
        $form = $this->createForm(RoleType::class, $role);
109
110
        if ($request->isMethod('POST')) {
111
            $form->handleRequest($request);
112
            if ($form->isSubmitted() && $form->isValid()) {
113
                $em->persist($role);
114
                $em->flush();
115
116
                $this->addFlash(
117
                    FlashTypes::SUCCESS,
118
                    $this->container->get('translator')->trans('kuma_user.roles.edit.flash.success.%role%', [
119
                        '%role%' => $role->getRole(),
120
                    ])
121
                );
122
123
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
124
            }
125
        }
126
127
        return [
128
            'form' => $form->createView(),
129
            'role' => $role,
130
        ];
131
    }
132
133
    /**
134
     * Delete a role
135
     *
136
     * @param int $id
137
     *
138
     * @Route ("/{id}/delete", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_roles_delete", methods={"POST"})
139
     *
140
     * @throws AccessDeniedException
141
     *
142
     * @return RedirectResponse
143
     */
144
    public function deleteAction($id)
145
    {
146
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
147
148
        /* @var EntityManager $em */
149
        $em = $this->getDoctrine()->getManager();
150
        /* @var Role $role */
151
        $role = $em->getRepository(Role::class)->find($id);
152
        if (!\is_null($role)) {
153
            $em->remove($role);
154
            $em->flush();
155
156
            $this->addFlash(
157
                FlashTypes::SUCCESS,
158
                $this->container->get('translator')->trans('kuma_user.roles.delete.flash.success.%role%', [
159
                    '%role%' => $role->getRole(),
160
                ])
161
            );
162
        }
163
164
        return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
165
    }
166
}
167