TeamChartRankingHandler::handle()   C
last analyzed

Complexity

Conditions 16
Paths 29

Size

Total Lines 84
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 53
c 0
b 0
f 0
dl 0
loc 84
rs 5.5666
cc 16
nc 29
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
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Ranking\Command\Team;
6
7
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
8
use Symfony\Component\Serializer\Serializer;
9
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
10
use VideoGamesRecords\CoreBundle\Tools\Ranking;
11
12
class TeamChartRankingHandler extends AbstractRankingHandler
13
{
14
    private array $teams = [];
15
    private array $games = [];
16
    private array $groups = [];
17
18
    public function handle($mixed): void
19
    {
20
        $chart = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Chart')->find($mixed);
21
        if (null === $chart) {
22
            return;
23
        }
24
25
        $this->groups[$chart->getGroup()->getId()] = $chart->getGroup();
26
        $this->games[$chart->getGroup()->getGame()->getId()] = $chart->getGroup()->getGame();
27
28
        //----- delete
29
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\TeamChart tc WHERE tc.chart = :chart');
30
        $query->setParameter('chart', $chart);
31
        $query->execute();
32
33
        $query = $this->em->createQuery("
34
            SELECT pc
35
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
36
            JOIN pc.player p
37
            JOIN p.team t
38
            WHERE pc.chart = :chart
39
            ORDER BY pc.pointChart DESC");
40
41
        $query->setParameter('chart', $chart);
42
        $result = $query->getResult();
43
44
        $list = array();
45
        foreach ($result as $playerChart) {
46
            $team = $playerChart->getPlayer()->getTeam();
47
            $this->teams[$team->getId()] = $team;
48
49
            $idTeam = $team->getId();
50
            if (!isset($list[$idTeam])) {
51
                $list[$idTeam] = [
52
                    'idTeam' => $playerChart->getPlayer()->getTeam()->getId(),
53
                    'nbPlayer' => 1,
54
                    'pointChart' => $playerChart->getPointChart(),
55
                    'chartRank0' => 0,
56
                    'chartRank1' => 0,
57
                    'chartRank2' => 0,
58
                    'chartRank3' => 0,
59
                ];
60
            } elseif ($list[$idTeam]['nbPlayer'] < 5) {
61
                $list[$idTeam]['nbPlayer']   += 1;
62
                $list[$idTeam]['pointChart'] += $playerChart->getPointChart();
63
            }
64
        }
65
66
        //----- add some data
67
        $list = array_values($list);
68
        $list = Ranking::order($list, ['pointChart' => SORT_DESC]);
69
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
70
71
        $normalizer = new ObjectNormalizer();
72
        $serializer = new Serializer([$normalizer]);
73
74
        $nbTeam = count($list);
75
76
        foreach ($list as $row) {
77
            //----- add medals
78
            if ($row['rankPointChart'] == 1 && $row['nbEqual'] == 1 && $nbTeam > 1) {
79
                $row['chartRank0'] = 1;
80
                $row['chartRank1'] = 1;
81
            } elseif ($row['rankPointChart'] == 1 && $row['nbEqual'] == 1 && $nbTeam == 1) {
82
                $row['chartRank1'] = 1;
83
            } elseif ($row['rankPointChart'] == 1 && $row['nbEqual'] > 1) {
84
                $row['chartRank1'] = 1;
85
            } elseif ($row['rankPointChart'] == 2) {
86
                $row['chartRank2'] = 1;
87
            } elseif ($row['rankPointChart'] == 3) {
88
                $row['chartRank3'] = 1;
89
            }
90
91
            $teamChart = $serializer->denormalize(
92
                $row,
93
                'VideoGamesRecords\CoreBundle\Entity\TeamChart'
94
            );
95
            $teamChart->setTeam($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['idTeam']));
96
            $teamChart->setChart($chart);
97
98
            $this->em->persist($teamChart);
99
        }
100
101
        $this->em->flush();
102
    }
103
104
    public function getTeams(): array
105
    {
106
        return $this->teams;
107
    }
108
109
    public function getGames(): array
110
    {
111
        return $this->games;
112
    }
113
114
    public function getGroups(): array
115
    {
116
        return $this->groups;
117
    }
118
}
119