src/Core/Controller/Admin/CompetitionMatchAdminController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Core\Controller\Admin;
  3. use AndreaSprega\Bundle\BreadcrumbBundle\Annotation\Breadcrumb;
  4. use App\Core\Entity\Competition;
  5. use App\Core\Entity\CompetitionMatch;
  6. use App\Core\Entity\Tournament;
  7. use App\Core\Form\Type\CompetitionMatchFromType;
  8. use App\Core\Repository\CompetitionMatchRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/{_locale}/admin/tournament/{tournament}/competition/{competition}/match", name="app_admin_competition_match_")
  16.  */
  17. class CompetitionMatchAdminController extends AbstractAdminController
  18. {
  19.     private CompetitionMatchRepository $competitionMatchRepository;
  20.     private EntityManagerInterface $entityManager;
  21.     public function __construct(
  22.         CompetitionMatchRepository $competitionMatchRepository,
  23.         EntityManagerInterface $entityManager
  24.     ) {
  25.         $this->competitionMatchRepository $competitionMatchRepository;
  26.         $this->entityManager $entityManager;
  27.     }
  28.     /**
  29.      * @Route("/list", name="list", methods={"GET"})
  30.      * @Breadcrumb({
  31.      *   { "label" = "Home", "route" = "app_admin_home" },
  32.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  33.      *   { "label" = "$tournament.name" },
  34.      *   { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
  35.      *   { "label" = "$competition.name" },
  36.      *   { "label" = "Matches list" },
  37.      * })
  38.      *
  39.      * @param Request $request
  40.      * @param Tournament $tournament
  41.      * @param Competition $competition
  42.      * @return Response
  43.      */
  44.     public function index(Request $requestTournament $tournamentCompetition $competition): Response
  45.     {
  46.         //TODO: fetch all competitions - show a list. Include action buttons for easier access. Think of CRUD actions.
  47.         $matchList $this->competitionMatchRepository->findBy(['competition' => $competition'isLaunched' => 1]);
  48.         //TODO: what if competition is null
  49.         return $this->render(
  50.             'admin/CompetitionMatch/list.html.twig',
  51.             [
  52.                 'user' => $this->getUser(),
  53.                 'match_list' => $matchList,
  54.                 'competition' => $competition,
  55.                 'tournament' => $tournament,
  56.             ]
  57.         );
  58.     }
  59.     /**
  60.      * @Route("/{match}/delete", name="delete", methods={"GET"})
  61.      *
  62.      * @param Request $request
  63.      * @param Tournament $tournament
  64.      * @param Competition $competition
  65.      * @param CompetitionMatch $match
  66.      * @return Response
  67.      */
  68.     public function delete(
  69.         Request $request,
  70.         Tournament $tournament,
  71.         Competition $competition,
  72.         CompetitionMatch $match
  73.     ): Response {
  74.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  75.         if ($redirectIfEditIsDisabled) {
  76.             return $redirectIfEditIsDisabled;
  77.         }
  78.         $this->entityManager->remove($match);
  79.         $this->entityManager->flush();
  80.         return $this->redirectToRoute(
  81.             'app_admin_competition_match_list',
  82.             ['tournament' => $tournament->getId(), 'competition' => $competition->getId()]
  83.         );
  84.     }
  85.     /**
  86.      * @Route("/{match}/edit", name="edit", methods={"GET", "POST"})
  87.      * @Breadcrumb({
  88.      *   { "label" = "Home", "route" = "app_admin_home" },
  89.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  90.      *   { "label" = "$tournament.name" },
  91.      *   { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
  92.      *   { "label" = "$competition.name" },
  93.      *   { "label" = "Matches list", "route" = "app_admin_competition_match_list", "params" = {"tournament" = "$tournament.id", "competition" = "$competition.id"} },
  94.      *   { "label" = "Edit team" }
  95.      * })
  96.      *
  97.      * @param Request $request
  98.      * @param Tournament $tournament
  99.      * @param Competition $competition
  100.      * @param CompetitionMatch $match
  101.      * @return Response
  102.      */
  103.     public function edit(
  104.         Request $request,
  105.         Tournament $tournament,
  106.         Competition $competition,
  107.         CompetitionMatch $match
  108.     ): Response {
  109.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  110.         if ($redirectIfEditIsDisabled) {
  111.             return $redirectIfEditIsDisabled;
  112.         }
  113.         $form $this->createForm(CompetitionMatchFromType::class, $match);
  114.         $form->handleRequest($request);
  115.         $editProcessResponse $this->processCompetitionMatch($form);
  116.         if ($editProcessResponse !== null) {
  117.             return $editProcessResponse;
  118.         }
  119.         return $this->render(
  120.             'admin/CompetitionMatch/edit.html.twig',
  121.             [
  122.                 'form' => $form->createView(),
  123.                 'match' => $match,
  124.                 'tournament' => $competition->getTournament(),
  125.                 'competition' => $competition,
  126.             ]
  127.         );
  128.     }
  129.     private function processCompetitionMatch(FormInterface $form): ?Response
  130.     {
  131.         if ($form->isSubmitted() && $form->isValid()) {
  132.             /** @var CompetitionMatch $match */
  133.             $match $form->getData();
  134.             $this->entityManager->persist($match);
  135.             $this->entityManager->flush();
  136.             $this->addFlash(
  137.                 'notice',
  138.                 "Your have saved a competition match: {$match->getId()} {$match->getDisplayName()}"
  139.             );
  140.             return $this->redirectToRoute(
  141.                 'app_admin_competition_match_list',
  142.                 [
  143.                     'tournament' => $match->getCompetition()->getTournament()->getId(),
  144.                     'competition' => $match->getCompetition()->getId(),
  145.                 ]
  146.             );
  147.         }
  148.         return null;
  149.     }
  150. }