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

TeamRankingHandler::handle()   B

Complexity

Conditions 7
Paths 25

Size

Total Lines 120
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 120
rs 8.2012
c 0
b 0
f 0
cc 7
nc 25
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\Team;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\NonUniqueResultException;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use VideoGamesRecords\CoreBundle\Entity\Team;
10
use VideoGamesRecords\CoreBundle\Event\TeamEvent;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking;
12
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
13
14
class TeamRankingHandler
15
{
16
    private EntityManagerInterface $em;
17
    private EventDispatcherInterface $eventDispatcher;
18
19
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher)
20
    {
21
        $this->em = $em;
22
        $this->eventDispatcher = $eventDispatcher;
23
    }
24
25
    public function majAll()
26
    {
27
        $query = $this->em->createQuery("
28
            SELECT p
29
            FROM VideoGamesRecords\CoreBundle\Entity\Team p
30
            WHERE p.nbGame > 0"
31
        );
32
        $teams = $query->getResult();
33
        foreach ($teams as $team) {
34
            $this->handle($team->getId());
35
        }
36
    }
37
38
    /**
39
     * @throws NonUniqueResultException
40
     */
41
    public function handle($mixed): void
42
    {
43
        /** @var Team $team */
44
        $team = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Team')->find($mixed);
45
        if (null === $team) {
46
            return;
47
        }
48
49
        $query = $this->em->createQuery("
50
            SELECT
51
                 t.id,
52
                 round(AVG(tg.rankPointChart),2) as averageGameRank,
53
                 SUM(tg.chartRank0) as chartRank0,
54
                 SUM(tg.chartRank1) as chartRank1,
55
                 SUM(tg.chartRank2) as chartRank2,
56
                 SUM(tg.chartRank3) as chartRank3,
57
                 SUM(tg.pointChart) as pointChart,
58
                 SUM(tg.pointGame) as pointGame,
59
                 COUNT(DISTINCT tg.game) as nbGame
60
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
61
            JOIN tg.team t
62
            WHERE tg.team = :team
63
            GROUP BY t.id");
64
65
        $query->setParameter('team', $team);
66
        $result = $query->getResult();
67
        if ($result) {
68
            $row = $result[0];
69
70
            $team->setAverageGameRank($row['averageGameRank']);
71
            $team->setChartRank0($row['chartRank0']);
72
            $team->setChartRank1($row['chartRank1']);
73
            $team->setChartRank2($row['chartRank2']);
74
            $team->setChartRank3($row['chartRank3']);
75
            $team->setPointChart($row['pointChart']);
76
            $team->setPointGame($row['pointGame']);
77
            $team->setNbGame($row['nbGame']);
78
        }
79
80
        // 2 game Ranking
81
        $data = [
82
            'gameRank0' => 0,
83
            'gameRank1' => 0,
84
            'gameRank2' => 0,
85
            'gameRank3' => 0,
86
        ];
87
88
        //----- data rank0
89
        $query = $this->em->createQuery("
90
            SELECT
91
                 t.id,
92
                 COUNT(tg.game) as nb
93
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
94
            JOIN tg.game g
95
            JOIN tg.team t
96
            WHERE g.nbTeam > 1
97
            AND tg.rankPointChart = 1
98
            AND tg.nbEqual = 1
99
            AND tg.team = :team
100
            GROUP BY t.id");
101
102
        $query->setParameter('team', $team);
103
        $row = $query->getOneOrNullResult();
104
        if ($row) {
105
            $data['gameRank0'] = $row['nb'];
106
        }
107
108
        //----- data rank1 to rank3
109
        $query = $this->em->createQuery("
110
            SELECT
111
                 t.id,
112
                 COUNT(tg.game) as nb
113
            FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg
114
            JOIN tg.team t
115
            WHERE tg.rankPointChart = :rank
116
            AND tg.team = :team
117
            GROUP BY t.id");
118
119
        $query->setParameter('team', $team);
120
121
        for ($i = 1; $i <= 3; $i++) {
122
            $query->setParameter('rank', $i);
123
            $row = $query->getOneOrNullResult();
124
            if ($row) {
125
                $data['gameRank' . $i] = $row['nb'];
126
            }
127
        }
128
129
        $team->setGameRank0($data['gameRank0']);
130
        $team->setGameRank1($data['gameRank1']);
131
        $team->setGameRank2($data['gameRank2']);
132
        $team->setGameRank3($data['gameRank3']);
133
134
        // 3 Badge Ranking
135
        $query = $this->em->createQuery("
136
            SELECT
137
                 t.id,
138
                 COUNT(tb.badge) as nbMasterBadge,
139
                 SUM(b.value) as pointBadge
140
            FROM VideoGamesRecords\CoreBundle\Entity\TeamBadge tb
141
            JOIN tb.badge b
142
            JOIN tb.team t
143
            WHERE b.type = :type
144
            AND tb.team = :team
145
            AND tb.ended_at IS NULL
146
            GROUP BY t.id");
147
        $query->setParameter('type', 'Master');
148
        $query->setParameter('team', $team);
149
150
        $row = $query->getOneOrNullResult();
151
        if ($row) {
152
            $team->setNbMasterBadge($row['nbMasterBadge']);
153
            $team->setPointBadge($row['pointBadge']);
154
        }
155
156
        $this->em->persist($team);
157
        $this->em->flush();
158
159
        $event = new TeamEvent($team);
160
        $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::TEAM_MAJ_COMPLETED);
161
    }
162
163
    /**
164
     * @return void
165
     */
166
    public function majRank(): void
167
    {
168
        $this->majRankPointChart();
169
        $this->majRankPointGame();
170
        $this->majRankMedal();
171
        $this->majRankCup();
172
        $this->majRankBadge();
173
    }
174
175
    /**
176
     * @return void
177
     */
178
    private function majRankPointChart(): void
179
    {
180
        $teams = $this->getTeamRepository()->findBy(array(), array('pointChart' => 'DESC'));
181
        Ranking::addObjectRank($teams);
182
        $this->em->flush();
183
    }
184
185
    /**
186
     * @return void
187
     */
188
    private function majRankPointGame(): void
189
    {
190
        $teams = $this->getTeamRepository()->findBy(array(), array('pointGame' => 'DESC'));
191
        Ranking::addObjectRank($teams, 'rankPointGame', array('pointGame'));
192
        $this->em->flush();
193
    }
194
195
    /**
196
     * @return void
197
     */
198
    private function majRankMedal(): void
199
    {
200
        $teams = $this->getTeamRepository()->findBy(array(), array('chartRank0' => 'DESC', 'chartRank1' => 'DESC', 'chartRank2' => 'DESC', 'chartRank3' => 'DESC'));
201
        Ranking::addObjectRank($teams, 'rankMedal', array('chartRank0', 'chartRank1', 'chartRank2', 'chartRank3'));
202
        $this->em->flush();
203
    }
204
205
    /**
206
     * @return void
207
     */
208
    private function majRankCup(): void
209
    {
210
        $teams = $this->getTeamRepository()->findBy(array(), array('gameRank0' => 'DESC', 'gameRank1' => 'DESC', 'gameRank2' => 'DESC', 'gameRank3' => 'DESC'));
211
        Ranking::addObjectRank($teams, 'rankCup', array('gameRank0', 'gameRank1', 'gameRank2', 'gameRank3'));
212
        $this->em->flush();
213
    }
214
215
    /**
216
     * @return void
217
     */
218
    private function majRankBadge(): void
219
    {
220
        $teams = $this->getTeamRepository()->findBy(array(), array('pointBadge' => 'DESC', 'nbMasterBadge' => 'DESC'));
221
        Ranking::addObjectRank($teams, 'rankBadge', array('pointBadge', 'nbMasterBadge'));
222
        $this->em->flush();
223
    }
224
225
    private function getTeamRepository(): EntityRepository
226
    {
227
        return $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Team');
228
    }
229
}
230