Passed
Push — develop ( 1358ff...4e8999 )
by BENARD
12:33
created

TeamSerieRankingHandler::handle()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 60
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 60
rs 9.504
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\Ranking\Command\Team;
4
5
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
6
use Symfony\Component\Serializer\Serializer;
7
use VideoGamesRecords\CoreBundle\Event\SerieEvent;
8
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
9
use VideoGamesRecords\CoreBundle\Tools\Ranking;
10
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
11
12
class TeamSerieRankingHandler extends AbstractRankingHandler
13
{
14
    public function handle($mixed): void
15
    {
16
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($mixed);
17
        if (null === $serie) {
18
            return;
19
        }
20
21
        // Delete old data
22
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\TeamSerie us WHERE us.serie = :serie');
23
        $query->setParameter('serie', $serie);
24
        $query->execute();
25
26
        // Select data
27
        $query = $this->em->createQuery("
28
            SELECT
29
                t.id as idTeam,
30
                '' as rankPointChart,
31
                '' as rankMedal,
32
                SUM(tg.chartRank0) as chartRank0,
33
                SUM(tg.chartRank1) as chartRank1,
34
                SUM(tg.chartRank2) as chartRank2,
35
                SUM(tg.chartRank3) as chartRank3,
36
                SUM(tg.pointGame) as pointGame,
37
                SUM(tg.pointChart) as pointChart
38
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
39
            JOIN tg.game g
40
            JOIN tg.team t
41
            WHERE g.serie = :serie
42
            GROUP BY t.id
43
            ORDER BY pointChart DESC");
44
45
        $query->setParameter('serie', $serie);
46
        $result = $query->getResult();
47
48
        $list = [];
49
        foreach ($result as $row) {
50
            $list[] = $row;
51
        }
52
53
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart']);
54
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
55
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3']);
56
57
        $normalizer = new ObjectNormalizer();
58
        $serializer = new Serializer([$normalizer]);
59
60
61
        foreach ($list as $row) {
62
            $teamSerie = $serializer->denormalize(
63
                $row, 'VideoGamesRecords\CoreBundle\Entity\TeamSerie'
64
            );
65
            $teamSerie->setTeam($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['idTeam']));
66
            $teamSerie->setSerie($serie);
67
68
            $this->em->persist($teamSerie);
69
            $this->em->flush();
70
        }
71
72
        $event = new SerieEvent($serie);
73
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::TEAM_SERIE_MAJ_COMPLETED);
74
    }
75
}
76