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