Passed
Push — develop ( e749e5...9ad298 )
by BENARD
04:20
created

PlayerGameRankingHandler::getRankingMedals()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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