| Conditions | 14 |
| Paths | 2 |
| Total Lines | 66 |
| Code Lines | 41 |
| 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 |
||
| 27 | #[IsGranted('ROLE_ADMIN')] |
||
| 28 | #[Route('/users/import', name: 'chamilo_core_access_url_users_import', methods: ['GET', 'POST'])] |
||
| 29 | public function importUsers(Request $request): Response |
||
| 30 | { |
||
| 31 | $report = []; |
||
| 32 | |||
| 33 | if ($request->isMethod('POST') && $request->files->has('csv_file')) { |
||
| 34 | $file = $request->files->get('csv_file')->getPathname(); |
||
| 35 | $handle = fopen($file, 'r'); |
||
| 36 | $lineNumber = 0; |
||
| 37 | |||
| 38 | while (($data = fgetcsv($handle, 1000, ',')) !== false) { |
||
| 39 | $lineNumber++; |
||
| 40 | |||
| 41 | if ($lineNumber === 1 && strtolower(trim($data[0])) === 'username') { |
||
| 42 | continue; // Skip header |
||
| 43 | } |
||
| 44 | |||
| 45 | if (count($data) < 2) { |
||
| 46 | $report[] = $this->formatReport('alert-circle', 'Line %s: invalid format. Two columns expected.', [$lineNumber]); |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | |||
| 50 | [$username, $url] = array_map('trim', $data); |
||
| 51 | |||
| 52 | if (!$username || !$url) { |
||
| 53 | $report[] = $this->formatReport('alert-circle', 'Line %s: missing username or URL.', [$lineNumber]); |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Normalize URL |
||
| 58 | if (!str_starts_with($url, 'http')) { |
||
| 59 | $url = 'https://' . $url; |
||
| 60 | } |
||
| 61 | if (!str_ends_with($url, '/')) { |
||
| 62 | $url .= '/'; |
||
| 63 | } |
||
| 64 | |||
| 65 | $user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]); |
||
| 66 | if (!$user) { |
||
| 67 | $report[] = $this->formatReport('close-circle', "Line %s: user '%s' not found.", [$lineNumber, $username]); |
||
| 68 | continue; |
||
| 69 | } |
||
| 70 | |||
| 71 | $accessUrl = $this->em->getRepository(AccessUrl::class)->findOneBy(['url' => $url]); |
||
| 72 | if (!$accessUrl) { |
||
| 73 | $report[] = $this->formatReport('close-circle', "Line %s: URL '%s' not found.", [$lineNumber, $url]); |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($accessUrl->hasUser($user)) { |
||
| 78 | $report[] = $this->formatReport('information-outline', "Line %s: user '%s' is already assigned to '%s'.", [$lineNumber, $username, $url]); |
||
| 79 | } else { |
||
| 80 | $accessUrl->addUser($user); |
||
| 81 | $this->em->persist($accessUrl); |
||
| 82 | $report[] = $this->formatReport('check-circle', "Line %s: user '%s' successfully assigned to '%s'.", [$lineNumber, $username, $url]); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | fclose($handle); |
||
| 87 | $this->em->flush(); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->render('@ChamiloCore/AccessUrl/import_users.html.twig', [ |
||
| 91 | 'report' => $report, |
||
| 92 | 'title' => $this->translator->trans('Assign users to URLs from CSV'), |
||
| 93 | ]); |
||
| 167 |