Passed
Push — develop ( e920eb...9cd2fd )
by BENARD
05:00
created

PlayerRankingProvider::getRanking()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 36
rs 8.8497
c 0
b 0
f 0
cc 6
nc 12
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Player;
4
5
use Doctrine\ORM\Exception\ORMException;
6
7
class PlayerRankingProvider
8
{
9
    /**
10
     * @param array $options
11
     * @return array
12
     * @throws ORMException
13
     */
14
    public function getRankingPointChart(array $options = []): array
15
    {
16
        return $this->getRanking('rankPointChart', $options);
17
    }
18
19
    /**
20
     * @param array $options
21
     * @return array
22
     * @throws ORMException
23
     */
24
    public function getRankingPointGame(array $options = []): array
25
    {
26
        return $this->getRanking('rankPointGame', $options);
27
    }
28
29
    /**
30
     * @param array $options
31
     * @return array
32
     * @throws ORMException
33
     */
34
    public function getRankingMedals(array $options = []): array
35
    {
36
        return $this->getRanking('rankMedal', $options);
37
    }
38
39
    /**
40
     * @param array $options
41
     * @return array
42
     * @throws ORMException
43
     */
44
    public function getRankingBadge(array $options = []): array
45
    {
46
        return $this->getRanking('rankBadge', $options);
47
    }
48
49
    /**
50
     * @param array $options
51
     * @return array
52
     * @throws ORMException
53
     */
54
    public function getRankingCup(array $options = []): array
55
    {
56
        return $this->getRanking('rankCup', $options);
57
    }
58
59
    /**
60
     * @param array $options
61
     * @return array
62
     * @throws ORMException
63
     */
64
    public function getRankingProof(array $options = []): array
65
    {
66
        return $this->getRanking('rankProof', $options);
67
    }
68
69
    /**
70
     * @param string $column
71
     * @param array  $options
72
     * @return array
73
     * @throws ORMException
74
     */
75
    private function getRanking(string $column = 'rankPointChart', array $options = []): array
76
    {
77
        $maxRank = $options['maxRank'] ?? 100;
78
        $limit = $options['limit'] ?? null;
79
        $player = $this->getPlayer();
0 ignored issues
show
Bug introduced by
The method getPlayer() does not exist on VideoGamesRecords\CoreBu...r\PlayerRankingProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        /** @scrutinizer ignore-call */ 
80
        $player = $this->getPlayer();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        $team = !empty($options['idTeam']) ? $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $options['idTeam']) : null;
0 ignored issues
show
Bug Best Practice introduced by
The property em does not exist on VideoGamesRecords\CoreBu...r\PlayerRankingProvider. Did you maybe forget to declare it?
Loading history...
81
82
        $query = $this->em->createQueryBuilder()
83
            ->select('p')
84
            ->from('VideoGamesRecords\CoreBundle\Entity\Player', 'p')
85
            ->leftJoin('p.team', 't')
86
            ->addSelect('t')
87
            ->leftJoin('p.country', 'c')
88
            ->addSelect('c')
89
            ->leftJoin('c.translations', 'trans')
90
            ->addSelect('trans')
91
            ->where("p.$column != 0")
92
            ->orderBy("p.$column");
93
94
        if ($team !== null) {
95
            $query->andWhere('(p.team = :team)')
96
                ->setParameter('team', $team);
97
        } elseif (($maxRank !== null) && ($player !== null)) {
98
            $query->andWhere("(p.$column <= :maxRank OR p = :player)")
99
                ->setParameter('maxRank', $maxRank)
100
                ->setParameter('player', $player);
101
        } else {
102
            $query->andWhere("p.$column <= :maxRank")
103
                ->setParameter('maxRank', $maxRank);
104
        }
105
106
        if (null !== $limit) {
107
            $query->setMaxResults($limit);
108
        }
109
110
        return $query->getQuery()->getResult();
111
    }
112
}
113