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

TeamGroupRankingProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 47
dl 0
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRankingMedals() 0 32 5
A getRankingPoints() 0 32 5
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Team;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use VideoGamesRecords\CoreBundle\Ranking\Provider\AbstractRankingProvider;
7
8
class TeamGroupRankingProvider 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
        $group = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->find($id);
19
        if (null === $group) {
20
            return [];
21
        }
22
23
        $maxRank = $options['maxRank'] ?? null;
24
        $team = $this->getTeam();
25
26
        $query = $this->em->createQueryBuilder()
27
            ->select('tg')
28
            ->from('VideoGamesRecords\CoreBundle\Entity\TeamGroup', 'tg')
29
            ->join('tg.team', 't')
30
            ->addSelect('t')
31
            ->orderBy('tg.rankPointChart');
32
33
        $query->where('tg.group = :group')
34
            ->setParameter('group', $group);
35
36
        if (($maxRank !== null) && ($team !== null)) {
37
            $query->andWhere('(tg.rankPointChart <= :maxRank OR tg.team = :team)')
38
                ->setParameter('maxRank', $maxRank)
39
                ->setParameter('team', $team);
40
        } elseif ($maxRank !== null) {
41
            $query->andWhere('tg.rankPointChart <= :maxRank')
42
                ->setParameter('maxRank', $maxRank);
43
        } else {
44
            $query->setMaxResults(100);
45
        }
46
47
        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...
48
    }
49
50
    /**
51
     * @param int|null $id
52
     * @param array $options
53
     * @return array
54
     * @throws ORMException
55
     */
56
    public function getRankingMedals(int $id = null, array $options = []): array
57
    {
58
        $group = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->find($id);
59
        if (null === $group) {
60
            return [];
61
        }
62
63
        $maxRank = $options['maxRank'] ?? null;
64
        $team = $this->getTeam();
65
66
        $query = $this->em->createQueryBuilder()
67
            ->select('tg')
68
            ->from('VideoGamesRecords\CoreBundle\Entity\TeamGroup', 'tg')
69
            ->join('tg.team', 't')
70
            ->addSelect('t')
71
            ->orderBy('tg.rankMedal');
72
73
        $query->where('tg.group = :group')
74
            ->setParameter('group', $group);
75
76
        if (($maxRank !== null) && ($team !== null)) {
77
            $query->andWhere('(tg.rankMedal <= :maxRank OR tg.team = :team)')
78
                ->setParameter('maxRank', $maxRank)
79
                ->setParameter('team', $team);
80
        } elseif ($maxRank !== null) {
81
            $query->andWhere('tg.rankMedal <= :maxRank')
82
                ->setParameter('maxRank', $maxRank);
83
        } else {
84
            $query->setMaxResults(100);
85
        }
86
87
        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...
88
    }
89
}
90