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