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

TeamGroupRankingHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 28
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 59 4
A __construct() 0 3 1
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