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

PlayerGroupRankingHandler::handle()   F

Complexity

Conditions 15
Paths 3097

Size

Total Lines 135
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 135
rs 1.7499
c 0
b 0
f 0
cc 15
nc 3097
nop 1

How to fix   Long Method    Complexity   

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