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 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 groups in an admin list |
||
20 | */ |
||
21 | View Code Duplication | class GroupsController extends BaseSettingsController |
|
0 ignored issues
–
show
|
|||
22 | { |
||
23 | /** |
||
24 | * List groups |
||
25 | * |
||
26 | * @Route("/", name="KunstmaanUserManagementBundle_settings_groups") |
||
27 | * @Template("@KunstmaanAdminList/Default/list.html.twig") |
||
28 | * |
||
29 | * @throws AccessDeniedException |
||
30 | * |
||
31 | * @return array |
||
0 ignored issues
–
show
|
|||
32 | */ |
||
33 | public function listAction(Request $request) |
||
34 | { |
||
35 | $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); |
||
36 | |||
37 | /* @var EntityManager $em */ |
||
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 [ |
||
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
|
|||
57 | */ |
||
58 | public function addAction(Request $request) |
||
59 | { |
||
60 | $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); |
||
61 | |||
62 | /* @var EntityManager $em */ |
||
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', [ |
||
76 | '%groupname%' => $group->getName(), |
||
77 | ]) |
||
78 | ); |
||
79 | |||
80 | return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups')); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return [ |
||
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
|
|||
100 | */ |
||
101 | public function editAction(Request $request, $id) |
||
102 | { |
||
103 | $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); |
||
104 | |||
105 | /* @var EntityManager $em */ |
||
106 | $em = $this->getDoctrine()->getManager(); |
||
107 | /* @var Group $group */ |
||
108 | $group = $em->getRepository(Group::class)->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', [ |
||
120 | '%groupname%' => $group->getName(), |
||
121 | ]) |
||
122 | ); |
||
123 | |||
124 | return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups')); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return [ |
||
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={"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 EntityManager $em */ |
||
150 | $em = $this->getDoctrine()->getManager(); |
||
151 | $group = $em->getRepository(Group::class)->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', [ |
||
159 | '%groupname%' => $group->getName(), |
||
160 | ]) |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups')); |
||
165 | } |
||
166 | } |
||
167 |
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.