Passed
Push — develop ( e749e5...9ad298 )
by BENARD
04:20
created

PlayerGroupRankingHandler::getRankingMedals()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 32
rs 9.2408
cc 5
nc 4
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Service\Ranking\Write;
4
5
use DateTime;
6
use Doctrine\ORM\EntityManagerInterface;
7
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...
8
use Symfony\Component\Serializer\Serializer;
9
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
10
use VideoGamesRecords\CoreBundle\Interface\Ranking\RankingCommandInterface;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking;
12
13
class PlayerGroupRankingHandler implements RankingCommandInterface
14
{
15
    private EntityManagerInterface $em;
16
17
    public function __construct(EntityManagerInterface $em)
18
    {
19
        $this->em = $em;
20
    }
21
22
    public function handle($mixed): void
23
    {
24
        $group = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->find($mixed);
25
        if (null === $group) {
26
            return;
27
        }
28
29
        //----- delete
30
        $query = $this->em->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg WHERE pg.group = :group');
31
        $query->setParameter('group', $group);
32
        $query->execute();
33
34
        $data = [];
35
36
        //----- data rank0
37
        $query = $this->em->createQuery("
38
            SELECT
39
                 p.id,
40
                 COUNT(pc.id) as nb
41
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
42
            JOIN pc.player p
43
            JOIN pc.chart c
44
            WHERE c.group = :group
45
            AND pc.rank = 1
46
            AND c.nbPost > 1
47
            AND pc.nbEqual = 1
48
            GROUP BY p.id");
49
50
51
        $query->setParameter('group', $group);
52
        $result = $query->getResult();
53
        foreach ($result as $row) {
54
            $data['chartRank0'][$row['id']] = $row['nb'];
55
        }
56
57
        //----- data rank1 to rank5
58
        $query = $this->em->createQuery("
59
            SELECT
60
                 p.id,
61
                 COUNT(pc.id) as nb
62
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
63
            JOIN pc.player p
64
            JOIN pc.chart c
65
            WHERE c.group = :group
66
            AND pc.rank = :rank
67
            GROUP BY p.id");
68
        $query->setParameter('group', $group);
69
70
        for ($i = 1; $i <= 5; $i++) {
71
            $query->setParameter('rank', $i);
72
            $result = $query->getResult();
73
            foreach ($result as $row) {
74
                $data['chartRank' . $i][$row['id']] = $row['nb'];
75
            }
76
        }
77
78
        //----- data nbRecordProuve
79
        $query = $this->em->createQuery("
80
            SELECT
81
                 p.id,
82
                 COUNT(pc.id) as nb
83
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
84
            JOIN pc.player p
85
            JOIN pc.chart c
86
            WHERE c.group = :group
87
            AND pc.status = :status
88
            GROUP BY p.id");
89
90
        $query->setParameter('group', $group);
91
        $query->setParameter(
92
            'status',
93
            $this->em->getReference(
94
                PlayerChartStatus::class,
95
                PlayerChartStatus::ID_STATUS_PROOVED
96
            )
97
        );
98
99
        $result = $query->getResult();
100
        foreach ($result as $row) {
101
            $data['nbChartProven'][$row['id']] = $row['nb'];
102
        }
103
104
105
        //----- select and save result in array
106
        $query = $this->em->createQuery("
107
            SELECT
108
                p.id,
109
                '' as rankPoint,
110
                '' as rankMedal,
111
                SUM(pc.pointChart) as pointChart,
112
                COUNT(pc.id) as nbChart,
113
                MAX(pc.lastUpdate) as lastUpdate
114
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
115
            JOIN pc.player p
116
            JOIN pc.chart c
117
            WHERE c.group = :group
118
            GROUP BY p.id
119
            ORDER BY pointChart DESC");
120
121
122
        $query->setParameter('group', $group);
123
        $result = $query->getResult();
124
125
        $list = [];
126
        foreach ($result as $row) {
127
            $row['rankMedal'] = 0;
128
            $row['chartRank0'] = (isset($data['chartRank0'][$row['id']])) ? $data['chartRank0'][$row['id']] : 0;
129
            $row['chartRank1'] = (isset($data['chartRank1'][$row['id']])) ? $data['chartRank1'][$row['id']] : 0;
130
            $row['chartRank2'] = (isset($data['chartRank2'][$row['id']])) ? $data['chartRank2'][$row['id']] : 0;
131
            $row['chartRank3'] = (isset($data['chartRank3'][$row['id']])) ? $data['chartRank3'][$row['id']] : 0;
132
            $row['chartRank4'] = (isset($data['chartRank4'][$row['id']])) ? $data['chartRank4'][$row['id']] : 0;
133
            $row['chartRank5'] = (isset($data['chartRank5'][$row['id']])) ? $data['chartRank5'][$row['id']] : 0;
134
            $row['nbChartProven'] = (isset($data['nbChartProven'][$row['id']])) ? $data['nbChartProven'][$row['id']] : 0;
135
            $row['lastUpdate'] = new DateTime($row['lastUpdate']);
136
            $list[] = $row;
137
        }
138
139
        //----- add some data
140
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart']);
141
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
142
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
143
144
        $normalizer = new ObjectNormalizer();
145
        $serializer = new Serializer([$normalizer]);
146
147
        foreach ($list as $row) {
148
            $playerGroup = $serializer->denormalize(
149
                $row, 'VideoGamesRecords\CoreBundle\Entity\PlayerGroup'
150
            );
151
            $playerGroup->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['id']));
152
            $playerGroup->setGroup($group);
153
154
            $this->em->persist($playerGroup);
155
        }
156
        $this->em->flush();
157
    }
158
}
159