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

TeamGroupRankingHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 61
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 59 4
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\Handler\Ranking\AbstractRankingHandler;
8
use VideoGamesRecords\CoreBundle\Tools\Ranking;
9
10
class TeamGroupRankingHandler extends AbstractRankingHandler
11
{
12
    public function handle($mixed): void
13
    {
14
        $group = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->find($mixed);
15
        if (null === $group) {
16
            return;
17
        }
18
19
        //----- delete
20
        $query = $this->em->createQuery(
21
            'DELETE VideoGamesRecords\CoreBundle\Entity\TeamGroup tg WHERE tg.group = :group'
22
        );
23
        $query->setParameter('group', $group);
24
        $query->execute();
25
26
        //----- select ans save result in array
27
        $query = $this->em->createQuery("
28
            SELECT
29
                t.id,
30
                '' as rankPointChart,
31
                '' as rankMedal,
32
                SUM(tc.chartRank0) as chartRank0,
33
                SUM(tc.chartRank1) as chartRank1,
34
                SUM(tc.chartRank2) as chartRank2,
35
                SUM(tc.chartRank3) as chartRank3,
36
                SUM(tc.pointChart) as pointChart
37
            FROM VideoGamesRecords\CoreBundle\Entity\TeamChart tc
38
            JOIN tc.chart c
39
            JOIN tc.team t
40
            WHERE c.group = :group
41
            GROUP BY t.id
42
            ORDER BY pointChart DESC");
43
44
45
        $query->setParameter('group', $group);
46
        $result = $query->getResult();
47
48
        $list = [];
49
        foreach ($result as $row) {
50
            $list[] = $row;
51
        }
52
53
        //----- add some data
54
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
55
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
56
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3']);
57
58
        $normalizer = new ObjectNormalizer();
59
        $serializer = new Serializer([$normalizer]);
60
61
        foreach ($list as $row) {
62
            $teamGroup = $serializer->denormalize(
63
                $row, 'VideoGamesRecords\CoreBundle\Entity\TeamGroup'
64
            );
65
            $teamGroup->setTeam($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['id']));
66
            $teamGroup->setGroup($group);
67
68
            $this->em->persist($teamGroup);
69
        }
70
        $this->em->flush();
71
    }
72
}
73