|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VideoGamesRecords\CoreBundle\Controller\PlayerChart; |
|
6
|
|
|
|
|
7
|
|
|
use ApiPlatform\Doctrine\Orm\Paginator; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use VideoGamesRecords\CoreBundle\Entity\PlayerChart; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Contrôleur pour récupérer les derniers scores postés (tous, sans distinction de jeux) |
|
16
|
|
|
*/ |
|
17
|
|
|
class GetLatestScores extends AbstractController |
|
18
|
|
|
{ |
|
19
|
|
|
private EntityManagerInterface $em; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(EntityManagerInterface $em) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->em = $em; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function __invoke(Request $request): Paginator |
|
27
|
|
|
{ |
|
28
|
|
|
// Récupérer les paramètres de pagination |
|
29
|
|
|
$days = (int) $request->query->get('days', 7); |
|
30
|
|
|
$page = $request->query->getInt('page', 1); |
|
31
|
|
|
$itemsPerPage = $request->query->getInt('itemsPerPage', 50); |
|
32
|
|
|
|
|
33
|
|
|
$queryBuilder = $this->em->createQueryBuilder() |
|
34
|
|
|
->select('pc') |
|
35
|
|
|
->from(PlayerChart::class, 'pc') |
|
36
|
|
|
->innerJoin('pc.chart', 'c') |
|
37
|
|
|
->innerJoin('c.group', 'g') |
|
38
|
|
|
->innerJoin('g.game', 'game') |
|
39
|
|
|
->innerJoin('pc.player', 'p') |
|
40
|
|
|
->addSelect('c', 'g', 'game', 'p') |
|
41
|
|
|
->orderBy('pc.lastUpdate', 'DESC') |
|
42
|
|
|
->addOrderBy('pc.id', 'DESC'); |
|
43
|
|
|
|
|
44
|
|
|
// Ajouter la condition sur les jours si spécifiée |
|
45
|
|
|
if ($days > 0) { |
|
46
|
|
|
$queryBuilder->where('pc.lastUpdate >= :dateLimit') |
|
47
|
|
|
->setParameter('dateLimit', new \DateTime("-{$days} days")); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Appliquer la pagination manuellement |
|
51
|
|
|
$firstResult = ($page - 1) * $itemsPerPage; |
|
52
|
|
|
$queryBuilder |
|
53
|
|
|
->setFirstResult($firstResult) |
|
54
|
|
|
->setMaxResults($itemsPerPage); |
|
55
|
|
|
|
|
56
|
|
|
return new Paginator(new DoctrinePaginator($queryBuilder)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|