| Conditions | 9 |
| Paths | 107 |
| Total Lines | 75 |
| Code Lines | 51 |
| 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 |
||
| 29 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 30 | { |
||
| 31 | $io = new SymfonyStyle($input, $output); |
||
| 32 | |||
| 33 | $io->title('Importing genres from IGDB API'); |
||
| 34 | |||
| 35 | try { |
||
| 36 | $genres = $this->igdbClient->getAllGenres(); |
||
| 37 | $io->progressStart(count($genres)); |
||
| 38 | |||
| 39 | $insertedCount = 0; |
||
| 40 | $updatedCount = 0; |
||
| 41 | $skippedCount = 0; |
||
| 42 | |||
| 43 | foreach ($genres as $genreData) { |
||
| 44 | $existingGenre = $this->entityManager->getRepository(Genre::class)->find($genreData['id']); |
||
| 45 | |||
| 46 | if ($existingGenre) { |
||
| 47 | // Comparer les updated_at pour savoir si une mise à jour est nécessaire |
||
| 48 | $needsUpdate = true; |
||
| 49 | if (isset($genreData['updated_at'])) { |
||
| 50 | $igdbUpdatedAt = new \DateTimeImmutable('@' . $genreData['updated_at']); |
||
| 51 | $needsUpdate = $existingGenre->getUpdatedAt() < $igdbUpdatedAt; |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($needsUpdate) { |
||
| 55 | $existingGenre->setName($genreData['name']); |
||
| 56 | $existingGenre->setSlug($genreData['slug']); |
||
| 57 | $existingGenre->setUrl($genreData['url'] ?? null); |
||
| 58 | |||
| 59 | if (isset($genreData['updated_at'])) { |
||
| 60 | $existingGenre->setUpdatedAt(new \DateTimeImmutable('@' . $genreData['updated_at'])); |
||
| 61 | } else { |
||
| 62 | $existingGenre->setUpdatedAt(new \DateTimeImmutable()); |
||
| 63 | } |
||
| 64 | $updatedCount++; |
||
| 65 | } else { |
||
| 66 | $skippedCount++; |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $genre = new Genre(); |
||
| 70 | $genre->setId($genreData['id']); |
||
| 71 | $genre->setName($genreData['name']); |
||
| 72 | $genre->setSlug($genreData['slug']); |
||
| 73 | $genre->setUrl($genreData['url'] ?? null); |
||
| 74 | |||
| 75 | if (isset($genreData['created_at'])) { |
||
| 76 | $genre->setCreatedAt(new \DateTimeImmutable('@' . $genreData['created_at'])); |
||
| 77 | } |
||
| 78 | if (isset($genreData['updated_at'])) { |
||
| 79 | $genre->setUpdatedAt(new \DateTimeImmutable('@' . $genreData['updated_at'])); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->entityManager->persist($genre); |
||
| 83 | $insertedCount++; |
||
| 84 | } |
||
| 85 | |||
| 86 | $io->progressAdvance(); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->entityManager->flush(); |
||
| 90 | $io->progressFinish(); |
||
| 91 | |||
| 92 | $io->success([ |
||
| 93 | "Genres imported successfully!", |
||
| 94 | "Inserted: $insertedCount genres", |
||
| 95 | "Updated: $updatedCount genres", |
||
| 96 | "Skipped: $skippedCount genres (no changes)", |
||
| 97 | "Total processed: " . ($insertedCount + $updatedCount + $skippedCount) . " genres" |
||
| 98 | ]); |
||
| 99 | |||
| 100 | return Command::SUCCESS; |
||
| 101 | } catch (\Exception $e) { |
||
| 102 | $io->error('Failed to import genres: ' . $e->getMessage()); |
||
| 103 | return Command::FAILURE; |
||
| 104 | } |
||
| 107 |