tournament/src/Controller/TournamentController.php
2024-03-11 17:52:08 +02:00

63 lines
2.3 KiB
PHP

<?php
namespace App\Controller;
use App\Repository\TournamentRepository;
use App\Service\PlayOffGameService;
use App\Service\QualifyingGameService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TournamentController extends AbstractController
{
public function __construct(
private readonly TournamentRepository $tournamentRepository,
private readonly QualifyingGameService $qualifyingGameService,
private readonly PlayOffGameService $playOffGameService,
) {}
#[Route(path: '/', name: 'tournament_list_get', methods: ['GET'])]
public function getTournamentList(Request $request)
{
$page = $request->get('page') ?? 0;
return $this->render(
'index.html.twig',
[
'tournamentList' => $this->tournamentRepository->getTournamentList($page),
'tournamentCount' => $this->tournamentRepository->getTotalTournament(),
'page' => $page,
'tournamentPerPage' => TournamentRepository::TOURNAMENT_PER_PAGE
]
);
}
#[Route(path: '/tournament/{tournamentId}', name: 'tournament_get', methods: ['GET'])]
public function getTournament($tournamentId): Response
{
$tournament = $this->tournamentRepository->find($tournamentId);
$isQualifyingGameEnd = true;
foreach ($tournament->getDivisionList() as $division) {
$this->qualifyingGameService->scheduleGames($division);
$isQualifyingGameEnd = $this->qualifyingGameService->isQualificationGamesComplete($division) && $isQualifyingGameEnd;
}
if ($isQualifyingGameEnd) {
$qualificationWinners = $this->qualifyingGameService->getQualificationGameWinners($tournament);
$this->playOffGameService->scheduleGames($tournament, $qualificationWinners);
}
return $this->render(
'tournament.html.twig',
['tournament' => $tournament]
);
}
#[Route(path: '/tournament', name: 'tournament_create', methods: ['POST'])]
public function createTournament()
{
return $this->redirectToRoute('tournament_list');
}
}