| Total Complexity | 45 |
| Total Lines | 229 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SearchAndImportGamesCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchAndImportGamesCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | #[AsCommand( |
||
| 21 | name: 'igdb:search-import:games', |
||
| 22 | description: 'Search and import specific games from IGDB API by name and platform' |
||
| 23 | )] |
||
| 24 | class SearchAndImportGamesCommand extends Command |
||
| 25 | { |
||
| 26 | public function __construct( |
||
| 27 | private readonly IgdbClient $igdbClient, |
||
| 28 | private readonly EntityManagerInterface $entityManager |
||
| 29 | ) { |
||
| 30 | parent::__construct(); |
||
| 31 | } |
||
| 32 | |||
| 33 | protected function configure(): void |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | private function importGames(array $games, SymfonyStyle $io): void |
||
| 167 | } |
||
| 168 | |||
| 169 | private function updateGame(Game $game, array $gameData): void |
||
| 170 | { |
||
| 171 | $game->setName($gameData['name']); |
||
| 172 | $game->setSlug($gameData['slug'] ?? null); |
||
| 173 | $game->setStoryline($gameData['storyline'] ?? null); |
||
| 174 | $game->setSummary($gameData['summary'] ?? null); |
||
| 175 | $game->setUrl($gameData['url'] ?? null); |
||
| 176 | $game->setChecksum($gameData['checksum'] ?? null); |
||
| 177 | $game->setFirstReleaseDate($gameData['first_release_date'] ?? null); |
||
| 178 | |||
| 179 | // Gérer l'association avec version_parent (auto-référence) |
||
| 180 | if (isset($gameData['version_parent']) && is_numeric($gameData['version_parent'])) { |
||
| 181 | $versionParent = $this->entityManager->getRepository(Game::class)->find($gameData['version_parent']); |
||
| 182 | $game->setVersionParent($versionParent); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Gérer les associations ManyToMany avec les genres |
||
| 186 | if (isset($gameData['genres']) && is_array($gameData['genres'])) { |
||
| 187 | foreach ($gameData['genres'] as $genreId) { |
||
| 188 | $genre = $this->entityManager->getRepository(Genre::class)->find($genreId); |
||
| 189 | if ($genre) { |
||
| 190 | $game->addGenre($genre); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // Gérer les associations ManyToMany avec les plateformes |
||
| 196 | if (isset($gameData['platforms']) && is_array($gameData['platforms'])) { |
||
| 197 | foreach ($gameData['platforms'] as $platformData) { |
||
| 198 | $platformId = null; |
||
| 199 | if (is_object($platformData)) { |
||
| 200 | $platformArray = (array) $platformData; |
||
| 201 | $platformId = $platformArray['id'] ?? null; |
||
| 202 | } elseif (is_array($platformData)) { |
||
| 203 | $platformId = $platformData['id'] ?? null; |
||
| 204 | } elseif (is_numeric($platformData)) { |
||
| 205 | $platformId = $platformData; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($platformId) { |
||
| 209 | $platform = $this->entityManager->getRepository(Platform::class)->find($platformId); |
||
| 210 | if ($platform) { |
||
| 211 | $game->addPlatform($platform); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if (isset($gameData['updated_at'])) { |
||
| 218 | $game->setUpdatedAt(new \DateTimeImmutable('@' . $gameData['updated_at'])); |
||
| 219 | } else { |
||
| 220 | $game->setUpdatedAt(new \DateTimeImmutable()); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | private function getPlatformNames(array $platforms): array |
||
| 249 | } |
||
| 250 | } |
||
| 251 |