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

PlayerGroupRankingQuery::getRankingPoints()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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