Passed
Push — develop ( e920eb...9cd2fd )
by BENARD
05:00
created

TeamGameRankingHandler::handle()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 68
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 68
rs 9.1288
c 0
b 0
f 0
cc 5
nc 7
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;
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\Entity\Game;
8
use VideoGamesRecords\CoreBundle\Event\GameEvent;
9
use VideoGamesRecords\CoreBundle\Handler\Ranking\AbstractRankingHandler;
10
use VideoGamesRecords\CoreBundle\Tools\Ranking;
11
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
12
13
class TeamGameRankingHandler extends AbstractRankingHandler
14
{
15
    public function handle($mixed): void
16
    {
17
        /** @var Game $game */
18
        $game = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Game')->find($mixed);
19
        if (null === $game) {
20
            return;
21
        }
22
23
        //----- delete
24
        $query = $this->em->createQuery(
25
            'DELETE VideoGamesRecords\CoreBundle\Entity\TeamGame tg WHERE tg.game = :game'
26
        );
27
        $query->setParameter('game', $game);
28
        $query->execute();
29
30
        //----- select ans save result in array
31
        $query = $this->em->createQuery("
32
            SELECT
33
                t.id,
34
                '' as rankPointChart,
35
                '' as rankMedal,
36
                SUM(tg.chartRank0) as chartRank0,
37
                SUM(tg.chartRank1) as chartRank1,
38
                SUM(tg.chartRank2) as chartRank2,
39
                SUM(tg.chartRank3) as chartRank3,
40
                SUM(tg.pointChart) as pointChart
41
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGroup tg
42
            JOIN tg.group g
43
            JOIN tg.team t
44
            WHERE g.game = :game
45
            GROUP BY t.id
46
            ORDER BY pointChart DESC");
47
48
49
        $query->setParameter('game', $game);
50
        $result = $query->getResult();
51
52
        $list = [];
53
        foreach ($result as $row) {
54
            $list[] = $row;
55
        }
56
57
        $game->setNbTeam(count($list));
58
59
        //----- add some data
60
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
61
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
62
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3']);
63
        $list = Ranking::calculateGamePoints($list, array('rankPointChart', 'nbEqual'), 'pointGame', 'pointChart');
64
65
        $normalizer = new ObjectNormalizer();
66
        $serializer = new Serializer([$normalizer]);
67
68
        foreach ($list as $row) {
69
            if (isset($row['id'])) {
70
                $teamGame = $serializer->denormalize(
71
                    $row, 'VideoGamesRecords\CoreBundle\Entity\TeamGame'
72
                );
73
                $teamGame->setTeam($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['id']));
74
                $teamGame->setGame($game);
75
76
                $this->em->persist($teamGame);
77
            }
78
        }
79
        $this->em->flush();
80
81
        $event = new GameEvent($game);
82
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::TEAM_GAME_MAJ_COMPLETED);
83
    }
84
}
85