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

PlayerRankingQuery::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\DataProvider\Ranking\Player;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use VideoGamesRecords\CoreBundle\DataProvider\Ranking\AbstractRankingQuery;
7
8
class PlayerRankingQuery extends AbstractRankingQuery
9
{
10
    /**
11
     * @param array $options
12
     * @return array
13
     * @throws ORMException
14
     */
15
    public function getRankingPointChart(array $options = []): array
16
    {
17
        return $this->getRanking('rankPointChart', $options);
18
    }
19
20
    /**
21
     * @param array $options
22
     * @return array
23
     * @throws ORMException
24
     */
25
    public function getRankingPointGame(array $options = []): array
26
    {
27
        return $this->getRanking('rankPointGame', $options);
28
    }
29
30
    /**
31
     * @param array $options
32
     * @return array
33
     * @throws ORMException
34
     */
35
    public function getRankingMedals(array $options = []): array
36
    {
37
        return $this->getRanking('rankMedal', $options);
38
    }
39
40
    /**
41
     * @param array $options
42
     * @return array
43
     * @throws ORMException
44
     */
45
    public function getRankingBadge(array $options = []): array
46
    {
47
        return $this->getRanking('rankBadge', $options);
48
    }
49
50
    /**
51
     * @param array $options
52
     * @return array
53
     * @throws ORMException
54
     */
55
    public function getRankingCup(array $options = []): array
56
    {
57
        return $this->getRanking('rankCup', $options);
58
    }
59
60
    /**
61
     * @param array $options
62
     * @return array
63
     * @throws ORMException
64
     */
65
    public function getRankingProof(array $options = []): array
66
    {
67
        return $this->getRanking('rankProof', $options);
68
    }
69
70
    /**
71
     * @param string $column
72
     * @param array  $options
73
     * @return array
74
     * @throws ORMException
75
     */
76
    private function getRanking(string $column = 'rankPointChart', array $options = []): array
77
    {
78
        $maxRank = $options['maxRank'] ?? 100;
79
        $limit = $options['limit'] ?? null;
80
        $player = $this->getPlayer();
81
        $team = !empty($options['idTeam']) ? $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $options['idTeam']) : null;
82
83
        $query = $this->em->createQueryBuilder()
84
            ->select('p')
85
            ->from('VideoGamesRecords\CoreBundle\Entity\Player', 'p')
86
            ->leftJoin('p.team', 't')
87
            ->addSelect('t')
88
            ->leftJoin('p.country', 'c')
89
            ->addSelect('c')
90
            ->leftJoin('c.translations', 'trans')
91
            ->addSelect('trans')
92
            ->where("p.$column != 0")
93
            ->orderBy("p.$column");
94
95
        if ($team !== null) {
96
            $query->andWhere('(p.team = :team)')
97
                ->setParameter('team', $team);
98
        } elseif (($maxRank !== null) && ($player !== null)) {
99
            $query->andWhere("(p.$column <= :maxRank OR p = :player)")
100
                ->setParameter('maxRank', $maxRank)
101
                ->setParameter('player', $player);
102
        } else {
103
            $query->andWhere("p.$column <= :maxRank")
104
                ->setParameter('maxRank', $maxRank);
105
        }
106
107
        if (null !== $limit) {
108
            $query->setMaxResults($limit);
109
        }
110
111
        return $query->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
112
    }
113
114
    public function getRankingPoints(int $id = null, array $options = []): array
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

114
    public function getRankingPoints(/** @scrutinizer ignore-unused */ int $id = null, array $options = []): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

114
    public function getRankingPoints(int $id = null, /** @scrutinizer ignore-unused */ array $options = []): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        // TODO: Implement getRankingPoints() method.
117
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
118
}
119