Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

Controller/RolesController.php (1 issue)

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 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 roles in an admin list
20
 */
21 View Code Duplication
class RolesController extends BaseSettingsController
22
{
23
    /**
24
     * List roles
25
     *
26
     * @Route("/", name="KunstmaanUserManagementBundle_settings_roles")
27
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
28
     *
29
     * @throws AccessDeniedException
30
     *
31
     * @return array
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));
0 ignored issues
show
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManagerInterface>. It seems like you assume a child interface of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
40
        $adminlist->bindRequest($request);
41
42
        return array(
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
56
     */
57
    public function addAction(Request $request)
58
    {
59
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
60
61
        /* @var $em EntityManager */
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 array(
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
99
     */
100
    public function editAction(Request $request, $id)
101
    {
102
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
103
104
        /* @var $em EntityManager */
105
        $em = $this->getDoctrine()->getManager();
106
        /* @var Role $role */
107
        $role = $em->getRepository('KunstmaanAdminBundle:Role')->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 array(
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 $em EntityManager */
149
        $em = $this->getDoctrine()->getManager();
150
        /* @var Role $role */
151
        $role = $em->getRepository('KunstmaanAdminBundle:Role')->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