src/Fast/Controller/FastController.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Fast\Controller;
  4. use App\Core\Event\CreateCompetitionEvent;
  5. use App\Core\Event\CreateCompetitionRankingEvent;
  6. use App\Core\Event\CreateMatchEvent;
  7. use App\Core\Event\CreateTeamEvent;
  8. use App\Core\Repository\TournamentRepository;
  9. use FOS\RestBundle\Controller\AbstractFOSRestController;
  10. use Psr\EventDispatcher\EventDispatcherInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route("/fast/api/v1", name="fast_api_")
  18.  */
  19. class FastController extends AbstractFOSRestController
  20. {
  21.     private TournamentRepository $tournamentRepository;
  22.     private LoggerInterface $logger;
  23.     private EventDispatcherInterface $eventDispatcher;
  24.     public function __construct(
  25.         TournamentRepository $tournamentRepository,
  26.         LoggerInterface $dataLogger,
  27.         EventDispatcherInterface $eventDispatcher
  28.     ) {
  29.         $this->tournamentRepository $tournamentRepository;
  30.         $this->logger $dataLogger;
  31.         $this->eventDispatcher $eventDispatcher;
  32.     }
  33.     /**
  34.      * @Route("/ping/{apiKey}", name="ping", methods={"GET", "POST"})
  35.      *
  36.      * @param Request $request
  37.      * @param string $apiKey
  38.      *
  39.      * @return Response
  40.      */
  41.     public function ping(Request $requeststring $apiKey): Response
  42.     {
  43.         $tournament $this->tournamentRepository->findOneByApiKey($apiKey);
  44.         if ($tournament === null) {
  45.             return new JsonResponse('Forbidden'Response::HTTP_FORBIDDEN);
  46.         }
  47.         return new Response();
  48.     }
  49.     /**
  50.      * @Route("/competitions/{apiKey}", name="competitions", methods={"GET", "POST"})
  51.      *
  52.      * @param Request $request
  53.      * @param string $apiKey
  54.      *
  55.      * @return JsonResponse
  56.      */
  57.     public function processCompetitions(Request $requeststring $apiKey): JsonResponse
  58.     {
  59.         $tournament $this->tournamentRepository->findOneByApiKey($apiKey);
  60.         if ($tournament === null) {
  61.             return new JsonResponse('Forbidden'Response::HTTP_FORBIDDEN);
  62.         }
  63.         $data $request->request->get('data');
  64.         $parsedData json_decode($data);
  65.         foreach ($parsedData as $dataItem) {
  66.             $this->eventDispatcher->dispatch(new CreateCompetitionEvent($tournament$dataItem));
  67.         }
  68.         return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
  69.     }
  70.     /**
  71.      * @Route("/launched-matches/{apiKey}", name="launched_matches", methods={"GET", "POST"})
  72.      *
  73.      * @param Request $request
  74.      * @param string $apiKey
  75.      *
  76.      * @return JsonResponse
  77.      */
  78.     public function processLaunchedMatches(Request $requeststring $apiKey): JsonResponse
  79.     {
  80.         $tournament $this->tournamentRepository->findOneByApiKey($apiKey);
  81.         if ($tournament === null) {
  82.             return new JsonResponse('Forbidden'Response::HTTP_FORBIDDEN);
  83.         }
  84.         $data $request->request->get('data');
  85.         $parsedData json_decode($data);
  86.         $this->logger->debug(
  87.             'FAST log',
  88.             ['fast_data' => $parsedData]
  89.         );
  90.         $this->eventDispatcher->dispatch(new CreateMatchEvent($tournament$parsedData));
  91.         return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
  92.     }
  93.     /**
  94.      * @Route("/competition-registration/{apiKey}", name="competition_registration", methods={"GET", "POST"})
  95.      *
  96.      * @param Request $request
  97.      * @param string $apiKey
  98.      *
  99.      * @return JsonResponse
  100.      */
  101.     public function processCompetitionRegistrations(Request $requeststring $apiKey): JsonResponse
  102.     {
  103.         $tournament $this->tournamentRepository->findOneByApiKey($apiKey);
  104.         if ($tournament === null) {
  105.             return new JsonResponse('Forbidden'Response::HTTP_FORBIDDEN);
  106.         }
  107.         $data $request->request->get('data');
  108.         $parsedData json_decode($data);
  109.         $this->logger->debug(
  110.             'FAST log',
  111.             ['fast_data' => $parsedData]
  112.         );
  113.         $this->eventDispatcher->dispatch(new CreateTeamEvent($tournament$parsedData));
  114.         return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
  115.     }
  116.     /**
  117.      * @Route("/competition-ranking/{apiKey}", name="competition_ranking", methods={"GET", "POST"})
  118.      *
  119.      * @param Request $request
  120.      * @param string $apiKey
  121.      *
  122.      * @return JsonResponse
  123.      */
  124.     public function processCompetitionRanking(Request $requeststring $apiKey): JsonResponse
  125.     {
  126.         $tournament $this->tournamentRepository->findOneByApiKey($apiKey);
  127.         if ($tournament === null) {
  128.             return new JsonResponse('Forbidden'Response::HTTP_FORBIDDEN);
  129.         }
  130.         $data $request->request->get('data');
  131.         $parsedData json_decode($data);
  132.         $this->eventDispatcher->dispatch(new CreateCompetitionRankingEvent($tournament$parsedData));
  133.         return new JsonResponse('OK'); // TODO: return success probably with some meaningful message.
  134.     }
  135. }