Passed
Push — develop ( e920eb...9cd2fd )
by BENARD
05:00
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\Ranking\Command\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\Handler\Ranking\AbstractRankingHandler;
9
use VideoGamesRecords\CoreBundle\Tools\Ranking;
10
11
class TeamChartRankingHandler extends AbstractRankingHandler
12
{
13
    private array $teams = [];
14
    private array $games = [];
15
    private array $groups = [];
16
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, 'VideoGamesRecords\CoreBundle\Entity\TeamChart'
93
            );
94
            $teamChart->setTeam($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['idTeam']));
95
            $teamChart->setChart($chart);
96
97
            $this->em->persist($teamChart);
98
        }
99
100
        $this->em->flush();
101
    }
102
103
    public function getTeams(): array
104
    {
105
        return $this->teams;
106
    }
107
108
    public function getGames(): array
109
    {
110
        return $this->games;
111
    }
112
113
    public function getGroups(): array
114
    {
115
        return $this->groups;
116
    }
117
}
118