| Conditions | 14 |
| Paths | 244 |
| Total Lines | 88 |
| Code Lines | 56 |
| 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 |
||
| 53 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 54 | { |
||
| 55 | $io = new SymfonyStyle($input, $output); |
||
| 56 | |||
| 57 | $gameName = $input->getArgument('name'); |
||
| 58 | $platformIds = $input->getOption('platform'); |
||
| 59 | $limit = (int) $input->getOption('limit'); |
||
| 60 | $exactMatch = $input->getOption('exact-match'); |
||
| 61 | $dryRun = $input->getOption('dry-run'); |
||
| 62 | |||
| 63 | $io->title("Searching for games matching: '$gameName'"); |
||
| 64 | |||
| 65 | if (!empty($platformIds)) { |
||
| 66 | $platformIds = array_map('intval', $platformIds); |
||
| 67 | $io->info('Filtering by platform IDs: ' . implode(', ', $platformIds)); |
||
| 68 | } |
||
| 69 | |||
| 70 | try { |
||
| 71 | $games = $this->igdbClient->searchGamesByName($gameName, $platformIds, $limit); |
||
| 72 | |||
| 73 | if (empty($games)) { |
||
| 74 | $io->warning('No games found matching your criteria.'); |
||
| 75 | return Command::SUCCESS; |
||
| 76 | } |
||
| 77 | |||
| 78 | $io->info(sprintf('Found %d game(s) in IGDB', count($games))); |
||
| 79 | |||
| 80 | $toImport = []; |
||
| 81 | foreach ($games as $gameData) { |
||
| 82 | // Skip if exact match required and names don't match exactly |
||
| 83 | if ($exactMatch && strcasecmp($gameData['name'], $gameName) !== 0) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Check if game already exists in our database |
||
| 88 | $existingGame = $this->entityManager->getRepository(Game::class)->find($gameData['id']); |
||
| 89 | if ($existingGame) { |
||
| 90 | $io->note(sprintf( |
||
| 91 | 'Game "%s" (ID: %d) already exists in database', |
||
| 92 | $gameData['name'], |
||
| 93 | $gameData['id'] |
||
| 94 | )); |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $toImport[] = $gameData; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (empty($toImport)) { |
||
| 102 | $io->success('All matching games are already in your database.'); |
||
| 103 | return Command::SUCCESS; |
||
| 104 | } |
||
| 105 | |||
| 106 | $io->section(sprintf('Games to import (%d):', count($toImport))); |
||
| 107 | $table = $io->createTable(); |
||
| 108 | $table->setHeaders(['ID', 'Name', 'Platforms', 'First Release Date']); |
||
| 109 | |||
| 110 | foreach ($toImport as $gameData) { |
||
| 111 | $platforms = $this->getPlatformNames($gameData['platforms'] ?? []); |
||
| 112 | $releaseDate = isset($gameData['first_release_date']) && is_numeric($gameData['first_release_date']) |
||
| 113 | ? date('Y-m-d', $gameData['first_release_date']) |
||
| 114 | : 'N/A'; |
||
| 115 | |||
| 116 | $table->addRow([ |
||
| 117 | $gameData['id'], |
||
| 118 | $gameData['name'], |
||
| 119 | implode(', ', $platforms), |
||
| 120 | $releaseDate |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | $table->render(); |
||
| 124 | |||
| 125 | if ($dryRun) { |
||
| 126 | $io->note('Dry run mode - no games were imported.'); |
||
| 127 | return Command::SUCCESS; |
||
| 128 | } |
||
| 129 | |||
| 130 | if (!$io->confirm('Do you want to import these games?', false)) { |
||
| 131 | $io->info('Import cancelled.'); |
||
| 132 | return Command::SUCCESS; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->importGames($toImport, $io); |
||
| 136 | |||
| 137 | return Command::SUCCESS; |
||
| 138 | } catch (\Exception $e) { |
||
| 139 | $io->error('Failed to search and import games: ' . $e->getMessage()); |
||
| 140 | return Command::FAILURE; |
||
| 141 | } |
||
| 251 |