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(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|