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