Conditions | 11 |
Paths | 10 |
Total Lines | 59 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | #[IsGranted('ROLE_ADMIN')] |
||
43 | #[Route('/permissions', name: 'permissions')] |
||
44 | public function index( |
||
45 | PermissionRepository $permissionRepo, |
||
46 | PermissionRelRoleRepository $permissionRelRoleRepo, |
||
47 | Request $request, |
||
48 | EntityManagerInterface $em |
||
49 | ): Response { |
||
50 | $permissions = $permissionRepo->findAll(); |
||
51 | $roles = ['ROLE_INVITEE', 'ROLE_STUDENT', 'ROLE_TEACHER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN', 'ROLE_GLOBAL_ADMIN', 'ROLE_RRHH', 'ROLE_QUESTION_MANAGER', 'ROLE_SESSION_MANAGER', 'ROLE_STUDENT_BOSS']; |
||
52 | |||
53 | if ($request->isMethod('POST')) { |
||
54 | $data = $request->request->all('permissions'); |
||
55 | foreach ($permissions as $permission) { |
||
56 | foreach ($roles as $role) { |
||
57 | $checkboxValue = isset($data[$permission->getSlug()][$role]); |
||
58 | error_log('Processing role: ' . $role . ' with value: ' . ($checkboxValue ? 'true' : 'false')); |
||
59 | $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
||
60 | |||
61 | if ($checkboxValue) { |
||
62 | if (!$permRelRole) { |
||
63 | $permRelRole = new PermissionRelRole(); |
||
64 | $permRelRole->setPermission($permission); |
||
65 | $permRelRole->setRoleCode($role); |
||
66 | } |
||
67 | $permRelRole->setChangeable(true); |
||
68 | $permRelRole->setUpdatedAt(new \DateTime()); |
||
69 | $em->persist($permRelRole); |
||
70 | error_log('Persisting PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
||
71 | } else { |
||
72 | if ($permRelRole) { |
||
73 | $em->remove($permRelRole); |
||
74 | error_log('Removing PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | $em->flush(); |
||
80 | error_log('Flush complete'); |
||
81 | |||
82 | return $this->redirectToRoute('permissions'); |
||
83 | } |
||
84 | |||
85 | $forms = []; |
||
86 | foreach ($permissions as $permission) { |
||
87 | $defaultData = []; |
||
88 | foreach ($roles as $role) { |
||
89 | $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
||
90 | $defaultData[$role] = $permRelRole ? $permRelRole->isChangeable() : false; |
||
91 | } |
||
92 | |||
93 | $form = $this->createForm(PermissionType::class, $defaultData, ['roles' => $roles]); |
||
94 | $forms[$permission->getSlug()] = $form->createView(); |
||
95 | } |
||
96 | |||
97 | return $this->render('@ChamiloCore/Permission/index.html.twig', [ |
||
98 | 'permissions' => $permissions, |
||
99 | 'forms' => $forms, |
||
100 | 'roles' => $roles |
||
101 | ]); |
||
104 |