Passed
Push — develop ( 0240e9...c67e1d )
by BENARD
04:28
created

TeamRankingHandler::majRankPointGame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Command\Team;
4
5
use Doctrine\ORM\NonUniqueResultException;
6
use VideoGamesRecords\CoreBundle\Entity\Team;
7
use VideoGamesRecords\CoreBundle\Event\TeamEvent;
8
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
9
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
10
11
class TeamRankingHandler extends AbstractRankingHandler
12
{
13
14
    /**
15
     * @throws NonUniqueResultException
16
     */
17
    public function handle($mixed): void
18
    {
19
        /** @var Team $team */
20
        $team = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Team')->find($mixed);
21
        if (null === $team) {
22
            return;
23
        }
24
25
        $query = $this->em->createQuery("
26
            SELECT
27
                 t.id,
28
                 round(AVG(tg.rankPointChart),2) as averageGameRank,
29
                 SUM(tg.chartRank0) as chartRank0,
30
                 SUM(tg.chartRank1) as chartRank1,
31
                 SUM(tg.chartRank2) as chartRank2,
32
                 SUM(tg.chartRank3) as chartRank3,
33
                 SUM(tg.pointChart) as pointChart,
34
                 SUM(tg.pointGame) as pointGame,
35
                 COUNT(DISTINCT tg.game) as nbGame
36
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
37
            JOIN tg.team t
38
            WHERE tg.team = :team
39
            GROUP BY t.id");
40
41
        $query->setParameter('team', $team);
42
        $result = $query->getResult();
43
        if ($result) {
44
            $row = $result[0];
45
46
            $team->setAverageGameRank($row['averageGameRank']);
47
            $team->setChartRank0($row['chartRank0']);
48
            $team->setChartRank1($row['chartRank1']);
49
            $team->setChartRank2($row['chartRank2']);
50
            $team->setChartRank3($row['chartRank3']);
51
            $team->setPointChart($row['pointChart']);
52
            $team->setPointGame($row['pointGame']);
53
            $team->setNbGame($row['nbGame']);
54
        }
55
56
        // 2 game Ranking
57
        $data = [
58
            'gameRank0' => 0,
59
            'gameRank1' => 0,
60
            'gameRank2' => 0,
61
            'gameRank3' => 0,
62
        ];
63
64
        //----- data rank0
65
        $query = $this->em->createQuery("
66
            SELECT
67
                 t.id,
68
                 COUNT(tg.game) as nb
69
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
70
            JOIN tg.game g
71
            JOIN tg.team t
72
            WHERE g.nbTeam > 1
73
            AND tg.rankPointChart = 1
74
            AND tg.nbEqual = 1
75
            AND tg.team = :team
76
            GROUP BY t.id");
77
78
        $query->setParameter('team', $team);
79
        $row = $query->getOneOrNullResult();
80
        if ($row) {
81
            $data['gameRank0'] = $row['nb'];
82
        }
83
84
        //----- data rank1 to rank3
85
        $query = $this->em->createQuery("
86
            SELECT
87
                 t.id,
88
                 COUNT(tg.game) as nb
89
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
90
            JOIN tg.team t
91
            WHERE tg.rankPointChart = :rank
92
            AND tg.team = :team
93
            GROUP BY t.id");
94
95
        $query->setParameter('team', $team);
96
97
        for ($i = 1; $i <= 3; $i++) {
98
            $query->setParameter('rank', $i);
99
            $row = $query->getOneOrNullResult();
100
            if ($row) {
101
                $data['gameRank' . $i] = $row['nb'];
102
            }
103
        }
104
105
        $team->setGameRank0($data['gameRank0']);
106
        $team->setGameRank1($data['gameRank1']);
107
        $team->setGameRank2($data['gameRank2']);
108
        $team->setGameRank3($data['gameRank3']);
109
110
        // 3 Badge Ranking
111
        $query = $this->em->createQuery("
112
            SELECT
113
                 t.id,
114
                 COUNT(tb.badge) as nbMasterBadge,
115
                 SUM(b.value) as pointBadge
116
            FROM VideoGamesRecords\CoreBundle\Entity\TeamBadge tb
117
            JOIN tb.badge b
118
            JOIN tb.team t
119
            WHERE b.type = :type
120
            AND tb.team = :team
121
            AND tb.ended_at IS NULL
122
            GROUP BY t.id");
123
        $query->setParameter('type', 'Master');
124
        $query->setParameter('team', $team);
125
126
        $row = $query->getOneOrNullResult();
127
        if ($row) {
128
            $team->setNbMasterBadge($row['nbMasterBadge']);
129
            $team->setPointBadge($row['pointBadge']);
130
        }
131
132
        $this->em->persist($team);
133
        $this->em->flush();
134
135
        $event = new TeamEvent($team);
136
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::TEAM_MAJ_COMPLETED);
137
    }
138
}
139