src/Controller/HomeController.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Country;
  4. use App\Entity\CountryGroup;
  5. use App\Entity\Registered;
  6. use App\Entity\User;
  7. use App\FormType\CarType;
  8. use App\FormType\RegisteredType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class HomeController extends AbstractController
  19. {
  20.     /** @var SessionInterface */
  21.     private $session;
  22.     private $request;
  23.     /** @var EntityManagerInterface */
  24.     private $entityManager;
  25.     /** @var TranslatorInterface */
  26.     private $translator;
  27.     /**
  28.      * @Route("/instructions", name="instructions")
  29.      * @return Response
  30.      */
  31.     public function instructions(): Response
  32.     {
  33.         $params = [];
  34.         return $this->render('home/instructions.html.twig'$params);
  35.     }
  36.     /**
  37.      * @Route("/", name="home_redirect")
  38.      * @return Response
  39.      */
  40.     public function home_redirect(): Response
  41.     {
  42.         return $this->redirectToRoute('home');
  43.     }
  44.     /**
  45.      * @Route("/registration/{locale}", name="change_locale")
  46.      */
  47.     public function changeLocale($localeRequest $request)
  48.     {
  49.         // On stocke la langue dans la session
  50.         $request->getSession()->set('_locale'$locale);
  51.         $request->setLocale($locale);
  52.         // On revient sur la page précédente
  53.         return $this->redirect($this->generateUrl('home'));
  54.     }
  55.     /**
  56.      * @Route("/registration", name="home")
  57.      * @param SessionInterface $session
  58.      * @param Request $request
  59.      * @param EntityManagerInterface $entityManager
  60.      * @param TranslatorInterface $translator
  61.      * @return Response
  62.      * @throws \Exception
  63.      */
  64.     public function index(
  65.         SessionInterface $session,
  66.         Request $request,
  67.         EntityManagerInterface $entityManager,
  68.         TranslatorInterface $translator
  69.     ): Response {
  70.         $this->session $session;
  71.         $this->request $request;
  72.         $this->entityManager $entityManager;
  73.         $this->translator $translator;
  74.         $countryGroup $request->getSession()->get('_locale');
  75.         /** @var User $user */
  76.         $user $this->getUser();
  77.         $params = [
  78.             "hasSteamId" => false,
  79.             "isRegistered" => false
  80.         ];
  81.         if ($user) {
  82.             $params["hasSteamId"] = !is_null($user->getSteamId());
  83.             $params["isRegistered"] = !is_null($user->getRegistered());
  84.             $params["countryGroup"] = ($user->getRegistered() && $user->getCountry()) ? $user->getCountry()->getCountryGroup() : null;
  85.             if(is_null($user->getRegistered())) {
  86.                 $form $this->manageForm($countryGroup);
  87.                 if($form === true) {
  88.                     $params["isRegistered"] = true;
  89.                     $params["successRegistered"] = true;
  90.                     $params["registered"] = new \DateTime();
  91.                     $params["form_car"] = $this->getFormCar($user->getRegistered());
  92.                     $params["date_limit_change_car"] = $this->getLimitDateChangerCar($user->getCountry()->getCountryGroup());
  93.                 }
  94.                 $params["form"] = $form;
  95.             } else {
  96.                 $params["registered"] = $user->getRegistered()->getCreatedAt();
  97.                 $params["form_car"] = $this->getFormCar($user->getRegistered());
  98.                 $params["date_limit_change_car"] = $this->getLimitDateChangerCar($user->getCountry()->getCountryGroup());
  99.             }
  100.         }
  101.         return $this->render('home/index.html.twig'$params);
  102.     }
  103.     public function manageForm($countryGroup)
  104.     {
  105.         $oRegistered = new Registered();
  106.         $options = [];
  107.         $oCountry $this->entityManager->getRepository(Country::class)->findOneBy([
  108.             'identifier' =>  $countryGroup
  109.         ]);
  110.         if ($oCountry) {
  111.             $options['country'] = $oCountry;
  112.         }
  113.         $form $this->createForm(RegisteredType::class, $oRegistered$options);
  114.         $form->handleRequest($this->request);
  115.         if($form->isSubmitted() && $form->isValid()) {
  116.             /** @var User $user */
  117.             $user $this->getUser();
  118.             $user->setCountry($form->get("country")->getData());
  119.             if ($this->isPassedDate($user)) {
  120.                 $form->addError(new FormError($this->translator->trans("You cannot anymore join the event")));
  121.                 return $form->createView();
  122.             }
  123.             $this->entityManager->persist($user);
  124.             /** @var Registered $oRegistered */
  125.             $oRegistered $form->getData();
  126.             $oRegistered->setUser($user);
  127.             $oRegistered->setCreatedAt(new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
  128.             $this->entityManager->persist($oRegistered);
  129.             $this->entityManager->flush();
  130.             $this->addFlash("success"$this->translator->trans("Your registration for the Preadator Race Cup 2022 was successful!"));
  131.             return true;
  132.         }
  133.         return $form->createView();
  134.     }
  135.     public function getFormCar($oRegistered)
  136.     {
  137.         $form $this->createForm(CarType::class, $oRegistered);
  138.         $form->handleRequest($this->request);
  139.         if ($this->isPassedDate($this->getUser())) {
  140.             $form->addError(new FormError($this->translator->trans("You cannot anymore change your car")));
  141.         }
  142.         if($form->isSubmitted() && $form->isValid()) {
  143.             /** @var Registered $oRegistered */
  144.             $oRegistered $form->getData();
  145.             $this->entityManager->persist($oRegistered);
  146.             $this->entityManager->flush();
  147.             $this->addFlash("updated"$this->translator->trans("You car has been updated"));
  148.         }
  149.         return $form->createView();
  150.     }
  151.     /**
  152.      * @param User $user
  153.      * @return bool
  154.      * @throws \Exception
  155.      */
  156.     private function isPassedDate(User $user)
  157.     {
  158.         $countryGroup $user->getCountry()->getCountryGroup();
  159.         if ($countryGroup) {
  160.             $now = new \DateTime('now', new \DateTimeZone('UTC'));
  161.             $limit $this->getLimitDateChangerCar($countryGroup);
  162.             if ($now $limit) {
  163.                 return true;
  164.             }
  165.         }
  166.         return false;
  167.     }
  168.     /**
  169.      * @param CountryGroup $countryGroup
  170.      * @return mixed
  171.      * @throws \Exception
  172.      */
  173.     private function getLimitDateChangerCar(CountryGroup $countryGroup)
  174.     {
  175.         $interval getenv("SECONDS_BEFORE_QUALIF");
  176.         $limit = new \DateTime('now', new \DateTimeZone('UTC'));
  177.         if ($countryGroup->getQualifEndDate()) {
  178.             $limit = clone $countryGroup->getQualifEndDate();
  179.         } else {
  180.             return $limit;
  181.         }
  182.         return $limit->sub(new \DateInterval("PT{$interval}S"));
  183.     }
  184. }