|
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\Platform; |
|
14
|
|
|
use VideoGamesRecords\IgdbBundle\Entity\PlatformType; |
|
15
|
|
|
use VideoGamesRecords\IgdbBundle\Entity\PlatformLogo; |
|
16
|
|
|
use VideoGamesRecords\IgdbBundle\Client\IgdbClient; |
|
17
|
|
|
|
|
18
|
|
|
#[AsCommand( |
|
19
|
|
|
name: 'igdb:import:platforms', |
|
20
|
|
|
description: 'Import all platforms from IGDB API into database' |
|
21
|
|
|
)] |
|
22
|
|
|
class ImportPlatformsCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
private IgdbClient $igdbClient, |
|
26
|
|
|
private EntityManagerInterface $entityManager |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
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
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function updatePlatform(Platform $platform, array $platformData): void |
|
97
|
|
|
{ |
|
98
|
|
|
$platform->setName($platformData['name']); |
|
99
|
|
|
$platform->setAbbreviation($platformData['abbreviation'] ?? null); |
|
100
|
|
|
$platform->setAlternativeName($platformData['alternative_name'] ?? null); |
|
101
|
|
|
$platform->setGeneration($platformData['generation'] ?? null); |
|
102
|
|
|
$platform->setSlug($platformData['slug'] ?? null); |
|
103
|
|
|
$platform->setSummary($platformData['summary'] ?? null); |
|
104
|
|
|
$platform->setUrl($platformData['url'] ?? null); |
|
105
|
|
|
$platform->setChecksum($platformData['checksum'] ?? null); |
|
106
|
|
|
|
|
107
|
|
|
// Gérer l'association avec PlatformLogo |
|
108
|
|
|
if (isset($platformData['platform_logo']) && is_numeric($platformData['platform_logo'])) { |
|
109
|
|
|
$platformLogo = $this->entityManager |
|
110
|
|
|
->getRepository(PlatformLogo::class) |
|
111
|
|
|
->find($platformData['platform_logo']); |
|
112
|
|
|
$platform->setPlatformLogo($platformLogo); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Gérer l'association avec PlatformType |
|
116
|
|
|
if (isset($platformData['platform_type']) && is_numeric($platformData['platform_type'])) { |
|
117
|
|
|
$platformType = $this->entityManager |
|
118
|
|
|
->getRepository(PlatformType::class) |
|
119
|
|
|
->find($platformData['platform_type']); |
|
120
|
|
|
$platform->setPlatformType($platformType); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (isset($platformData['updated_at'])) { |
|
124
|
|
|
$platform->setUpdatedAt(new \DateTimeImmutable('@' . $platformData['updated_at'])); |
|
125
|
|
|
} else { |
|
126
|
|
|
$platform->setUpdatedAt(new \DateTimeImmutable()); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|