UpdateTeamChartRankHandler::__invoke()   C
last analyzed

Complexity

Conditions 16
Paths 29

Size

Total Lines 94
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 58
nc 29
nop 1
dl 0
loc 94
rs 5.5666
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\Team;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Exception\ORMException;
9
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
10
use Symfony\Component\Messenger\Exception\ExceptionInterface;
11
use Symfony\Component\Messenger\MessageBusInterface;
12
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
13
use Symfony\Component\Serializer\Serializer;
14
use VideoGamesRecords\CoreBundle\Message\Team\UpdateTeamChartRank;
15
use VideoGamesRecords\CoreBundle\Message\Team\UpdateTeamGroupRank;
16
use VideoGamesRecords\CoreBundle\Tools\Ranking;
17
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...
18
19
#[AsMessageHandler]
20
readonly class UpdateTeamChartRankHandler
21
{
22
    public function __construct(
23
        private EntityManagerInterface $em,
24
        private MessageBusInterface $bus,
25
    ) {
26
    }
27
28
    /**
29
     * @throws ORMException
30
     * @throws ExceptionInterface
31
     */
32
    public function __invoke(UpdateTeamChartRank $updateTeamChartRank): array
33
    {
34
        $chart = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Chart')
35
            ->find($updateTeamChartRank->getChartId());
36
        if (null == $chart) {
37
            return ['error' => 'chart not found'];
38
        }
39
40
        //----- delete
41
        $query = $this->em
42
            ->createQuery('DELETE VideoGamesRecords\CoreBundle\Entity\TeamChart tc WHERE tc.chart = :chart');
43
        $query->setParameter('chart', $chart);
44
        $query->execute();
45
46
        $query = $this->em->createQuery("
47
            SELECT pc
48
            FROM VideoGamesRecords\CoreBundle\Entity\PlayerChart pc
49
            JOIN pc.player p
50
            JOIN p.team t
51
            WHERE pc.chart = :chart
52
            ORDER BY pc.pointChart DESC");
53
54
        $query->setParameter('chart', $chart);
55
        $result = $query->getResult();
56
57
        $list = array();
58
        foreach ($result as $playerChart) {
59
            $team = $playerChart->getPlayer()->getTeam();
60
61
            $idTeam = $team->getId();
62
            if (!isset($list[$idTeam])) {
63
                $list[$idTeam] = [
64
                    'idTeam' => $playerChart->getPlayer()->getTeam()->getId(),
65
                    'nbPlayer' => 1,
66
                    'pointChart' => $playerChart->getPointChart(),
67
                    'chartRank0' => 0,
68
                    'chartRank1' => 0,
69
                    'chartRank2' => 0,
70
                    'chartRank3' => 0,
71
                ];
72
            } elseif ($list[$idTeam]['nbPlayer'] < 5) {
73
                $list[$idTeam]['nbPlayer']   += 1;
74
                $list[$idTeam]['pointChart'] += $playerChart->getPointChart();
75
            }
76
        }
77
78
        //----- add some data
79
        $list = array_values($list);
80
        $list = Ranking::order($list, ['pointChart' => SORT_DESC]);
81
        $list = Ranking::addRank($list, 'rankPointChart', ['pointChart'], true);
82
83
        $normalizer = new ObjectNormalizer();
84
        $serializer = new Serializer([$normalizer]);
85
86
        $nbTeam = count($list);
87
88
        foreach ($list as $row) {
89
            //----- add medals
90
            if ($row['rankPointChart'] == 1 && $row['nbEqual'] == 1 && $nbTeam > 1) {
91
                $row['chartRank0'] = 1;
92
                $row['chartRank1'] = 1;
93
            } elseif ($row['rankPointChart'] == 1 && $row['nbEqual'] == 1 && $nbTeam == 1) {
94
                $row['chartRank1'] = 1;
95
            } elseif ($row['rankPointChart'] == 1 && $row['nbEqual'] > 1) {
96
                $row['chartRank1'] = 1;
97
            } elseif ($row['rankPointChart'] == 2) {
98
                $row['chartRank2'] = 1;
99
            } elseif ($row['rankPointChart'] == 3) {
100
                $row['chartRank3'] = 1;
101
            }
102
103
            $teamChart = $serializer->denormalize(
104
                $row,
105
                'VideoGamesRecords\CoreBundle\Entity\TeamChart'
106
            );
107
            $teamChart->setTeam(
108
                $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $row['idTeam'])
109
            );
110
            $teamChart->setChart($chart);
111
112
            $this->em->persist($teamChart);
113
        }
114
115
        $this->em->flush();
116
117
        $this->bus->dispatch(
118
            new UpdateTeamGroupRank($chart->getGroup()->getId()),
119
            [
120
                new DescriptionStamp(
121
                    sprintf('Update team-ranking for group [%d]', $chart->getGroup()->getId())
122
                )
123
            ]
124
        );
125
        return ['success' => true];
126
    }
127
}
128