PlayerGameRankingHandler::handle()   B
last analyzed

Complexity

Conditions 8
Paths 41

Size

Total Lines 130
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 58
c 0
b 0
f 0
dl 0
loc 130
rs 7.6719
cc 8
nc 41
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
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Ranking\Command\Player;
6
7
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
8
use Symfony\Component\Serializer\Serializer;
9
use VideoGamesRecords\CoreBundle\Entity\Game;
10
use VideoGamesRecords\CoreBundle\Event\GameEvent;
11
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
12
use VideoGamesRecords\CoreBundle\Tools\Ranking;
13
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
14
15
class PlayerGameRankingHandler extends AbstractRankingHandler
16
{
17
    public function handle($mixed): void
18
    {
19
        /** @var Game $game */
20
        $game = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Game')->find($mixed);
21
        if (null == $game) {
22
            return;
23
        }
24
25
        //----- delete
26
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\PlayerGame pg WHERE pg.game = :game');
27
        $query->setParameter('game', $game);
28
        $query->execute();
29
30
        //----- data without DLC
31
        $query = $this->em->createQuery("
32
            SELECT
33
                 p.id,
34
                 SUM(pg.pointChart) as pointChartWithoutDlc,
35
                 SUM(pg.nbChart) as nbChartWithoutDlc,
36
                 SUM(pg.nbChartProven) as nbChartProvenWithoutDlc
37
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg
38
            JOIN pg.player p
39
            JOIN pg.group g
40
            WHERE g.game = :game
41
            AND g.isDlc = 0
42
            GROUP BY p.id");
43
44
        $dataWithoutDlc = [];
45
46
        $query->setParameter('game', $game);
47
        $result = $query->getResult();
48
        foreach ($result as $row) {
49
            $dataWithoutDlc[$row['id']] = $row;
50
        }
51
52
        //----- select and save result in array
53
        $query = $this->em->createQuery("
54
            SELECT
55
                p.id,
56
                SUM(pg.chartRank0) as chartRank0,
57
                SUM(pg.chartRank1) as chartRank1,
58
                SUM(pg.chartRank2) as chartRank2,
59
                SUM(pg.chartRank3) as chartRank3,
60
                SUM(pg.chartRank4) as chartRank4,
61
                SUM(pg.chartRank5) as chartRank5,
62
                SUM(pg.pointChart) as pointChart
63
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg
64
            JOIN pg.player p
65
            JOIN pg.group g
66
            WHERE g.game = :game
67
            AND g.isRank = 1
68
            GROUP BY p.id");
69
70
71
        $dataRank = [];
72
        $query->setParameter('game', $game);
73
        $result = $query->getResult();
74
        foreach ($result as $row) {
75
            $dataRank[$row['id']] = $row;
76
        }
77
78
        //----- select and save result in array
79
        $query = $this->em->createQuery("
80
            SELECT
81
                p.id,
82
                '' as rankPointChart,
83
                '' as rankMedal,
84
                SUM(pg.nbChart) as nbChart,
85
                SUM(pg.nbChartProven) as nbChartProven,
86
                MAX(pg.lastUpdate) as lastUpdate
87
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg
88
            JOIN pg.player p
89
            JOIN pg.group g
90
            WHERE g.game = :game
91
            GROUP BY p.id");
92
93
94
        $query->setParameter('game', $game);
95
        $result = $query->getResult();
96
97
        $list = [];
98
        foreach ($result as $row) {
99
            $row['lastUpdate'] = new \DateTime($row['lastUpdate']);
100
            // $dataWithoutDlc
101
            if (isset($dataWithoutDlc[$row['id']])) {
102
                $row = array_merge($row, $dataWithoutDlc[$row['id']]);
103
            } else {
104
                $row['pointChartWithoutDlc'] = 0;
105
                $row['nbChartWithoutDlc'] = 0;
106
                $row['nbChartProvenWithoutDlc'] = 0;
107
            }
108
            // $dataRank
109
            if (isset($dataRank[$row['id']])) {
110
                $row = array_merge($row, $dataRank[$row['id']]);
111
            } else {
112
                $row['chartRank0'] = 0;
113
                $row['chartRank1'] = 0;
114
                $row['chartRank2'] = 0;
115
                $row['chartRank3'] = 0;
116
                $row['chartRank4'] = 0;
117
                $row['chartRank5'] = 0;
118
                $row['pointChart'] = 0;
119
            }
120
            $list[] = $row;
121
        }
122
123
        //----- add some data
124
        $list = Ranking::order($list, ['pointChart' => SORT_DESC]);
125
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
126
        $list = Ranking::calculateGamePoints($list, ['rankPointChart', 'nbEqual'], 'pointGame', 'pointChart');
127
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
128
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
129
130
        $normalizer = new ObjectNormalizer();
131
        $serializer = new Serializer([$normalizer]);
132
133
        foreach ($list as $row) {
134
            $playerGame = $serializer->denormalize(
135
                $row,
136
                'VideoGamesRecords\CoreBundle\Entity\PlayerGame'
137
            );
138
            $playerGame->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['id']));
139
            $playerGame->setGame($game);
140
141
            $this->em->persist($playerGame);
142
        }
143
        $this->em->flush();
144
145
        $event = new GameEvent($game);
146
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_GAME_MAJ_COMPLETED);
147
    }
148
}
149