| 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 |
||
| 39 | #[IsGranted('ROLE_ADMIN')] |
||
| 40 | #[Route('/permissions', name: 'permissions')] |
||
| 41 | public function index( |
||
| 42 | PermissionRepository $permissionRepo, |
||
| 43 | PermissionRelRoleRepository $permissionRelRoleRepo, |
||
| 44 | Request $request, |
||
| 45 | EntityManagerInterface $em |
||
| 46 | ): Response { |
||
| 47 | $permissions = $permissionRepo->findAll(); |
||
| 48 | $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']; |
||
| 49 | |||
| 50 | if ($request->isMethod('POST')) { |
||
| 51 | $data = $request->request->all('permissions'); |
||
| 52 | foreach ($permissions as $permission) { |
||
| 53 | foreach ($roles as $role) { |
||
| 54 | $checkboxValue = isset($data[$permission->getSlug()][$role]); |
||
| 55 | error_log('Processing role: ' . $role . ' with value: ' . ($checkboxValue ? 'true' : 'false')); |
||
| 56 | $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
||
| 57 | |||
| 58 | if ($checkboxValue) { |
||
| 59 | if (!$permRelRole) { |
||
| 60 | $permRelRole = new PermissionRelRole(); |
||
| 61 | $permRelRole->setPermission($permission); |
||
| 62 | $permRelRole->setRoleCode($role); |
||
| 63 | } |
||
| 64 | $permRelRole->setChangeable(true); |
||
| 65 | $permRelRole->setUpdatedAt(new \DateTime()); |
||
| 66 | $em->persist($permRelRole); |
||
| 67 | error_log('Persisting PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
||
| 68 | } else { |
||
| 69 | if ($permRelRole) { |
||
| 70 | $em->remove($permRelRole); |
||
| 71 | error_log('Removing PermissionRelRole for permission: ' . $permission->getSlug() . ' and role: ' . $role); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | $em->flush(); |
||
| 77 | error_log('Flush complete'); |
||
| 78 | |||
| 79 | return $this->redirectToRoute('permissions'); |
||
| 80 | } |
||
| 81 | |||
| 82 | $forms = []; |
||
| 83 | foreach ($permissions as $permission) { |
||
| 84 | $defaultData = []; |
||
| 85 | foreach ($roles as $role) { |
||
| 86 | $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); |
||
| 87 | $defaultData[$role] = $permRelRole ? $permRelRole->isChangeable() : false; |
||
| 88 | } |
||
| 89 | |||
| 90 | $form = $this->createForm(PermissionType::class, $defaultData, ['roles' => $roles]); |
||
| 91 | $forms[$permission->getSlug()] = $form->createView(); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this->render('@ChamiloCore/Permission/index.html.twig', [ |
||
| 95 | 'permissions' => $permissions, |
||
| 96 | 'forms' => $forms, |
||
| 97 | 'roles' => $roles |
||
| 98 | ]); |
||
| 101 |