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

TeamGroupRankingHandler::handle()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 59
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 59
rs 9.52
c 0
b 0
f 0
cc 4
nc 5
nop 1

How to fix   Long Method   

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