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

ChartFixtures::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
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\Chart;
13
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
14
use VideoGamesRecords\CoreBundle\Entity\ChartType;
15
16
class ChartFixtures extends Fixture implements DependentFixtureInterface
17
{
18
    /**
19
     * @return string[]
20
     */
21
    public function getDependencies(): array
22
    {
23
        return [
24
            GameFixtures::class
25
        ];
26
    }
27
28
    /**
29
     * @var array<string>
30
     */
31
    private array $entities = [
32
        'Group',
33
    ];
34
35
36
    /**
37
     * @var array<mixed>
38
     */
39
    private array $types = [
40
        [
41
            'id'  => 1,
42
            'name'    => 'Score',
43
            'mask'    => '30~',
44
            'order_by' => 'DESC',
45
        ],
46
        [
47
            'id'  => 2,
48
            'name'    => 'Temps',
49
            'mask'    => '30~:|2~.|2~',
50
            'order_by' => 'ASC',
51
        ],
52
        [
53
            'id'  => 3,
54
            'name'    => 'Distance',
55
            'mask'    => '30~ m',
56
            'order_by' => 'DESC',
57
        ],
58
    ];
59
60
61
    /**
62
     * @var array<mixed>
63
     */
64
    private array $charts = [
65
        [
66
            'id'   => 1,
67
            'group_id'   => 1,
68
            'LibChartEn' => 'Baby Park',
69
            'libChartFr' => 'Baby Park',
70
            'types'     => [1],
71
        ],
72
        [
73
            'id'   => 2,
74
            'group_id'   => 1,
75
            'LibChartEn' => 'Bowser\'s Castle',
76
            'libChartFr' => 'Bowser\'s Castle',
77
            'types'     => [1],
78
        ],
79
        [
80
            'id'   => 3,
81
            'group_id'   => 1,
82
            'LibChartEn' => 'Daisy Cruiser',
83
            'libChartFr' => 'Daisy Cruiser',
84
            'types'     => [2],
85
        ],
86
        [
87
            'id'   => 4,
88
            'group_id'   => 1,
89
            'LibChartEn' => 'Dino Dino Jungle',
90
            'libChartFr' => 'Dino Dino Jungle',
91
            'types'     => [3],
92
        ],
93
        [
94
            'id'   => 5,
95
            'group_id'   => 1,
96
            'LibChartEn' => 'DK Mountain',
97
            'libChartFr' => 'DK Mountain',
98
            'types'     => [1],
99
        ],
100
        [
101
            'id'   => 6,
102
            'group_id'   => 1,
103
            'LibChartEn' => 'Dry Dry Desert',
104
            'libChartFr' => 'Dry Dry Desert',
105
            'types'     => [2],
106
        ],
107
        [
108
            'id'   => 7,
109
            'group_id'   => 1,
110
            'LibChartEn' => 'Luigi Circuit',
111
            'libChartFr' => 'Luigi Circuit',
112
            'types'     => [1, 2],
113
        ],
114
        [
115
            'id'   => 8,
116
            'group_id'   => 1,
117
            'LibChartEn' => 'Mario Circuit',
118
            'libChartFr' => 'Mario Circuit',
119
            'types'     => [1, 3],
120
        ],
121
        [
122
            'id'   => 9,
123
            'group_id'   => 1,
124
            'LibChartEn' => 'Mushroom Bridge',
125
            'libChartFr' => 'Mushroom Bridge',
126
            'types'     => [2, 3],
127
        ],
128
        [
129
            'id'   => 10,
130
            'group_id'   => 1,
131
            'LibChartEn' => 'Mushroom City',
132
            'libChartFr' => 'Mushroom City',
133
            'types'     => [1],
134
        ],
135
        [
136
            'id'   => 11,
137
            'group_id'   => 1,
138
            'LibChartEn' => 'Peach Beach',
139
            'libChartFr' => 'Peach Beach',
140
            'types'     => [1],
141
        ],
142
        [
143
            'id'   => 12,
144
            'group_id'   => 1,
145
            'LibChartEn' => 'Rainbow Road',
146
            'libChartFr' => 'Rainbow Road',
147
            'types'     => [1],
148
        ],
149
        [
150
            'id'   => 13,
151
            'group_id'   => 1,
152
            'LibChartEn' => 'Sherbet Land',
153
            'libChartFr' => 'Sherbet Land',
154
            'types'     => [1],
155
        ],
156
        [
157
            'id'   => 14,
158
            'group_id'   => 1,
159
            'LibChartEn' => 'Waluigi Stadium',
160
            'libChartFr' => 'Waluigi Stadium',
161
            'types'     => [1],
162
        ],
163
        [
164
            'id'   => 15,
165
            'group_id'   => 1,
166
            'LibChartEn' => 'Wario Colosseum',
167
            'libChartFr' => 'Wario Colosseum',
168
            'types'     => [3],
169
        ],
170
        [
171
            'id'   => 16,
172
            'group_id'   => 1,
173
            'LibChartEn' => 'Yoshi Circuit',
174
            'libChartFr' => 'Yoshi Circuit',
175
            'types'     => [2],
176
        ],
177
    ];
178
179
    private function updateGeneratorType(ObjectManager $manager): void
180
    {
181
        foreach ($this->entities as $entity) {
182
            $metadata = $manager->getClassMetaData("VideoGamesRecords\\CoreBundle\\Entity\\" . $entity);
183
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
184
        }
185
    }
186
187
    /**
188
     * @throws Exception
189
     */
190
    public function load(ObjectManager $manager): void
191
    {
192
        $this->updateGeneratorType($manager);
193
        $this->loadTypes($manager);
194
        $this->loadCharts($manager);
195
        $manager->flush();
196
    }
197
198
199
    /**
200
     * @param ObjectManager $manager
201
     */
202
    private function loadTypes(ObjectManager $manager): void
203
    {
204
        foreach ($this->types as $row) {
205
            $chartType = new ChartType();
206
            $chartType->setId($row['id']);
207
            $chartType->setName($row['name']);
208
            $chartType->setMask($row['mask']);
209
            $chartType->setOrderBy($row['order_by']);
210
211
            $manager->persist($chartType);
212
            $this->addReference('chart-type.' . $chartType->getId(), $chartType);
213
        }
214
        $manager->flush();
215
    }
216
217
    /**
218
     * @param ObjectManager $manager
219
     */
220
    private function loadCharts(ObjectManager $manager): void
221
    {
222
        foreach ($this->charts as $row) {
223
            $chart = new Chart();
224
            $chart->setId($row['id']);
225
            $chart->setLibChartEn($row['LibChartEn']);
226
            $chart->setLibChartFr($row['libChartFr']);
227
            $chart->setCreatedAt(new \Datetime());
228
            $chart->setUpdatedAt(new \Datetime());
229
            $chart->setGroup($this->getReference('group' . $row['group_id']));
230
231
            foreach ($row['types'] as $type) {
232
                $chartLib = new ChartLib();
233
                $chartLib->setChart($chart);
234
                $chartLib->setType($this->getReference('chart-type.' . $type));
235
                $chartLib->setCreatedAt(new \Datetime());
236
                $chartLib->setUpdatedAt(new \Datetime());
237
                $chartLib->setName('test');
238
239
                $chart->addLib($chartLib);
240
            }
241
242
            $manager->persist($chart);
243
            $this->addReference('chart' . $chart->getId(), $chart);
244
        }
245
    }
246
}
247