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

PlayerChartFixtures::loadStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 12
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\DataFixtures;
6
7
use Datetime;
8
use Doctrine\Bundle\FixturesBundle\Fixture;
9
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
10
use Doctrine\ORM\Mapping\ClassMetadataInfo;
11
use Doctrine\Persistence\ObjectManager;
12
use Exception;
13
use VideoGamesRecords\CoreBundle\Entity\Chart;
14
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
15
use VideoGamesRecords\CoreBundle\Entity\PlayerChartLib;
16
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
17
18
class PlayerChartFixtures extends Fixture implements DependentFixtureInterface
19
{
20
    /**
21
     * @return string[]
22
     */
23
    public function getDependencies(): array
24
    {
25
        return [
26
            ChartFixtures::class
27
        ];
28
    }
29
30
    /**
31
     * @var array<string>
32
     */
33
    private array $entities = [
34
        'PlayerChartStatus', 'PlayerChart'
35
    ];
36
37
38
    /**
39
     * @var array<mixed>
40
     */
41
    private array $status = [
42
        [
43
            'id' => 1,
44
            'libStatus' => 'NORMAL',
45
            'class'     => 'proof--none',
46
            'ranking'   => true,
47
            'proof'     => false,
48
        ],
49
        [
50
            'id' => 2,
51
            'libStatus' => 'DEMAND',
52
            'class'     => 'proof--request-pending',
53
            'ranking'   => true,
54
            'proof'     => false,
55
        ],
56
        [
57
            'id' => 3,
58
            'libStatus' => 'INVESTIGATION',
59
            'class'     => 'proof--request-validated',
60
            'ranking'   => false,
61
            'proof'     => false,
62
        ],
63
        [
64
            'id' => 4,
65
            'libStatus' => 'DEMAND_SEND_PROOF',
66
            'class'     => 'proof--request-sent',
67
            'ranking'   => true,
68
            'proof'     => true,
69
        ],
70
        [
71
            'id' => 5,
72
            'libStatus' => 'NORMAL_SEND_PROOF',
73
            'class'     => 'proof--sent',
74
            'ranking'   => true,
75
            'proof'     => true,
76
        ],
77
        [
78
            'id' => 6,
79
            'libStatus' => 'PROOVED',
80
            'class'     => 'proof--proved',
81
            'ranking'   => true,
82
            'proof'     => true,
83
        ],
84
        [
85
            'id' => 7,
86
            'libStatus' => 'NOT_PROOVED',
87
            'class'     => 'proof--unproved',
88
            'ranking'   => true,
89
            'proof'     => false,
90
        ],
91
    ];
92
93
    /**
94
     * @var array<mixed>
95
     */
96
    private array $scores = [
97
        [
98
            'player_id' => 1,
99
            'value'    => 9999,
100
            'status'   => 1,
101
        ],
102
        [
103
            'player_id' => 2,
104
            'value'    => 10101,
105
            'status'   => 6,
106
        ],
107
        [
108
            'player_id' => 3,
109
            'value'    => 10101,
110
            'status'   => 2,
111
        ],
112
    ];
113
114
    private function updateGeneratorType(ObjectManager $manager): void
115
    {
116
        foreach ($this->entities as $entity) {
117
            $metadata = $manager->getClassMetaData("VideoGamesRecords\\CoreBundle\\Entity\\" . $entity);
118
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
119
        }
120
    }
121
122
    /**
123
     * @throws Exception
124
     */
125
    public function load(ObjectManager $manager): void
126
    {
127
        $this->updateGeneratorType($manager);
128
        $this->loadStatus($manager);
129
        $this->loadScores($manager);
130
        $manager->flush();
131
    }
132
133
134
    /**
135
     * @param $manager
136
     */
137
    public function loadStatus($manager): void
138
    {
139
        foreach ($this->status as $row) {
140
            $playerChartStatus = new PlayerChartStatus();
141
            $playerChartStatus->setId($row['id']);
142
            $playerChartStatus->setName($row['libStatus']);
143
            $playerChartStatus->setClass($row['class']);
144
            $playerChartStatus->setBoolRanking($row['ranking']);
145
            $playerChartStatus->setBoolSendProof($row['proof']);
146
147
            $manager->persist($playerChartStatus);
148
            $this->addReference('player-chart-status' . $playerChartStatus->getId(), $playerChartStatus);
149
        }
150
    }
151
152
153
    /**
154
     * @param ObjectManager $manager
155
     * @throws Exception
156
     */
157
    private function loadScores(ObjectManager $manager): void
158
    {
159
        /** @var Chart $chart */
160
        $chart = $this->getReference('chart1');
161
162
        foreach ($this->scores as $row) {
163
            $playerChart = new PlayerChart();
164
            $playerChart->setPlayer($this->getReference('player' . $row['player_id']));
165
            $playerChart->setChart($chart);
166
            $playerChart->setStatus($this->getReference(sprintf('player-chart-status%d', $row['status'])));
167
            $playerChart->setCreatedAt(new Datetime());
168
            $playerChart->setUpdatedAt(new Datetime());
169
            $playerChart->setLastUpdate(new DateTime());
170
            $manager->persist($playerChart);
171
172
            foreach ($chart->getLibs() as $lib) {
173
                $playerChartLib = new PlayerChartLib();
174
                $playerChartLib->setPlayerChart($playerChart);
175
                $playerChartLib->setLibChart($lib);
176
                $playerChartLib->setValue($row['value']);
177
                $manager->persist($playerChartLib);
178
            }
179
        }
180
        $chart->setNbPost(count($this->scores));
181
    }
182
}
183