<?php
namespace App\Controller\Frontend;
use App\Controller\Controller;
use App\Twig\Extension\CountryExtension;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CountryController extends Controller
{
// 3600×24×7 = 604800 seconds
public const ONE_WEEK = 604800;
public const COOKIE_NAME = 'dont-change-country';
public function dontChangeCountryAction(): Response
{
$response = new JsonResponse(['result' => 1]);
$response->headers->setCookie(Cookie::create(self::COOKIE_NAME, 1, time() + self::ONE_WEEK));
return $response;
}
public function redirectToCountry(Request $request, CountryExtension $countryExtension)
{
$scheme = $request->getScheme();
$country = $request->get('country', 'fr');
$domain = $countryExtension->getCountryDomain($country);
$route = $request->get('route', 'homepage');
$params = $request->get('params', []);
// switch language in order to avoid having a language prefix
$params['_locale'] = $countryExtension->getCountryDefaultLocale($country);
$url = $scheme.'://'.$domain.$countryExtension->getRouteForCountry($country, $route, $params);
return new RedirectResponse($url, Response::HTTP_PERMANENTLY_REDIRECT);
}
}