Passed
Push — develop ( 462fa0...bd18e2 )
by BENARD
04:27
created

PlayerRankingHandler::handle()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 143
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 143
rs 8.0266
c 0
b 0
f 0
cc 7
nc 14
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\Handler\Ranking\Player;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\NonUniqueResultException;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use VideoGamesRecords\CoreBundle\Entity\Player;
10
use VideoGamesRecords\CoreBundle\Event\PlayerEvent;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking;
12
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
13
14
class PlayerRankingHandler
15
{
16
    private EntityManagerInterface $em;
17
    private EventDispatcherInterface $eventDispatcher;
18
19
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher)
20
    {
21
        $this->em = $em;
22
        $this->eventDispatcher = $eventDispatcher;
23
    }
24
25
    public function majAll()
26
    {
27
        $query = $this->em->createQuery("
28
            SELECT p
29
            FROM VideoGamesRecords\CoreBundle\Entity\Player p
30
            WHERE p.nbChart > 0"
31
        );
32
        $players = $query->getResult();
33
        foreach ($players as $player) {
34
            $this->handle($player->getId());
35
        }
36
    }
37
38
    /**
39
     * @throws NonUniqueResultException
40
     */
41
    public function handle($mixed): void
42
    {
43
        /** @var Player $player */
44
        $player = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player')->find($mixed);
45
        if (null === $player) {
46
            return;
47
        }
48
        if ($player->getId() == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $player->getId() of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
49
            return;
50
        }
51
52
        $query = $this->em->createQuery("
53
            SELECT
54
                 p.id,
55
                 SUM(g.nbChart) as nbChartMax,
56
                 round(AVG(pg.rankPointChart),2) as averageGameRank,
57
                 SUM(pg.chartRank0) as chartRank0,
58
                 SUM(pg.chartRank1) as chartRank1,
59
                 SUM(pg.chartRank2) as chartRank2,
60
                 SUM(pg.chartRank3) as chartRank3,
61
                 SUM(pg.nbChart) as nbChart,
62
                 SUM(pg.nbChartProven) as nbChartProven,
63
                 SUM(pg.pointChart) as pointChart,
64
                 SUM(pg.pointGame) as pointGame,
65
                 COUNT(DISTINCT pg.game) as nbGame
66
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGame pg
67
            JOIN pg.player p
68
            JOIN pg.game g
69
            WHERE pg.player = :player
70
            AND g.boolRanking = 1
71
            GROUP BY p.id");
72
        $query->setParameter('player', $player);
73
        $row = $query->getOneOrNullResult();
74
75
        $player->setNbChartMax($row['nbChartMax']);
76
        $player->setAverageGameRank($row['averageGameRank']);
77
        $player->setChartRank0($row['chartRank0']);
78
        $player->setChartRank1($row['chartRank1']);
79
        $player->setChartRank2($row['chartRank2']);
80
        $player->setChartRank3($row['chartRank3']);
81
        $player->setNbChart($row['nbChart']);
82
        $player->setNbChartProven($row['nbChartProven']);
83
        $player->setNbGame($row['nbGame']);
84
        $player->setPointChart($row['pointChart']);
85
        $player->setPointGame($row['pointGame']);
86
87
        // 2 game Ranking
88
        $data = [
89
            'gameRank0' => 0,
90
            'gameRank1' => 0,
91
            'gameRank2' => 0,
92
            'gameRank3' => 0,
93
        ];
94
95
        //----- data rank0
96
        $query = $this->em->createQuery("
97
            SELECT
98
                 p.id,
99
                 COUNT(pg.game) as nb
100
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGame pg
101
            JOIN pg.game g
102
            JOIN pg.player p
103
            WHERE pg.rankPointChart = 1
104
            AND pg.player = :player
105
            AND g.nbPlayer > 1
106
            AND g.boolRanking = 1
107
            AND pg.nbEqual = 1
108
            GROUP BY p.id");
109
110
        $query->setParameter('player', $player);
111
        $row = $query->getOneOrNullResult();
112
        if ($row) {
113
            $data['gameRank0'] = $row['nb'];
114
        }
115
        //----- data rank1 to rank3
116
        $query = $this->em->createQuery("
117
            SELECT
118
                 p.id,
119
                 COUNT(pg.game) as nb
120
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGame pg
121
            JOIN pg.game g
122
            JOIN pg.player p
123
            WHERE pg.rankPointChart = :rank
124
            AND pg.player = :player
125
            AND g.boolRanking = 1
126
            GROUP BY p.id");
127
128
        $query->setParameter('player', $player);
129
        for ($i = 1; $i <= 3; $i++) {
130
            $query->setParameter('rank', $i);
131
            $row = $query->getOneOrNullResult();
132
            if ($row) {
133
                $data['gameRank' . $i] = $row['nb'];
134
            }
135
        }
136
137
        $player->setGameRank0($data['gameRank0']);
138
        $player->setGameRank1($data['gameRank1']);
139
        $player->setGameRank2($data['gameRank2']);
140
        $player->setGameRank3($data['gameRank3']);
141
142
143
        // 3 Badge Ranking
144
        $query = $this->em->createQuery("
145
            SELECT
146
                 p.id,
147
                 COUNT(pb.badge) as nbMasterBadge,
148
                 SUM(b.value) as pointBadge
149
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerBadge pb
150
            JOIN pb.badge b
151
            JOIN b.game g
152
            JOIN pb.player p
153
            WHERE b.type = :type
154
            AND pb.player = :player
155
            AND pb.ended_at IS NULL
156
            AND g.boolRanking = 1
157
            GROUP BY p.id");
158
        $query->setParameter('type', 'Master');
159
        $query->setParameter('player', $player);
160
161
        $row = $query->getOneOrNullResult();
162
        if ($row) {
163
            $player->setNbMasterBadge($row['nbMasterBadge']);
164
            $player->setPointBadge($row['pointBadge']);
165
        }
166
167
        // 4 nbChartWithPlatform
168
        $query = $this->em->createQuery("
169
            SELECT COUNT(pc) as nb
170
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
171
            WHERE pc.player = :player
172
            AND pc.platform IS NOT NULL");
173
        $query->setParameter('player', $player);
174
175
        $nb = $query->getSingleScalarResult();
176
        $player->setNbChartWithPlatform($nb);
177
        $player->getCountry()?->setBoolMaj(true);
178
179
        $this->em->persist($player);
180
        $this->em->flush();
181
182
        $event = new PlayerEvent($player);
183
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_MAJ_COMPLETED);
184
    }
185
186
    /**
187
     * @return void
188
     */
189
    public function majRank(): void
190
    {
191
        $this->majRankPointChart();
192
        $this->majRankPointGame();
193
        $this->majRankMedal();
194
        $this->majRankCup();
195
    }
196
197
    /**
198
     * @return void
199
     */
200
    public function majRankPointChart(): void
201
    {
202
        $players = $this->getPlayerRepository()->findBy(array(), array('pointChart' => 'DESC'));
203
        Ranking::addObjectRank($players);
204
        $this->em->flush();
205
    }
206
207
    /**
208
     * @return void
209
     */
210
    public function majRankMedal(): void
211
    {
212
        $players = $this->getPlayerRepository()->findBy(array(), array('chartRank0' => 'DESC', 'chartRank1' => 'DESC', 'chartRank2' => 'DESC', 'chartRank3' => 'DESC'));
213
        Ranking::addObjectRank($players, 'rankMedal', array('chartRank0', 'chartRank1', 'chartRank2', 'chartRank3'));
214
        $this->em->flush();
215
    }
216
217
    /**
218
     * @return void
219
     */
220
    public function majRankPointGame(): void
221
    {
222
        $players = $this->getPlayerRepository()->findBy(array(), array('pointGame' => 'DESC'));
223
        Ranking::addObjectRank($players, 'rankPointGame', array('pointGame'));
224
        $this->em->flush();
225
    }
226
227
    /**
228
     * @return void
229
     */
230
    public function majRankCup(): void
231
    {
232
        $players = $this->getPlayerRepository()->findBy(array(), array('gameRank0' => 'DESC', 'gameRank1' => 'DESC', 'gameRank2' => 'DESC', 'gameRank3' => 'DESC'));
233
        Ranking::addObjectRank($players, 'rankCup', array('gameRank0', 'gameRank1', 'gameRank2', 'gameRank3'));
234
        $this->em->flush();
235
    }
236
237
    /**
238
     * @return void
239
     */
240
    public function majRankProof(): void
241
    {
242
        $players = $this->getPlayerRepository()->findBy(array(), array('nbChartProven' => 'DESC'));
243
        Ranking::addObjectRank($players, 'rankProof', array('nbChartProven'));
244
        $this->em->flush();
245
    }
246
247
    /**
248
     * @return void
249
     */
250
    public function majRankBadge(): void
251
    {
252
        $players = $this->getPlayerRepository()->findBy(array(), array('pointBadge' => 'DESC', 'nbMasterBadge' => 'DESC'));
253
        Ranking::addObjectRank($players, 'rankBadge', array('pointBadge', 'nbMasterBadge'));
254
        $this->em->flush();
255
    }
256
257
    private function getPlayerRepository(): EntityRepository
258
    {
259
        return $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player');
260
    }
261
}
262