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

GroupFixtures   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 34
c 1
b 0
f 0
dl 0
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 5 1
A getDependencies() 0 4 1
A loadGroups() 0 12 2
A updateGeneratorType() 0 5 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\Group;
13
14
class GroupFixtures extends Fixture implements DependentFixtureInterface
15
{
16
    /**
17
     * @return string[]
18
     */
19
    public function getDependencies(): array
20
    {
21
        return [
22
            GameFixtures::class
23
        ];
24
    }
25
26
    /**
27
     * @var array<string>
28
     */
29
    private array $entities = [
30
        'Group',
31
    ];
32
33
34
35
    /**
36
     * @var array<mixed>
37
     */
38
    private array $groups = [
39
        [
40
            'id' => 1,
41
            'game_id' => 11,
42
            'LibGroupEn' => 'Meilleur Tour',
43
            'libGroupFr' => 'Fastest Lap Times',
44
        ],
45
        [
46
            'id'   => 2,
47
            'game_id' => 11,
48
            'LibGroupEn' => 'Meilleur Temps',
49
            'libGroupFr' => 'Fastest Total Times',
50
        ],
51
        [
52
            'id'   => 3,
53
            'game_id' => 11,
54
            'LibGroupEn' => 'Grand Prix',
55
            'libGroupFr' => 'Grand Prix',
56
        ],
57
    ];
58
59
    private function updateGeneratorType(ObjectManager $manager): void
60
    {
61
        foreach ($this->entities as $entity) {
62
            $metadata = $manager->getClassMetaData("VideoGamesRecords\\CoreBundle\\Entity\\" . $entity);
63
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
64
        }
65
    }
66
67
    /**
68
     * @throws Exception
69
     */
70
    public function load(ObjectManager $manager): void
71
    {
72
        $this->updateGeneratorType($manager);
73
        $this->loadGroups($manager);
74
        $manager->flush();
75
    }
76
77
78
    /**
79
     * @param ObjectManager $manager
80
     */
81
    private function loadGroups(ObjectManager $manager): void
82
    {
83
        foreach ($this->groups as $row) {
84
            $group = new Group();
85
            $group->setId($row['id']);
86
            $group->setLibGroupEn($row['LibGroupEn']);
87
            $group->setLibGroupFr($row['libGroupFr']);
88
            $group->setCreatedAt(new \Datetime());
89
            $group->setUpdatedAt(new \Datetime());
90
            $group->setGame($this->getReference('game' . $row['game_id']));
91
            $manager->persist($group);
92
            $this->addReference('group' . $group->getId(), $group);
93
        }
94
    }
95
}
96