UpdatePlayerGroupRankHandler::__invoke()   F
last analyzed

Complexity

Conditions 15
Paths 3097

Size

Total Lines 150
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

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

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
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\MessageHandler\Player;
6
7
use DateMalformedStringException;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Exception\ORMException;
10
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
11
use Symfony\Component\Messenger\Exception\ExceptionInterface;
12
use Symfony\Component\Messenger\MessageBusInterface;
13
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14
use Symfony\Component\Serializer\Serializer;
15
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus;
16
use VideoGamesRecords\CoreBundle\Message\Player\UpdatePlayerGameRank;
17
use VideoGamesRecords\CoreBundle\Message\Player\UpdatePlayerGroupRank;
18
use VideoGamesRecords\CoreBundle\Tools\Ranking;
19
use Zenstruck\Messenger\Monitor\Stamp\DescriptionStamp;
0 ignored issues
show
Bug introduced by
The type Zenstruck\Messenger\Monitor\Stamp\DescriptionStamp 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...
20
21
#[AsMessageHandler]
22
readonly class UpdatePlayerGroupRankHandler
23
{
24
    public function __construct(
25
        private EntityManagerInterface $em,
26
        private MessageBusInterface $bus,
27
    ) {
28
    }
29
30
    /**
31
     * @throws ORMException
32
     * @throws ExceptionInterface|DateMalformedStringException
33
     */
34
    public function __invoke(UpdatePlayerGroupRank $updatePlayerGroupRank): array
35
    {
36
        $group = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')
37
            ->find($updatePlayerGroupRank->getGroupId());
38
        if (null === $group) {
39
            return ['error' => 'group not found'];
40
        }
41
42
        //----- delete
43
        $query = $this->em->createQuery(
44
            'DELETE VideoGamesRecords\CoreBundle\Entity\PlayerGroup pg WHERE pg.group = :group'
45
        );
46
        $query->setParameter('group', $group);
47
        $query->execute();
48
49
        $data = [];
50
51
        //----- data rank0
52
        $query = $this->em->createQuery("
53
            SELECT
54
                 p.id,
55
                 COUNT(pc.id) as nb
56
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
57
            JOIN pc.player p
58
            JOIN pc.chart c
59
            WHERE c.group = :group
60
            AND pc.rank = 1
61
            AND c.nbPost > 1
62
            AND pc.nbEqual = 1
63
            GROUP BY p.id");
64
65
66
        $query->setParameter('group', $group);
67
        $result = $query->getResult();
68
        foreach ($result as $row) {
69
            $data['chartRank0'][$row['id']] = $row['nb'];
70
        }
71
72
        //----- data rank1 to rank5
73
        $query = $this->em->createQuery("
74
            SELECT
75
                 p.id,
76
                 COUNT(pc.id) as nb
77
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
78
            JOIN pc.player p
79
            JOIN pc.chart c
80
            WHERE c.group = :group
81
            AND pc.rank = :rank
82
            GROUP BY p.id");
83
        $query->setParameter('group', $group);
84
85
        for ($i = 1; $i <= 5; $i++) {
86
            $query->setParameter('rank', $i);
87
            $result = $query->getResult();
88
            foreach ($result as $row) {
89
                $data['chartRank' . $i][$row['id']] = $row['nb'];
90
            }
91
        }
92
93
        //----- data nbRecordProuve
94
        $query = $this->em->createQuery("
95
            SELECT
96
                 p.id,
97
                 COUNT(pc.id) as nb
98
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
99
            JOIN pc.player p
100
            JOIN pc.chart c
101
            WHERE c.group = :group
102
            AND pc.status = :status
103
            GROUP BY p.id");
104
105
        $query->setParameter('group', $group);
106
        $query->setParameter(
107
            'status',
108
            $this->em->getReference(
109
                PlayerChartStatus::class,
110
                PlayerChartStatus::ID_STATUS_PROOVED
111
            )
112
        );
113
114
        $result = $query->getResult();
115
        foreach ($result as $row) {
116
            $data['nbChartProven'][$row['id']] = $row['nb'];
117
        }
118
119
120
        //----- select and save result in array
121
        $query = $this->em->createQuery("
122
            SELECT
123
                p.id,
124
                '' as rankPoint,
125
                '' as rankMedal,
126
                SUM(pc.pointChart) as pointChart,
127
                COUNT(pc.id) as nbChart,
128
                MAX(pc.lastUpdate) as lastUpdate
129
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
130
            JOIN pc.player p
131
            JOIN pc.chart c
132
            WHERE c.group = :group
133
            GROUP BY p.id
134
            ORDER BY pointChart DESC");
135
136
137
        $query->setParameter('group', $group);
138
        $result = $query->getResult();
139
140
        $list = [];
141
        foreach ($result as $row) {
142
            $row['rankMedal'] = 0;
143
            $row['chartRank0'] = (isset($data['chartRank0'][$row['id']])) ? $data['chartRank0'][$row['id']] : 0;
144
            $row['chartRank1'] = (isset($data['chartRank1'][$row['id']])) ? $data['chartRank1'][$row['id']] : 0;
145
            $row['chartRank2'] = (isset($data['chartRank2'][$row['id']])) ? $data['chartRank2'][$row['id']] : 0;
146
            $row['chartRank3'] = (isset($data['chartRank3'][$row['id']])) ? $data['chartRank3'][$row['id']] : 0;
147
            $row['chartRank4'] = (isset($data['chartRank4'][$row['id']])) ? $data['chartRank4'][$row['id']] : 0;
148
            $row['chartRank5'] = (isset($data['chartRank5'][$row['id']])) ? $data['chartRank5'][$row['id']] : 0;
149
            $row['nbChartProven'] = (isset($data['nbChartProven'][$row['id']])) ? $data['nbChartProven'][$row['id']] : 0;
150
            $row['lastUpdate'] = new \DateTime($row['lastUpdate']);
151
            $list[] = $row;
152
        }
153
154
        //----- add some data
155
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart']);
156
        $list = Ranking::order($list, ['chartRank0' => SORT_DESC, 'chartRank1' => SORT_DESC, 'chartRank2' => SORT_DESC, 'chartRank3' => SORT_DESC]);
157
        $list = Ranking::addRank($list, 'rankMedal', ['chartRank0', 'chartRank1', 'chartRank2', 'chartRank3', 'chartRank4', 'chartRank5']);
158
159
        $normalizer = new ObjectNormalizer();
160
        $serializer = new Serializer([$normalizer]);
161
162
        foreach ($list as $row) {
163
            $playerGroup = $serializer->denormalize(
164
                $row,
165
                'VideoGamesRecords\CoreBundle\Entity\PlayerGroup'
166
            );
167
            $playerGroup->setPlayer($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Player', $row['id']));
168
            $playerGroup->setGroup($group);
169
170
            $this->em->persist($playerGroup);
171
        }
172
        $this->em->flush();
173
174
        $this->bus->dispatch(
175
            new UpdatePlayerGameRank($group->getGame()->getId()),
176
            [
177
                new DescriptionStamp(
178
                    sprintf('Update player-ranking for game [%d]', $group->getGame()->getId())
179
                )
180
            ]
181
        );
182
183
        return ['success' => true];
184
    }
185
}
186