| Conditions | 14 |
| Paths | 2 |
| Total Lines | 62 |
| Code Lines | 38 |
| 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 |
||
| 96 | #[IsGranted('ROLE_ADMIN')] |
||
| 97 | #[Route('/users/remove', name: 'chamilo_core_access_url_users_remove', methods: ['GET', 'POST'])] |
||
| 98 | public function removeUsers(Request $request): Response |
||
| 99 | { |
||
| 100 | $report = []; |
||
| 101 | |||
| 102 | if ($request->isMethod('POST') && $request->files->has('csv_file')) { |
||
| 103 | $file = $request->files->get('csv_file')->getPathname(); |
||
| 104 | $handle = fopen($file, 'r'); |
||
| 105 | $lineNumber = 0; |
||
| 106 | |||
| 107 | while (($data = fgetcsv($handle, 1000, ',')) !== false) { |
||
| 108 | $lineNumber++; |
||
| 109 | |||
| 110 | if ($lineNumber === 1 && strtolower(trim($data[0])) === 'username') { |
||
| 111 | continue; // Skip header |
||
| 112 | } |
||
| 113 | |||
| 114 | [$username, $url] = array_map('trim', $data); |
||
| 115 | |||
| 116 | if (!$username || !$url) { |
||
| 117 | $report[] = $this->formatReport('alert-circle', 'Line %s: empty fields.', [$lineNumber]); |
||
| 118 | continue; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!str_starts_with($url, 'http')) { |
||
| 122 | $url = 'https://' . $url; |
||
| 123 | } |
||
| 124 | if (!str_ends_with($url, '/')) { |
||
| 125 | $url .= '/'; |
||
| 126 | } |
||
| 127 | |||
| 128 | $user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]); |
||
| 129 | if (!$user) { |
||
| 130 | $report[] = $this->formatReport('close-circle', "Line %s: user '%s' not found.", [$lineNumber, $username]); |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | |||
| 134 | $accessUrl = $this->em->getRepository(AccessUrl::class)->findOneBy(['url' => $url]); |
||
| 135 | if (!$accessUrl) { |
||
| 136 | $report[] = $this->formatReport('close-circle', "Line %s: URL '%s' not found.", [$lineNumber, $url]); |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | foreach ($accessUrl->getUsers() as $rel) { |
||
| 141 | if ($rel->getUser()->getId() === $user->getId()) { |
||
| 142 | $this->em->remove($rel); |
||
| 143 | $report[] = $this->formatReport('account-remove-outline', "Line %s: user '%s' removed from '%s'.", [$lineNumber, $username, $url]); |
||
| 144 | continue 2; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | $report[] = $this->formatReport('alert-circle', 'Line %s: no relation found between user and URL.', [$lineNumber]); |
||
| 149 | } |
||
| 150 | |||
| 151 | fclose($handle); |
||
| 152 | $this->em->flush(); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $this->render('@ChamiloCore/AccessUrl/remove_users.html.twig', [ |
||
| 156 | 'report' => $report, |
||
| 157 | 'title' => $this->translator->trans('Remove users from URLs from CSV') |
||
| 158 | ]); |
||
| 167 |