Passed
Push — develop ( 5f4a50...d4ae8f )
by BENARD
05:10
created

LoadFixtures::loadGames()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 77
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 77
rs 8.1357
cc 7
nc 9
nop 1

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
namespace VideoGamesRecords\CoreBundle\DataFixtures\ORM;
4
5
use DateTime;
6
use Doctrine\Bundle\FixturesBundle\Fixture;
7
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\Persistence\ObjectManager;
10
use Exception;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
13
use VideoGamesRecords\CoreBundle\Entity\Chart;
14
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
15
use VideoGamesRecords\CoreBundle\Entity\ChartType;
16
use VideoGamesRecords\CoreBundle\Entity\Game;
17
use VideoGamesRecords\CoreBundle\Entity\Group;
18
use VideoGamesRecords\CoreBundle\Entity\Platform;
19
use VideoGamesRecords\CoreBundle\Entity\Player;
20
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
21
use VideoGamesRecords\CoreBundle\Entity\PlayerChartLib;
22
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
23
use VideoGamesRecords\CoreBundle\Entity\PlayerStatus;
24
use VideoGamesRecords\CoreBundle\Entity\Serie;
25
use VideoGamesRecords\CoreBundle\ValueObject\GameStatus;
26
27
/**
28
 * Defines the sample data to load in the database when running the unit and
29
 * functional tests. Execute this command to load the data:
30
 *
31
 *   $ php app/console doctrine:fixtures:load
32
 *
33
 * See http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
34
 *
35
 * @author David Benard <[email protected]>
36
 */
