Passed
Push — develop ( 03e02c...0276b7 )
by BENARD
04:36
created

GetRanking   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 28 5
A __construct() 0 3 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller\Chart\Player;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\Request;
8
use VideoGamesRecords\CoreBundle\Ranking\Provider\Player\PlayerChartRankingProvider;
9
use VideoGamesRecords\CoreBundle\Entity\Chart;
10
use VideoGamesRecords\CoreBundle\Tools\Score;
11
12
class GetRanking extends AbstractController
13
{
14
    private PlayerChartRankingProvider $playerChartRankingProvider;
15
16
    public function __construct(PlayerChartRankingProvider $playerChartRankingProvider)
17
    {
18
        $this->playerChartRankingProvider = $playerChartRankingProvider;
19
    }
20
21
    /**
22
     * @param Chart   $chart
23
     * @param Request $request
24
     * @return array
25
     * @throws ORMException
26
     */
27
    public function __invoke(Chart $chart, Request $request): array
28
    {
29
        $ranking = $this->playerChartRankingProvider->getRanking(
30
            $chart,
31
            [
32
                'maxRank' => $request->query->get('maxRank', 100),
33
            ]
34
        );
35
36
        if (!$chart->getStatusPlayer()->isNormal()) {
37
            $i = 1;
38
            foreach ($ranking as $row) {
39
                $row[0]->setRank($i);
40
                $i++;
41
            }
42
        }
43
44
        for ($i = 0; $i <= count($ranking) - 1; $i++) {
45
            foreach ($chart->getLibs() as $lib) {
46
                $key = $lib->getIdLibChart();
47
                // format value
48
                $ranking[$i]['values'][] = Score::formatScore(
49
                    $ranking[$i]["value_$key"],
50
                    $lib->getType()->getMask()
51
                );
52
            }
53
        }
54
        return $ranking;
55
    }
56
}
57