Passed
Push — develop ( cfccb6...0b3512 )
by BENARD
04:54
created

TeamRankingProvider::getTeam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Team;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Exception\ORMException;
7
use VideoGamesRecords\CoreBundle\Entity\Team;
8
use VideoGamesRecords\CoreBundle\Security\UserProvider;
9
10
class TeamRankingProvider
11
{
12
    protected EntityManagerInterface $em;
13
    protected UserProvider $userProvider;
14
15
    public function __construct(
16
        EntityManagerInterface $em,
17
        UserProvider $userProvider
18
    ) {
19
        $this->em = $em;
20
        $this->userProvider = $userProvider;
21
    }
22
23
    /**
24
     * @throws ORMException
25
     */
26
    protected function getTeam(): ?Team
27
    {
28
        if ($this->userProvider->getUser()) {
29
            return $this->userProvider->getTeam();
30
        }
31
        return null;
32
    }
33
34
    /**
35
     * @param array $options
36
     * @return array
37
     * @throws ORMException
38
     */
39
    public function getRankingPointChart(array $options = []): array
40
    {
41
        return $this->getRanking('rankPointChart', $options);
42
    }
43
44
    /**
45
     * @param array $options
46
     * @return array
47
     * @throws ORMException
48
     */
49
    public function getRankingPointGame(array $options = []): array
50
    {
51
        return $this->getRanking('rankPointGame', $options);
52
    }
53
54
55
    /**
56
     * @param array $options
57
     * @return array
58
     * @throws ORMException
59
     */
60
    public function getRankingMedals(array $options = []): array
61
    {
62
        return $this->getRanking('rankMedal', $options);
63
    }
64
65
    /**
66
     * @param array $options
67
     * @return array
68
     * @throws ORMException
69
     */
70
    public function getRankingBadge(array $options = []): array
71
    {
72
        return $this->getRanking('rankBadge', $options);
73
    }
74
75
    /**
76
     * @param array $options
77
     * @return array
78
     * @throws ORMException
79
     */
80
    public function getRankingCup(array $options = []): array
81
    {
82
        return $this->getRanking('rankCup', $options);
83
    }
84
85
86
    /**
87
     * @param string $column
88
     * @param array  $options
89
     * @return array
90
     * @throws ORMException
91
     */
92
    private function getRanking(string $column = 'rankPointChart', array $options = []): array
93
    {
94
        $maxRank = $options['maxRank'] ?? 100;
95
        $team = $this->getTeam();
96
97
        $query = $this->em->createQueryBuilder()
98
            ->select('t')
99
            ->from('VideoGamesRecords\CoreBundle\Entity\Team', 't')
100
            ->where("t.$column IS NOT NULL")
101
            ->orderBy("t.$column");
102
103
        if ($team !== null) {
104
            $query->andWhere("(t.$column <= :maxRank OR t = :team)")
105
                ->setParameter('maxRank', $maxRank)
106
                ->setParameter('team', $team);
107
        } else {
108
            $query->andWhere("t.$column <= :maxRank")
109
                ->setParameter('maxRank', $maxRank);
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