Passed
Pull Request — develop (#95)
by BENARD
13:04 queued 39s
created

GameFixtures   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 88
c 1
b 0
f 0
dl 0
loc 168
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateGeneratorType() 0 5 2
A getDependencies() 0 4 1
A loadGames() 0 21 4
A load() 0 6 1
A loadPlatforms() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\DataFixtures;
6
7
use Doctrine\Bundle\FixturesBundle\Fixture;
8
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
9
use Doctrine\ORM\Mapping\ClassMetadataInfo;
10
use Doctrine\Persistence\ObjectManager;
11
use Exception;
12
use VideoGamesRecords\CoreBundle\Entity\Game;
13
use VideoGamesRecords\CoreBundle\Entity\Platform;
14
use VideoGamesRecords\CoreBundle\ValueObject\GameStatus;
15
16
class GameFixtures extends Fixture implements DependentFixtureInterface
17
{
18
    /**
19
     * @return string[]
20
     */
21
    public function getDependencies(): array
22
    {
23
        return [
24
            SerieFixtures::class
25
        ];
26
    }
27
28
    /**
29
     * @var array<string>
30
     */
31
    private array $entities = [
32
        'Platform', 'Game',
33
    ];
34
35
36
    /**
37
     * @var array<mixed>
38
     */
39
    private array $platforms = [
40
        [
41
            'id'  => 1,
42
            'libPlatform' => 'Game Cube',
43
        ],
44
        [
45
            'id'  => 2,
46
            'libPlatform' => 'Playstation 2',
47
        ],
48
        [
49
            'id'  => 3,
50
            'libPlatform' => 'Xbox',
51
        ],
52
    ];
53
54
    /**
55
     * @var array<mixed>
56
     */
57
    private array $games = [
58
        [
59
            'id'    => 1,
60
            'status'    => GameStatus::ACTIVE,
61
            'LibGameEn' => 'Burnout 2',
62
            'libGameFr' => 'Burnout 2',
63
            'platforms' => [1, 2, 3],
64
            'serie_id'   => null,
65
        ],
66
        [
67
            'id'    => 2,
68
            'status'    => GameStatus::ACTIVE,
69
            'LibGameEn' => 'Mario Kart 8',
70
            'libGameFr' => 'Mario Kart 8',
71
            'platforms' => [],
72
            'serie_id'   => 2,
73
        ],
74
        [
75
            'id'    => 3,
76
            'status'    => GameStatus::ACTIVE,
77
            'LibGameEn' => 'Forza Motosport 4',
78
            'libGameFr' => 'Forza Motosport 4',
79
            'platforms' => [],
80
            'serie_id'   => 1,
81
        ],
82
        [
83
            'id'    => 4,
84
            'status'    => GameStatus::ACTIVE,
85
            'LibGameEn' => 'Forza Motosport 3',
86
            'libGameFr' => 'Forza Motosport 3',
87
            'platforms' => [],
88
            'serie_id'   => 1,
89
        ],
90
        [
91
            'id'    => 5,
92
            'status'    => GameStatus::ACTIVE,
93
            'LibGameEn' => 'Sega Rallye [EN]',
94
            'libGameFr' => 'Sega Rallye [FR]',
95
            'platforms' => [],
96
            'serie_id'   => null,
97
        ],
98
        [
99
            'id'    => 6,
100
            'status'    => GameStatus::ACTIVE,
101
            'LibGameEn' => 'Gran Turismo',
102
            'libGameFr' => 'Gran Turismo',
103
            'platforms' => [2],
104
            'serie_id'   => null,
105
        ],
106
        [
107
            'id'    => 7,
108
            'status'    => GameStatus::ACTIVE,
109
            'LibGameEn' => 'Jet Set Radio',
110
            'libGameFr' => 'Jet Set Radio',
111
            'platforms' => [],
112
            'serie_id'   => null,
113
        ],
114
        [
115
            'id'    => 11,
116
            'status'    => GameStatus::ACTIVE,
117
            'LibGameEn' => 'Mario Kart Double Dash',
118
            'libGameFr' => 'Mario Kart Double Dash',
119
            'platforms' => [1],
120
            'serie_id'   => 2,
121
        ],
122
    ];
123
124
125
    private function updateGeneratorType(ObjectManager $manager): void
126
    {
127
        foreach ($this->entities as $entity) {
128
            $metadata = $manager->getClassMetaData("VideoGamesRecords\\CoreBundle\\Entity\\" . $entity);
129
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
130
        }
131
    }
132
133
    /**
134
     * @throws Exception
135
     */
136
    public function load(ObjectManager $manager): void
137
    {
138
        $this->updateGeneratorType($manager);
139
        $this->loadPlatforms($manager);
140
        $this->loadGames($manager);
141
        $manager->flush();
142
    }
143
144
145
    /**
146
     * @param ObjectManager $manager
147
     */
148
    private function loadPlatforms(ObjectManager $manager): void
149
    {
150
        foreach ($this->platforms as $row) {
151
            $platform = new Platform();
152
            $platform->setId($row['id']);
153
            $platform->setLibPlatform($row['libPlatform']);
154
            $manager->persist($platform);
155
            $this->addReference('platform.' . $platform->getId(), $platform);
156
        }
157
    }
158
159
160
    /**
161
     * @param ObjectManager $manager
162
     */
163
    private function loadGames(ObjectManager $manager): void
164
    {
165
        foreach ($this->games as $row) {
166
            $game = new Game();
167
            $game->setId($row['id']);
168
            $game->setStatus($row['status']);
169
            $game->setLibGameEn($row['LibGameEn']);
170
            $game->setLibGameFr($row['libGameFr']);
171
            $game->setCreatedAt(new \Datetime());
172
            $game->setUpdatedAt(new \Datetime());
173
174
            if (null !== $row['serie_id']) {
175
                $game->setSerie($this->getReference('serie.' . $row['serie_id']));
176
            }
177
178
            foreach ($row['platforms'] as $id) {
179
                $game->addPlatform($this->getReference('platform.' . $id));
180
            }
181
182
            $manager->persist($game);
183
            $this->addReference('game' . $game->getId(), $game);
184
        }
185
    }
186
}
187