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