<?php
namespace App\Controller\Front;
use App\Entity\User;
use App\Factory\UserExtensionFactory;
use App\Services\DTV\YamlConfig\YamlReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CommonController extends AbstractController
{
private YamlReader $yamlReader;
private EntityManagerInterface $em;
/**
* @param YamlReader $yamlReader
* @param EntityManagerInterface $em
*/
public function __construct( YamlReader $yamlReader, EntityManagerInterface $em )
{
$this->yamlReader = $yamlReader;
$this->em = $em;
}
/**
* @return Response
*/
public function cgvShow(): Response
{
return $this->render( 'front/common/cgv.html.twig' );
}
/**
* @return Response
*/
public function cguShow(): Response
{
return $this->render( 'front/common/cgu.html.twig' );
}
/**
* @return Response
*/
public function cssCustom(): Response
{
$response = $this->render( 'css/front.css.twig' );
$response->headers->set( 'Content-Type', 'text/css' );
return $response;
}
/**
* @return Response
*/
public function mentionLegaleShow(): Response
{
return $this->render( 'front/common/mention_legale.html.twig' );
}
/**
* @param Request $request
* @param string $locale
*
* @return RedirectResponse
*/
public function switchLanguage( Request $request, string $locale ): RedirectResponse
{
$global = $this->yamlReader->getGlobal();
// la traduction n'est pas active
if ( !isset( $global[ 'language' ][ 'enabled' ] ) && $global[ 'language' ][ 'enabled' ] ) {
return $this->redirectToRoute( 'front_homepage' );
}
/** @var User $currentUser */
$currentUser = $this->getUser();
// la variable passée existe dans la configuration
if ( in_array( strtolower( $locale ), $global[ 'language' ][ 'available' ], TRUE ) ) {
$currentUser->addExtension( UserExtensionFactory::setLanguage( $currentUser, strtolower( $locale ) ) );
$this->em->flush();
}
return $this->redirect( $request->server->get( 'HTTP_REFERER' ) );
}
}