Passed
Push — develop ( df9e56...fcc4e3 )
by BENARD
12:25
created

PlayerRankingProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 57
c 1
b 0
f 0
dl 0
loc 151
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getRankingBadge() 0 5 1
A getRankingCup() 0 5 1
A getRankingPointChart() 0 5 1
A getPlayer() 0 6 2
A __construct() 0 6 1
A getRankingMedals() 0 5 1
A getRankingProof() 0 5 1
B getRankingQuery() 0 34 6
A getRankingPointGame() 0 5 1
A getRankingCountry() 0 7 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Player;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Exception\ORMException;
7
use Doctrine\ORM\Query;
8
use Doctrine\ORM\QueryBuilder;
9
use VideoGamesRecords\CoreBundle\Entity\Country;
10
use VideoGamesRecords\CoreBundle\Entity\Player;
11
use VideoGamesRecords\CoreBundle\Security\UserProvider;
12
13
class PlayerRankingProvider
14
{
15
    protected EntityManagerInterface $em;
16
    protected UserProvider $userProvider;
17
18
    public function __construct(
19
        EntityManagerInterface $em,
20
        UserProvider $userProvider
21
    ) {
22
        $this->em = $em;
23
        $this->userProvider = $userProvider;
24
    }
25
26
    /**
27
     * @throws ORMException
28
     */
29
    protected function getPlayer(): ?Player
30
    {
31
        if ($this->userProvider->getUser()) {
32
            return $this->userProvider->getPlayer();
33
        }
34
        return null;
35
    }
36
37
    /**
38
     * @param array $options
39
     * @return array
40
     * @throws ORMException
41
     */
42
    public function getRankingPointChart(array $options = []): array
43
    {
44
        return $this->getRankingQuery('rankPointChart', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
45
            ->getQuery()
46
            ->getResult();
47
    }
48
49
    /**
50
     * @param array $options
51
     * @return array
52
     * @throws ORMException
53
     */
54
    public function getRankingPointGame(array $options = []): array
55
    {
56
        return $this->getRankingQuery('rankPointGame', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
57
            ->getQuery()
58
            ->getResult();
59
    }
60
61
    /**
62
     * @param array $options
63
     * @return array
64
     * @throws ORMException
65
     */
66
    public function getRankingMedals(array $options = []): array
67
    {
68
        return $this->getRankingQuery('rankMedal', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
69
            ->getQuery()
70
            ->getResult();
71
    }
72
73
    /**
74
     * @param array $options
75
     * @return array
76
     * @throws ORMException
77
     */
78
    public function getRankingBadge(array $options = []): array
79
    {
80
        return $this->getRankingQuery('rankBadge', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
81
            ->getQuery()
82
            ->getResult();
83
    }
84
85
    /**
86
     * @param array $options
87
     * @return array
88
     * @throws ORMException
89
     */
90
    public function getRankingCup(array $options = []): array
91
    {
92
        return $this->getRankingQuery('rankCup', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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
            ->getQuery()
94
            ->getResult();
95
    }
96
97
    /**
98
     * @param array $options
99
     * @return array
100
     * @throws ORMException
101
     */
102
    public function getRankingProof(array $options = []): array
103
    {
104
        return $this->getRankingQuery('rankProof', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
105
            ->getQuery()
106
            ->getResult();
107
    }
108
109
    /**
110
     * @param Country $country
111
     * @param array $options
112
     * @return array
113
     * @throws ORMException
114
     */
115
    public function getRankingCountry(Country $country, array $options = []): array
116
    {
117
        return $this->getRankingQuery('rankCountry', $options)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRanking...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...
118
            ->andWhere('p.country = :country')
119
            ->setParameter('country', $country)
120
            ->getQuery()
121
            ->getResult();
122
    }
123
124
    /**
125
     * @param string $column
126
     * @param array $options
127
     * @return QueryBuilder
128
     * @throws ORMException
129
     */
130
    private function getRankingQuery(string $column = 'rankPointChart', array $options = []): QueryBuilder
131
    {
132
        $maxRank = $options['maxRank'] ?? 100;
133
        $limit = $options['limit'] ?? null;
134
        $player = $this->getPlayer();
135
        $team = !empty($options['idTeam']) ? $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Team', $options['idTeam']) : null;
136
137
        $query = $this->em->createQueryBuilder()
138
            ->select('p')
139
            ->from('VideoGamesRecords\CoreBundle\Entity\Player', 'p')
140
            ->leftJoin('p.team', 't')
141
            ->addSelect('t')
142
            ->leftJoin('p.country', 'c')
143
            ->addSelect('c')
144
            ->where("p.$column != 0")
145
            ->orderBy("p.$column");
146
147
        if ($team !== null) {
148
            $query->andWhere('(p.team = :team)')
149
                ->setParameter('team', $team);
150
        } elseif (($maxRank !== null) && ($player !== null)) {
151
            $query->andWhere("(p.$column <= :maxRank OR p = :player)")
152
                ->setParameter('maxRank', $maxRank)
153
                ->setParameter('player', $player);
154
        } else {
155
            $query->andWhere("p.$column <= :maxRank")
156
                ->setParameter('maxRank', $maxRank);
157
        }
158
159
        if (null !== $limit) {
160
            $query->setMaxResults($limit);
161
        }
162
163
        return $query;
164
    }
165
}
166