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

PlayerChartRankingHandler::handle()   F

Complexity

Conditions 16
Paths 723

Size

Total Lines 128
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 128
rs 1.7986
c 0
b 0
f 0
cc 16
nc 723
nop 1

How to fix   Long Method    Complexity   

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 Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
use VideoGamesRecords\CoreBundle\Entity\Chart;
8
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
9
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent;
10
use VideoGamesRecords\CoreBundle\DataProvider\Ranking\Player\PlayerChartRankingQuery;
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...PlayerChartRankingQuery 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...
11
use VideoGamesRecords\CoreBundle\Handler\Ranking\AbstractRankingHandler;
12
use VideoGamesRecords\CoreBundle\Tools\Ranking;
13
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
14
15
class PlayerChartRankingHandler extends AbstractRankingHandler
16
{
17
    private PlayerChartRankingQuery $playerChartRankingQuery;
18
    private array $players = [];
19
    private array $games = [];
20
    private array $groups = [];
21
22
23
    /**
24
     * @param PlayerChartRankingQuery $playerChartRankingQuery
25
     * @return void
26
     */
27
    public function setPlayerChartRankingQuert(PlayerChartRankingQuery $playerChartRankingQuery): void
28
    {
29
        $this->playerChartRankingQuery = $playerChartRankingQuery;
30
    }
31
32
    public function handle($mixed): void
33
    {
34
        /** @var Chart $chart */
35
        $chart = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Chart')->find($mixed);
36
        if (null === $chart) {
37
            return;
38
        }
39
40
        $this->groups[$chart->getGroup()->getId()] = $chart->getGroup();
41
        $this->games[$chart->getGroup()->getGame()->getId()] = $chart->getGroup()->getGame();
42
43
        $ranking     = $this->playerChartRankingQuery->getRanking($chart);
44
        $pointsChart = Ranking::chartPointProvider(count($ranking));
45
46
        $topScoreLibValue = '';
47
        $previousLibValue = '';
48
        $rank             = 1;
49
        $nbEqual          = 1;
50
        $playerChartEqual = [];
51
52
        $result = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\PlayerChart')->getPlatforms($chart);
53
        $platforms = [];
54
        foreach ($result as $row) {
55
            $platforms[$row['id']] = [
56
                'count' => $row['nb'],
57
                'points' => Ranking::platformPointProvider($row['nb']),
58
                'previousLibValue' => '',
59
                'rank' => 0,
60
                'nbEqual' => 1,
61
                'playerChartEqual' => [],
62
            ];
63
        }
64
65
        foreach ($ranking as $k => $item) {
66
            $libValue = '';
67
            /** @var PlayerChart $playerChart */
68
            $playerChart = $item[0];
69
70
            $this->players[$playerChart->getPlayer()->getId()] = $playerChart->getPlayer();
71
72
            // Lost position ?
73
            $oldRank = $playerChart->getRank();
74
            $oldNbEqual = $playerChart->getNbEqual();
75
            $playerChart->setTopScore(false);
76
77
            foreach ($chart->getLibs() as $lib) {
78
                $libValue .= $item['value_' . $lib->getIdLibChart()] . '/';
79
            }
80
            if ($k === 0) {
81
                // Premier élément => topScore
82
                $playerChart->setTopScore(true);
83
                $topScoreLibValue = $libValue;
84
            } else {
85
                if ($libValue === $topScoreLibValue) {
86
                    $playerChart->setTopScore(true);
87
                }
88
                if ($previousLibValue === $libValue) {
89
                    ++$nbEqual;
90
                } else {
91
                    $rank += $nbEqual;
92
                    $nbEqual = 1;
93
                    $playerChartEqual = [];
94
                }
95
96
            }
97
            // Platform point
98
            if ($playerChart->getPlatform() != null) {
99
                $idPlatForm = $playerChart->getPlatform()->getId();
100
                if ($platforms[$idPlatForm]['previousLibValue'] === $libValue) {
101
                    ++$platforms[$idPlatForm]['nbEqual'];
102
                } else {
103
                    $platforms[$idPlatForm]['rank'] += $platforms[$idPlatForm]['nbEqual'];
104
                    $platforms[$idPlatForm]['nbEqual'] = 1;
105
                    $platforms[$idPlatForm]['playerChartEqual'] = [];
106
                }
107
                $platforms[$idPlatForm]['playerChartEqual'][] = $playerChart;
108
            }
109
110
            $playerChartEqual[] = $playerChart;
111
112
            $playerChart
113
                ->setNbEqual($nbEqual)
114
                ->setRank($rank)
115
                ->setPointChart((int) (
116
                    array_sum(
117
                        array_slice(array_values($pointsChart), $playerChart->getRank() - 1, $playerChart->getNbEqual())
118
                    ) / $playerChart->getNbEqual()
119
                ));
120
121
            if ($nbEqual > 1) {
122
                // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points
123
                foreach ($playerChartEqual as $playerChartToModify) {
124
                    $playerChartToModify
125
                        ->setNbEqual($nbEqual)
126
                        ->setPointChart($playerChart->getPointChart());
127
                }
128
            }
129
130
            if ($playerChart->getPlatform() != null) {
131
                $idPlatForm = $playerChart->getPlatform()->getId();
132
                $playerChart->setPointPlatform((int) (
133
                    array_sum(
134
                        array_slice(array_values($platforms[$idPlatForm]['points']), $platforms[$idPlatForm]['rank'] - 1, $platforms[$idPlatForm]['nbEqual'])
135
                    ) / $platforms[$idPlatForm]['nbEqual']
136
                ));
137
                if ($platforms[$idPlatForm]['nbEqual'] > 1) {
138
                    // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points
139
                    foreach ($platforms[$idPlatForm]['playerChartEqual'] as $playerChartToModify) {
140
                        $playerChartToModify
141
                            ->setPointPlatform($playerChart->getPointPlatform());
142
                    }
143
                }
144
            } else {
145
                $playerChart->setPointPlatform(0);
146
            }
147
148
149
            $event = new PlayerChartEvent($playerChart, $oldRank, $oldNbEqual);
150
            $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED);
151
152
            $previousLibValue = $libValue;
153
154
            // Platform point
155
            if ($playerChart->getPlatform() != null) {
156
                $platforms[$playerChart->getPlatform()->getId()]['previousLibValue'] = $libValue;
157
            }
158
        }
159
        $this->em->flush();
160
    }
161
162
    public function getPlayers(): array
163
    {
164
        return $this->players;
165
    }
166
167
    public function getGames(): array
168
    {
169
        return $this->games;
170
    }
171
172
    public function getGroups(): array
173
    {
174
        return $this->groups;
175
    }
176
}
177