|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VideoGamesRecords\IgdbBundle\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
use VideoGamesRecords\IgdbBundle\Entity\Genre; |
|
14
|
|
|
use VideoGamesRecords\IgdbBundle\Client\IgdbClient; |
|
15
|
|
|
|
|
16
|
|
|
#[AsCommand( |
|
17
|
|
|
name: 'igdb:import:genres', |
|
18
|
|
|
description: 'Import all genres from IGDB API into database' |
|
19
|
|
|
)] |
|
20
|
|
|
class ImportGenresCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct( |
|
23
|
|
|
private IgdbClient $igdbClient, |
|
24
|
|
|
private EntityManagerInterface $entityManager |
|
25
|
|
|
) { |
|
26
|
|
|
parent::__construct(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
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
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|