37
class LoadFixtures extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
38
{
39
    use ContainerAwareTrait;
40
41
    /**
42
     * @param ObjectManager $manager
43
     * @throws Exception
44
     */
45
    public function load(ObjectManager $manager): void
46
    {
47
        $this->loadPlayerChartStatus($manager);
48
        $this->loadPlayerStatus($manager);
49
        $this->loadPlayers($manager);
50
        $this->loadChartType($manager);
51
        $this->loadSeries($manager);
52
        $this->loadPlatforms($manager);
53
        $this->loadGames($manager);
54
        $this->loadGroups($manager);
55
        $this->loadCharts($manager);
56
        $this->loadPlayerChart($manager);
57
    }
58
59
    /**
60
     * @param ObjectManager $manager
61
     */
62
    private function loadSeries(ObjectManager $manager): void
63
    {
64
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Serie');
65
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
66
        $list = [
67
            [
68
                'id'        => 1,
69
                'libSerie' => 'Forza Motosport',
70
            ],
71
            [
72
                'id'        => 2,
73
                'libSerie' => 'Mario Kart',
74
            ],
75
        ];
76
77
        foreach ($list as $row) {
78
            $serie = new Serie();
79
            $serie->setId($row['id']);
80
            $serie->setLibSerie($row['libSerie']);
81
            $manager->persist($serie);
82
            $this->addReference('serie.' . $serie->getId(), $serie);
83
        }
84
        $manager->flush();
85
    }
86
87
    /**
88
     * @param ObjectManager $manager
89
     */
90
    private function loadPlatforms(ObjectManager $manager): void
91
    {
92
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Platform');
93
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
94
        $list = [
95
            [
96
                'idPlatform'  => 1,
97
                'libPlatform' => 'Game Cube',
98
            ],
99
            [
100
                'idPlatform'  => 2,
101
                'libPlatform' => 'Playstation 2',
102
            ],
103
            [
104
                'idPlatform'  => 3,
105
                'libPlatform' => 'Xbox',
106
            ],
107
        ];
108
        foreach ($list as $row) {
109
            $platform = new Platform();
110
            $platform->setId($row['idPlatform']);
111
            $platform->setLibPlatform($row['libPlatform']);
112
            $manager->persist($platform);
113
            $this->addReference('platform.' . $platform->getId(), $platform);
114
        }
115
        $manager->flush();
116
    }
117
118
    /**
119
     * @param ObjectManager $manager
120
     */
121
    private function loadGames(ObjectManager $manager): void
122
    {
123
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Game');
124
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
125
        $list = [
126
            [
127
                'idGame'    => 1,
128
                'LibGameEn' => 'Burnout 2',
129
                'libGameFr' => 'Burnout 2',
130
                'platforms' => [1, 2, 3],
131
                'status'    => GameStatus::STATUS_ACTIVE,
132
            ],
133
            [
134
                'idGame'    => 2,
135
                'LibGameEn' => 'Mario Kart 8',
136
                'libGameFr' => 'Mario Kart 8',
137
                'idSerie'   => 2,
138
            ],
139
            [
140
                'idGame'    => 3,
141
                'LibGameEn' => 'Forza Motosport 4',
142
                'libGameFr' => 'Forza Motosport 4',
143
                'idSerie'   => 1,
144
            ],
145
            [
146
                'idGame'    => 4,
147
                'LibGameEn' => 'Forza Motosport 3',
148
                'libGameFr' => 'Forza Motosport 3',
149
                'idSerie'   => 1,
150
            ],
151
            [
152
                'idGame'    => 5,
153
                'LibGameEn' => 'Sega Rallye [EN]',
154
                'libGameFr' => 'Sega Rallye [FR]',
155
            ],
156
            [
157
                'idGame'    => 6,
158
                'LibGameEn' => 'Gran Turismo',
159
                'libGameFr' => 'Gran Turismo',
160
                'platforms' => [2],
161
            ],
162
            [
163
                'idGame'    => 7,
164
                'LibGameEn' => 'Jet Set Radio',
165
                'libGameFr' => 'Jet Set Radio',
166
            ],
167
            [
168
                'idGame'    => 11,
169
                'LibGameEn' => 'Mario Kart Double Dash',
170
                'libGameFr' => 'Mario Kart Double Dash',
171
                'idSerie'   => 2,
172
                'platforms' => [1],
173
                'status'    => GameStatus::STATUS_ACTIVE,
174
            ],
175
        ];
176
177
        foreach ($list as $row) {
178
            $game = new Game();
179
            $game->setId($row['idGame']);
180
            $game->setLibGameEn($row['LibGameEn']);
181
            $game->setLibGameFr($row['libGameFr']);
182
183
            if (isset($row['idSerie'])) {
184
                $game->setSerie($this->getReference('serie.' . $row['idSerie']));
185
            }
186
            if (isset($row['platforms'])) {
187
                foreach ($row['platforms'] as $id) {
188
                    $game->addPlatform($this->getReference('platform.' . $id));
189
                }
190
            }
191
            if (isset($row['status']) && GameStatus::STATUS_ACTIVE === $row['status']) {
192
                $game->setStatus(GameStatus::STATUS_ACTIVE);
193
            }
194
            $manager->persist($game);
195
            $this->addReference('game' . $game->getId(), $game);
196
        }
197
        $manager->flush();
198
    }
199
200
    /**
201
     * @param ObjectManager $manager
202
     */
203
    private function loadGroups(ObjectManager $manager): void
204
    {
205
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Group');
206
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
207
208
        $list = [
209
            [
210
                'idGroup'   => 1,
211
                'idGame'    => 11,
212
                'LibGroupEn' => 'Meilleur Tour',
213
                'libGroupFr' => 'Fastest Lap Times',
214
            ],
215
            [
216
                'idGroup'   => 2,
217
                'idGame'    => 11,
218
                'LibGroupEn' => 'Meilleur Temps',
219
                'libGroupFr' => 'Fastest Total Times',
220
            ],
221
            [
222
                'idGroup'   => 3,
223
                'idGame'    => 11,
224
                'LibGroupEn' => 'Grand Prix',
225
                'libGroupFr' => 'Grand Prix',
226
            ],
227
        ];
228
229
        foreach ($list as $row) {
230
            $group = new Group();
231
            $group->setId($row['idGroup']);
232
            $group->setLibGroupEn($row['LibGroupEn']);
233
            $group->setLibGroupFr($row['libGroupFr']);
234
            $group->setGame($this->getReference('game' . $row['idGame']));
235
            $manager->persist($group);
236
            $this->addReference('group' . $group->getId(), $group);
237
        }
238
239
        $manager->flush();
240
    }
241
242
    /**
243
     * @param ObjectManager $manager
244
     */
245
    private function loadChartType(ObjectManager $manager): void
246
    {
247
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\ChartType');
248
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
249
250
        $list = [
251
            [
252
                'idType'  => 1,
253
                'name'    => 'Score',
254
                'mask'    => '30~',
255
                'orderBy' => 'DESC',
256
            ],
257
            [
258
                'idType'  => 2,
259
                'name'    => 'Temps',
260
                'mask'    => '30~:|2~.|2~',
261
                'orderBy' => 'ASC',
262
            ],
263
            [
264
                'idType'  => 3,
265
                'name'    => 'Distance',
266
                'mask'    => '30~ m',
267
                'orderBy' => 'DESC',
268
            ],
269
        ];
270
271
        foreach ($list as $row) {
272
            $chartType = new ChartType();
273
            $chartType
274
                ->setIdType($row['idType'])
275
                ->setName($row['name'])
276
                ->setMask($row['mask'])
277
                ->setOrderBy($row['orderBy']);
278
279
            $manager->persist($chartType);
280
            $this->addReference('charttype.' . $chartType->getIdType(), $chartType);
281
        }
282
        $manager->flush();
283
    }
284
285
    /**
286
     * @param ObjectManager $manager
287
     */
288
    private function loadCharts(ObjectManager $manager): void
289
    {
290
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Chart');
291
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
292
293
        $list = [
294
            [
295
                'idChart'   => 1,
296
                'idGroup'   => 1,
297
                'LibChartEn' => 'Baby Park',
298
                'libChartFr' => 'Baby Park',
299
                'types'     => [1],
300
            ],
301
            [
302
                'idChart'   => 2,
303
                'idGroup'   => 1,
304
                'LibChartEn' => 'Bowser\'s Castle',
305
                'libChartFr' => 'Bowser\'s Castle',
306
                'types'     => [1],
307
            ],
308
            [
309
                'idChart'   => 3,
310
                'idGroup'   => 1,
311
                'LibChartEn' => 'Daisy Cruiser',
312
                'libChartFr' => 'Daisy Cruiser',
313
                'types'     => [2],
314
            ],
315
            [
316
                'idChart'   => 4,
317
                'idGroup'   => 1,
318
                'LibChartEn' => 'Dino Dino Jungle',
319
                'libChartFr' => 'Dino Dino Jungle',
320
                'types'     => [3],
321
            ],
322
            [
323
                'idChart'   => 5,
324
                'idGroup'   => 1,
325
                'LibChartEn' => 'DK Mountain',
326
                'libChartFr' => 'DK Mountain',
327
                'types'     => [1],
328
            ],
329
            [
330
                'idChart'   => 6,
331
                'idGroup'   => 1,
332
                'LibChartEn' => 'Dry Dry Desert',
333
                'libChartFr' => 'Dry Dry Desert',
334
                'types'     => [2],
335
            ],
336
            [
337
                'idChart'   => 7,
338
                'idGroup'   => 1,
339
                'LibChartEn' => 'Luigi Circuit',
340
                'libChartFr' => 'Luigi Circuit',
341
                'types'     => [1, 2],
342
            ],
343
            [
344
                'idChart'   => 8,
345
                'idGroup'   => 1,
346
                'LibChartEn' => 'Mario Circuit',
347
                'libChartFr' => 'Mario Circuit',
348
                'types'     => [1, 3],
349
            ],
350
            [
351
                'idChart'   => 9,
352
                'idGroup'   => 1,
353
                'LibChartEn' => 'Mushroom Bridge',
354
                'libChartFr' => 'Mushroom Bridge',
355
                'types'     => [2, 3],
356
            ],
357
            [
358
                'idChart'   => 10,
359
                'idGroup'   => 1,
360
                'LibChartEn' => 'Mushroom City',
361
                'libChartFr' => 'Mushroom City',
362
                'types'     => [1],
363
            ],
364
            [
365
                'idChart'   => 11,
366
                'idGroup'   => 1,
367
                'LibChartEn' => 'Peach Beach',
368
                'libChartFr' => 'Peach Beach',
369
                'types'     => [1],
370
            ],
371
            [
372
                'idChart'   => 12,
373
                'idGroup'   => 1,
374
                'LibChartEn' => 'Rainbow Road',
375
                'libChartFr' => 'Rainbow Road',
376
                'types'     => [1],
377
            ],
378
            [
379
                'idChart'   => 13,
380
                'idGroup'   => 1,
381
                'LibChartEn' => 'Sherbet Land',
382
                'libChartFr' => 'Sherbet Land',
383
                'types'     => [1],
384
            ],
385
            [
386
                'idChart'   => 14,
387
                'idGroup'   => 1,
388
                'LibChartEn' => 'Waluigi Stadium',
389
                'libChartFr' => 'Waluigi Stadium',
390
                'types'     => [1],
391
            ],
392
            [
393
                'idChart'   => 15,
394
                'idGroup'   => 1,
395
                'LibChartEn' => 'Wario Colosseum',
396
                'libChartFr' => 'Wario Colosseum',
397
                'types'     => [3],
398
            ],
399
            [
400
                'idChart'   => 16,
401
                'idGroup'   => 1,
402
                'LibChartEn' => 'Yoshi Circuit',
403
                'libChartFr' => 'Yoshi Circuit',
404
                'types'     => [2],
405
            ],
406
        ];
407
408
        foreach ($list as $row) {
409
            $chart = new Chart();
410
            $chart->setId($row['idChart']);
411
            $chart->setLibChartEn($row['LibChartEn']);
412
            $chart->setLibChartFr($row['libChartFr']);
413
            $chart->setGroup($this->getReference('group' . $row['idGroup']));
414
415
            foreach ($row['types'] as $type) {
416
                $chartLib = new ChartLib();
417
                $chartLib
418
                    ->setChart($chart)
419
                    ->setType($this->getReference('charttype.' . $type))
420
                    ->setName('test');
421
422
                $chart->addLib($chartLib);
423
            }
424
425
            $manager->persist($chart);
426
            $this->addReference('chart' . $chart->getId(), $chart);
427
        }
428
429
        $manager->flush();
430
    }
431
432
    /**
433
     * @param $manager
434
     */
435
    public function loadPlayerStatus($manager): void
436
    {
437
        $list = [
438
            [
439
                'class'     => 'normal',
440
            ],
441
        ];
442
443
        foreach ($list as $key => $row) {
444
            $playerStatus = new PlayerStatus();
445
            $playerStatus
446
                ->setId($key + 1)
447
                ->setClass($row['class']);
448
449
            $manager->persist($playerStatus);
450
            $this->addReference('playerStatus' . $playerStatus->getId(), $playerStatus);
451
        }
452
        $manager->flush();
453
    }
454
455
456
    /**
457
     * @param ObjectManager $manager
458
     */
459
    private function loadPlayers(ObjectManager $manager): void
460
    {
461
        $metadata = $manager->getClassMetaData('VideoGamesRecords\CoreBundle\Entity\Player');
462
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
463
464
        $list = [
465
            [
466
                'idPlayer' => 1,
467
                'pseudo'   => 'magicbart',
468
            ],
469
            [
470
                'idPlayer' => 2,
471
                'pseudo'   => 'kloh',
472
            ],
473
            [
474
                'idPlayer' => 3,
475
                'pseudo'   => 'flatine',
476
            ],
477
        ];
478
479
        foreach ($list as $row) {
480
            $player = new Player();
481
            $player
482
                ->setId($row['idPlayer'])
483
                ->setUserId($row['idPlayer'])
484
                ->setPseudo($row['pseudo'])
485
                ->setStatus($this->getReference('playerStatus1'))
486
                ->setCreatedAt(new \Datetime())
487
                ->setUpdatedAt(new \Datetime());
488
489
            $manager->persist($player);
490
            $this->addReference('player' . $player->getId(), $player);
491
        }
492
        $manager->flush();
493
    }
494
495
    /**
496
     * @param $manager
497
     */
498
    public function loadPlayerChartStatus($manager): void
499
    {
500
        $list = [
501
            [
502
                'libStatus' => 'NORMAL',
503
                'class'     => 'proof--none',
504
                'ranking'   => true,
505
                'proof'     => false,
506
            ],
507
            [
508
                'libStatus' => 'DEMAND',
509
                'class'     => 'proof--request-pending',
510
                'ranking'   => true,
511
                'proof'     => false,
512
            ],
513
            [
514
                'libStatus' => 'INVESTIGATION',
515
                'class'     => 'proof--request-validated',
516
                'ranking'   => false,
517
                'proof'     => false,
518
            ],
519
            [
520
                'libStatus' => 'DEMAND_SEND_PROOF',
521
                'class'     => 'proof--request-sent',
522
                'ranking'   => true,
523
                'proof'     => true,
524
            ],
525
            [
526
                'libStatus' => 'NORMAL_SEND_PROOF',
527
                'class'     => 'proof--sent',
528
                'ranking'   => true,
529
                'proof'     => true,
530
            ],
531
            [
532
                'libStatus' => 'PROOVED',
533
                'class'     => 'proof--proved',
534
                'ranking'   => true,
535
                'proof'     => true,
536
            ],
537
            [
538
                'libStatus' => 'NOT_PROOVED',
539
                'class'     => 'proof--unproved',
540
                'ranking'   => true,
541
                'proof'     => false,
542
            ],
543
        ];
544
545
        foreach ($list as $key => $row) {
546
            $playerChartStatus = new PlayerChartStatus();
547
            $playerChartStatus
548
                ->setId($key + 1)
549
                ->setName($row['libStatus'])
550
                ->setClass($row['class'])
551
                ->setBoolRanking($row['ranking'])
552
                ->setBoolSendProof($row['proof']);
553
554
            $manager->persist($playerChartStatus);
555
            $this->addReference('playerchartstatus' . $playerChartStatus->getId(), $playerChartStatus);
556
        }
557
        $manager->flush();
558
    }
559
560
561
    /**
562
     * @param ObjectManager $manager
563
     * @throws Exception
564
     */
565
    private function loadPlayerChart(ObjectManager $manager): void
566
    {
567
        /** @var Chart $chart */
568
        $chart = $this->getReference('chart1');
569
570
        $list = [
571
            [
572
                'idPlayer' => 1,
573
                'value'    => 9999,
574
                'status'   => 1,
575
            ],
576
            [
577
                'idPlayer' => 2,
578
                'value'    => 10101,
579
                'status'   => 6,
580
            ],
581
            [
582
                'idPlayer' => 3,
583
                'value'    => 10101,
584
                'status'   => 2,
585
            ],
586
        ];
587
588
        foreach ($list as $row) {
589
            $playerChart = new PlayerChart();
590
            $playerChart->setPlayer($this->getReference('player' . $row['idPlayer']));
591
            $playerChart->setChart($chart);
592
            $playerChart->setStatus($this->getReference(sprintf('playerchartstatus%d', $row['status'])));
593
            $playerChart->setLastUpdate(new DateTime());
594
            $manager->persist($playerChart);
595
596
            foreach ($chart->getLibs() as $lib) {
597
                $playerChartLib = new PlayerChartLib();
598
                $playerChartLib->setPlayerChart($playerChart);
599
                $playerChartLib->setLibChart($lib);
600
                $playerChartLib->setValue($row['value']);
601
                $manager->persist($playerChartLib);
602
            }
603
        }
604
        $chart->setNbPost(count($list));
605
606
        $manager->flush();
607
    }
608
609
    /**
610
     * Get the order of this fixture
611
     *
612
     * @return integer
613
     */
614
    public function getOrder(): int
615
    {
616
        return 50;
617
    }
618
}
619