PlayerSerieRankingHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 28
c 0
b 0
f 0
dl 0
loc 71
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 69 4
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\Event\SerieEvent;
10
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking;
12
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
13
14
class PlayerSerieRankingHandler extends AbstractRankingHandler
15
{
16
    public function handle($mixed): void
17
    {
18
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($mixed);
19
        if (null === $serie) {
20
            return;
21
        }
22
23
        // Delete old data
24
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\PlayerSerie us WHERE us.serie = :serie');
25
        $query->setParameter('serie', $serie);
26
        $query->execute();
27
28
        // Select data
29
        $query = $this->em->createQuery("
30
            SELECT
31
                p.id as idPlayer,
32
                '' as rankPointChart,
33
                '' as rankMedal,
34
                SUM(pg.chartRank0) as chartRank0,
35
                SUM(pg.chartRank1) as chartRank1,
36
                SUM(pg.chartRank2) as chartRank2,
37
                SUM(pg.chartRank3) as chartRank3,
38
                SUM(pg.chartRank4) as chartRank4,
39
                SUM(pg.chartRank5) as chartRank5,
40
                SUM(pg.pointGame) as pointGame,
41
                SUM(pg.pointChart) as pointChart,
42
                SUM(pg.pointChartWithoutDlc) as pointChartWithoutDlc,
43
                SUM(pg.nbChart) as nbChart,
44
                SUM(pg.nbChartWithoutDlc) as nbChartWithoutDlc,
45
                SUM(pg.nbChartProven) as nbChartProven,
46
                SUM(pg.nbChartProvenWithoutDlc) as nbChartProvenWithoutDlc,
47
                COUNT(DISTINCT pg.game) as nbGame
48
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGame pg
49
            JOIN pg.game g
50
            JOIN pg.player p
51
            WHERE g.serie = :serie
52
            GROUP BY p.id
53
            ORDER BY pointChart DESC");
54
55
        $query->setParameter('serie', $serie);
56
        $result = $query->getResult();
57
58
        $list = [];
59
        foreach ($result as $row) {
60
            $list[] = $row;
61
        }
62
63
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart']);
64
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
65
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
66
67
        $normalizer = new ObjectNormalizer();
68
        $serializer = new Serializer([$normalizer]);
69
70
71
        foreach ($list as $row) {
72
            $playerSerie = $serializer->denormalize(
73
                $row,
74
                'VideoGamesRecords\CoreBundle\Entity\PlayerSerie'
75
            );
76
            $playerSerie->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['idPlayer']));
77
            $playerSerie->setSerie($serie);
78
79
            $this->em->persist($playerSerie);
80
            $this->em->flush();
81
        }
82
83
        $event = new SerieEvent($serie);
84
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_SERIE_MAJ_COMPLETED);
85
    }
86
}
87