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

PlayerSerieRankingHandler::handle()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 64
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 64
rs 9.536
c 0
b 0
f 0
cc 4
nc 5
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\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 PlayerSerieRankingHandler
11
{
12
    private EntityManagerInterface $em;
13
14
    public function __construct(EntityManagerInterface $em)
15
    {
16
        $this->em = $em;
17
    }
18
19
    public function majAll()
20
    {
21
        $series = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->findAll();
22
        foreach ($series as $serie) {
23
            $this->handle($serie->getId());
24
        }
25
    }
26
27
    public function handle($mixed): void
28
    {
29
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($mixed);
30
        if (null === $serie) {
31
            return;
32
        }
33
        
34
        // Delete old data
35
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\PlayerSerie us WHERE us.serie = :serie');
36
        $query->setParameter('serie', $serie);
37
        $query->execute();
38
39
        // Select data
40
        $query = $this->em->createQuery("
41
            SELECT
42
                p.id as idPlayer,
43
                '' as rankPointChart,
44
                '' as rankMedal,
45
                SUM(pg.chartRank0) as chartRank0,
46
                SUM(pg.chartRank1) as chartRank1,
47
                SUM(pg.chartRank2) as chartRank2,
48
                SUM(pg.chartRank3) as chartRank3,
49
                SUM(pg.chartRank4) as chartRank4,
50
                SUM(pg.chartRank5) as chartRank5,
51
                SUM(pg.pointGame) as pointGame,
52
                SUM(pg.pointChart) as pointChart,
53
                SUM(pg.pointChartWithoutDlc) as pointChartWithoutDlc,
54
                SUM(pg.nbChart) as nbChart,
55
                SUM(pg.nbChartWithoutDlc) as nbChartWithoutDlc,
56
                SUM(pg.nbChartProven) as nbChartProven,
57
                SUM(pg.nbChartProvenWithoutDlc) as nbChartProvenWithoutDlc,
58
                COUNT(DISTINCT pg.game) as nbGame
59
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerGame pg
60
            JOIN pg.game g
61
            JOIN pg.player p
62
            WHERE g.serie = :serie
63
            GROUP BY p.id
64
            ORDER BY pointChart DESC");
65
66
        $query->setParameter('serie', $serie);
67
        $result = $query->getResult();
68
69
        $list = [];
70
        foreach ($result as $row) {
71
            $list[] = $row;
72
        }
73
74
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart']);
75
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
76
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
77
78
        $normalizer = new ObjectNormalizer();
79
        $serializer = new Serializer([$normalizer]);
80
81
82
        foreach ($list as $row) {
83
            $playerSerie = $serializer->denormalize(
84
                $row, 'VideoGamesRecords\CoreBundle\Entity\PlayerSerie'
85
            );
86
            $playerSerie->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['idPlayer']));
87
            $playerSerie->setSerie($serie);
88
89
            $this->em->persist($playerSerie);
90
            $this->em->flush();
91
        }
92
    }
93
}
94