Passed
Push — develop ( e920eb...9cd2fd )
by BENARD
05:00
created

PlayerGameRankingHandler::handle()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 97
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 97
rs 8.6577
c 0
b 0
f 0
cc 6
nc 13
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\Ranking\Command\Player;
4
5
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serial...alizer\ObjectNormalizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Serializer\Serializer;
7
use VideoGamesRecords\CoreBundle\Entity\Game;
8
use VideoGamesRecords\CoreBundle\Event\GameEvent;
9
use VideoGamesRecords\CoreBundle\Handler\Ranking\AbstractRankingHandler;
10
use VideoGamesRecords\CoreBundle\Tools\Ranking;
11
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
12
13
class PlayerGameRankingHandler extends AbstractRankingHandler
14
{
15
    public function handle($mixed): void
16
    {
17
        /** @var Game $game */
18
        $game = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Game')->find($mixed);
19
        if (null === $game) {
20
            return;
21
        }
22
23
        //----- delete
24
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\PlayerGame pg WHERE pg.game = :game');
25
        $query->setParameter('game', $game);
26
        $query->execute();
27
28
        //----- data without DLC
29
        $query = $this->em->createQuery("
30
            SELECT
31
                 p.id,
32
                 SUM(pg.pointChart) as pointChartWithoutDlc,
33
                 SUM(pg.nbChart) as nbChartWithoutDlc,
34
                 SUM(pg.nbChartProven) as nbChartProvenWithoutDlc
35
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg
36
            JOIN pg.player p
37
            JOIN pg.group g
38
            WHERE g.game = :game
39
            AND g.boolDlc = 0
40
            GROUP BY p.id");
41
42
        $dataWithoutDlc = [];
43
44
        $query->setParameter('game', $game);
45
        $result = $query->getResult();
46
        foreach ($result as $row) {
47
            $dataWithoutDlc[$row['id']] = $row;
48
        }
49
50
        //----- select and save result in array
51
        $query = $this->em->createQuery("
52
            SELECT
53
                p.id,
54
                '' as rankPointChart,
55
                '' as rankMedal,
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
                SUM(pg.nbChart) as nbChart,
64
                SUM(pg.nbChartProven) as nbChartProven,
65
                MAX(pg.lastUpdate) as lastUpdate
66
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg
67
            JOIN pg.player p
68
            JOIN pg.group g
69
            WHERE g.game = :game
70
            GROUP BY p.id
71
            ORDER BY pointChart DESC");
72
73
74
        $query->setParameter('game', $game);
75
        $result = $query->getResult();
76
77
        $list = [];
78
        foreach ($result as $row) {
79
            $row['lastUpdate'] = new \DateTime($row['lastUpdate']);
80
            if (isset($dataWithoutDlc[$row['id']])) {
81
                $row = array_merge($row, $dataWithoutDlc[$row['id']]);
82
            } else {
83
                $row['pointChartWithoutDlc'] = 0;
84
                $row['nbChartWithoutDlc'] = 0;
85
                $row['nbChartProvenWithoutDlc'] = 0;
86
            }
87
            $list[] = $row;
88
        }
89
90
        //----- add some data
91
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
92
        $list = Ranking::calculateGamePoints($list, ['rankPointChart', 'nbEqual'], 'pointGame', 'pointChart');
93
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
94
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
95
96
        $normalizer = new ObjectNormalizer();
97
        $serializer = new Serializer([$normalizer]);
98
99
        foreach ($list as $row) {
100
            $playerGame = $serializer->denormalize(
101
                $row, 'VideoGamesRecords\CoreBundle\Entity\PlayerGame'
102
            );
103
            $playerGame->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['id']));
104
            $playerGame->setGame($game);
105
106
            $this->em->persist($playerGame);
107
        }
108
        $this->em->flush();
109
110
        $event = new GameEvent($game);
111
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_GAME_MAJ_COMPLETED);
112
    }
113
}
114