ImportGenresCommand::execute()   B
last analyzed

Complexity

Conditions 9
Paths 107

Size

Total Lines 75
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 51
c 1
b 0
f 0
nc 107
nop 2
dl 0
loc 75
rs 7.4668

How to fix   Long Method   

Long Method

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:

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