<?php
declare(strict_types=1);
namespace App\Fast\Controller;
use App\Core\Event\CreateCompetitionEvent;
use App\Core\Event\CreateCompetitionRankingEvent;
use App\Core\Event\CreateMatchEvent;
use App\Core\Event\CreateTeamEvent;
use App\Core\Repository\TournamentRepository;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/fast/api/v1", name="fast_api_")
*/
class FastController extends AbstractFOSRestController
{
private TournamentRepository $tournamentRepository;
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;
public function __construct(
TournamentRepository $tournamentRepository,
LoggerInterface $dataLogger,
EventDispatcherInterface $eventDispatcher
) {
$this->tournamentRepository = $tournamentRepository;
$this->logger = $dataLogger;
$this->eventDispatcher = $eventDispatcher;
}
/**
* @Route("/ping/{apiKey}", name="ping", methods={"GET", "POST"})
*
* @param Request $request
* @param string $apiKey
*
* @return Response
*/
public function ping(Request $request, string $apiKey): Response
{
$tournament = $this->tournamentRepository->findOneByApiKey($apiKey);
if ($tournament === null) {
return new JsonResponse('Forbidden', Response::HTTP_FORBIDDEN);
}
return new Response();
}
/**
* @Route("/competitions/{apiKey}", name="competitions", methods={"GET", "POST"})
*
* @param Request $request
* @param string $apiKey
*
* @return JsonResponse
*/
public function processCompetitions(Request $request, string $apiKey): JsonResponse
{
$tournament = $this->tournamentRepository->findOneByApiKey($apiKey);
if ($tournament === null) {
return new JsonResponse('Forbidden', Response::HTTP_FORBIDDEN);
}
$data = $request->request->get('data');
$parsedData = json_decode($data);
foreach ($parsedData as $dataItem) {
$this->eventDispatcher->dispatch(new CreateCompetitionEvent($tournament, $dataItem));
}
return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
}
/**
* @Route("/launched-matches/{apiKey}", name="launched_matches", methods={"GET", "POST"})
*
* @param Request $request
* @param string $apiKey
*
* @return JsonResponse
*/
public function processLaunchedMatches(Request $request, string $apiKey): JsonResponse
{
$tournament = $this->tournamentRepository->findOneByApiKey($apiKey);
if ($tournament === null) {
return new JsonResponse('Forbidden', Response::HTTP_FORBIDDEN);
}
$data = $request->request->get('data');
$parsedData = json_decode($data);
$this->logger->debug(
'FAST log',
['fast_data' => $parsedData]
);
$this->eventDispatcher->dispatch(new CreateMatchEvent($tournament, $parsedData));
return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
}
/**
* @Route("/competition-registration/{apiKey}", name="competition_registration", methods={"GET", "POST"})
*
* @param Request $request
* @param string $apiKey
*
* @return JsonResponse
*/
public function processCompetitionRegistrations(Request $request, string $apiKey): JsonResponse
{
$tournament = $this->tournamentRepository->findOneByApiKey($apiKey);
if ($tournament === null) {
return new JsonResponse('Forbidden', Response::HTTP_FORBIDDEN);
}
$data = $request->request->get('data');
$parsedData = json_decode($data);
$this->logger->debug(
'FAST log',
['fast_data' => $parsedData]
);
$this->eventDispatcher->dispatch(new CreateTeamEvent($tournament, $parsedData));
return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
}
/**
* @Route("/competition-ranking/{apiKey}", name="competition_ranking", methods={"GET", "POST"})
*
* @param Request $request
* @param string $apiKey
*
* @return JsonResponse
*/
public function processCompetitionRanking(Request $request, string $apiKey): JsonResponse
{
$tournament = $this->tournamentRepository->findOneByApiKey($apiKey);
if ($tournament === null) {
return new JsonResponse('Forbidden', Response::HTTP_FORBIDDEN);
}
$data = $request->request->get('data');
$parsedData = json_decode($data);
$this->eventDispatcher->dispatch(new CreateCompetitionRankingEvent($tournament, $parsedData));
return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
}
}