<?php
namespace App\Controller;
use App\Entity\Country;
use App\Entity\CountryGroup;
use App\Entity\Registered;
use App\Entity\User;
use App\FormType\CarType;
use App\FormType\RegisteredType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomeController extends AbstractController
{
/** @var SessionInterface */
private $session;
private $request;
/** @var EntityManagerInterface */
private $entityManager;
/** @var TranslatorInterface */
private $translator;
/**
* @Route("/instructions", name="instructions")
* @return Response
*/
public function instructions(): Response
{
$params = [];
return $this->render('home/instructions.html.twig', $params);
}
/**
* @Route("/", name="home_redirect")
* @return Response
*/
public function home_redirect(): Response
{
return $this->redirectToRoute('home');
}
/**
* @Route("/registration/{locale}", name="change_locale")
*/
public function changeLocale($locale, Request $request)
{
// On stocke la langue dans la session
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
// On revient sur la page précédente
return $this->redirect($this->generateUrl('home'));
}
/**
* @Route("/registration", name="home")
* @param SessionInterface $session
* @param Request $request
* @param EntityManagerInterface $entityManager
* @param TranslatorInterface $translator
* @return Response
* @throws \Exception
*/
public function index(
SessionInterface $session,
Request $request,
EntityManagerInterface $entityManager,
TranslatorInterface $translator
): Response {
$this->session = $session;
$this->request = $request;
$this->entityManager = $entityManager;
$this->translator = $translator;
$countryGroup = $request->getSession()->get('_locale');
/** @var User $user */
$user = $this->getUser();
$params = [
"hasSteamId" => false,
"isRegistered" => false
];
if ($user) {
$params["hasSteamId"] = !is_null($user->getSteamId());
$params["isRegistered"] = !is_null($user->getRegistered());
$params["countryGroup"] = ($user->getRegistered() && $user->getCountry()) ? $user->getCountry()->getCountryGroup() : null;
if(is_null($user->getRegistered())) {
$form = $this->manageForm($countryGroup);
if($form === true) {
$params["isRegistered"] = true;
$params["successRegistered"] = true;
$params["registered"] = new \DateTime();
$params["form_car"] = $this->getFormCar($user->getRegistered());
$params["date_limit_change_car"] = $this->getLimitDateChangerCar($user->getCountry()->getCountryGroup());
}
$params["form"] = $form;
} else {
$params["registered"] = $user->getRegistered()->getCreatedAt();
$params["form_car"] = $this->getFormCar($user->getRegistered());
$params["date_limit_change_car"] = $this->getLimitDateChangerCar($user->getCountry()->getCountryGroup());
}
}
return $this->render('home/index.html.twig', $params);
}
public function manageForm($countryGroup)
{
$oRegistered = new Registered();
$options = [];
$oCountry = $this->entityManager->getRepository(Country::class)->findOneBy([
'identifier' => $countryGroup
]);
if ($oCountry) {
$options['country'] = $oCountry;
}
$form = $this->createForm(RegisteredType::class, $oRegistered, $options);
$form->handleRequest($this->request);
if($form->isSubmitted() && $form->isValid()) {
/** @var User $user */
$user = $this->getUser();
$user->setCountry($form->get("country")->getData());
if ($this->isPassedDate($user)) {
$form->addError(new FormError($this->translator->trans("You cannot anymore join the event")));
return $form->createView();
}
$this->entityManager->persist($user);
/** @var Registered $oRegistered */
$oRegistered = $form->getData();
$oRegistered->setUser($user);
$oRegistered->setCreatedAt(new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
$this->entityManager->persist($oRegistered);
$this->entityManager->flush();
$this->addFlash("success", $this->translator->trans("Your registration for the Preadator Race Cup 2022 was successful!"));
return true;
}
return $form->createView();
}
public function getFormCar($oRegistered)
{
$form = $this->createForm(CarType::class, $oRegistered);
$form->handleRequest($this->request);
if ($this->isPassedDate($this->getUser())) {
$form->addError(new FormError($this->translator->trans("You cannot anymore change your car")));
}
if($form->isSubmitted() && $form->isValid()) {
/** @var Registered $oRegistered */
$oRegistered = $form->getData();
$this->entityManager->persist($oRegistered);
$this->entityManager->flush();
$this->addFlash("updated", $this->translator->trans("You car has been updated"));
}
return $form->createView();
}
/**
* @param User $user
* @return bool
* @throws \Exception
*/
private function isPassedDate(User $user)
{
$countryGroup = $user->getCountry()->getCountryGroup();
if ($countryGroup) {
$now = new \DateTime('now', new \DateTimeZone('UTC'));
$limit = $this->getLimitDateChangerCar($countryGroup);
if ($now > $limit) {
return true;
}
}
return false;
}
/**
* @param CountryGroup $countryGroup
* @return mixed
* @throws \Exception
*/
private function getLimitDateChangerCar(CountryGroup $countryGroup)
{
$interval = getenv("SECONDS_BEFORE_QUALIF");
$limit = new \DateTime('now', new \DateTimeZone('UTC'));
if ($countryGroup->getQualifEndDate()) {
$limit = clone $countryGroup->getQualifEndDate();
} else {
return $limit;
}
return $limit->sub(new \DateInterval("PT{$interval}S"));
}
}