Passed
Push — develop ( 462fa0...bd18e2 )
by BENARD
04:27
created

TeamChartRankingHandler::handle()   C

Complexity

Conditions 16
Paths 29

Size

Total Lines 83
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

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