Passed
Push — develop ( 7dc84c...ae723d )
by BENARD
12:22
created

PlayerGameRankingProvider::getRankingMedals()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 36
rs 8.5546
cc 7
nc 9
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Player;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use VideoGamesRecords\CoreBundle\Ranking\Provider\AbstractRankingProvider;
7
8
class PlayerGameRankingProvider extends AbstractRankingProvider
9
{
10
    /**
11
     * @param int|null $id
12
     * @param array $options
13
     * @return array
14
     * @throws ORMException
15
     */
16
    public function getRankingPoints(int $id = null, array $options = []): array
17
    {
18
        $game = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Game')->find($id);
19
        if (null === $game) {
20
            return [];
21
        }
22
23
        $maxRank = $options['maxRank'] ?? null;
24
        $player = $this->getPlayer($options['user'] ?? null);
25
        $team = !empty($options['idTeam']) ? $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $options['idTeam']) : null;
26
27
        $query = $this->em->createQueryBuilder()
28
            ->select('pg')
29
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerGame', 'pg')
30
            ->join('pg.player', 'p')
31
            ->addSelect('p')
32
            ->orderBy('pg.rankPointChart');
33
34
        $query->where('pg.game = :game')
35
            ->setParameter('game', $game);
36
37
        if ($team != null) {
38
            $query->andWhere('(p.team = :team)')
39
                ->setParameter('team', $team);
40
        } elseif (($maxRank !== null) && ($player !== null)) {
41
            $query->andWhere('(pg.rankPointChart <= :maxRank OR pg.player = :player)')
42
                ->setParameter('maxRank', $maxRank)
43
                ->setParameter('player', $player);
44
        } elseif ($maxRank !== null) {
45
            $query->andWhere('pg.rankPointChart <= :maxRank')
46
                ->setParameter('maxRank', $maxRank);
47
        } else {
48
            $query->setMaxResults(100);
49
        }
50
        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...
51
    }
52
53
    /**
54
     * @param int|null $id
55
     * @param array $options
56
     * @return array
57
     * @throws ORMException
58
     */
59
    public function getRankingMedals(int $id = null, array $options = []): array
60
    {
61
        $game = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Game')->find($id);
62
        if (null === $game) {
63
            return [];
64
        }
65
66
        $maxRank = $options['maxRank'] ?? null;
67
        $player = $this->getPlayer($options['user'] ?? null);
68
        $team = !empty($options['idTeam']) ? $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $options['idTeam']) : null;
69
70
        $query = $this->em->createQueryBuilder()
71
            ->select('pg')
72
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerGame', 'pg')
73
            ->join('pg.player', 'p')
74
            ->addSelect('p')
75
            ->orderBy('pg.rankMedal');
76
77
        $query->where('pg.game = :game')
78
            ->setParameter('game', $game);
79
80
        if ($team != null) {
81
            $query->andWhere('(p.team = :team)')
82
                ->setParameter('team', $team);
83
        } elseif (($maxRank !== null) && ($player !== null)) {
84
            $query->andWhere('(pg.rankMedal <= :maxRank OR pg.player = :player)')
85
                ->setParameter('maxRank', $maxRank)
86
                ->setParameter('player', $player);
87
        } elseif ($maxRank !== null) {
88
            $query->andWhere('pg.rankMedal <= :maxRank')
89
                ->setParameter('maxRank', $maxRank);
90
        } else {
91
            $query->setMaxResults(100);
92
        }
93
94
        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...
95
    }
96
}
97