PlayerRankingProvider   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

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