<?php
namespace App\Manager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Language;
class LanguageManager {
private $em;
private $session;
private $router;
protected $currentLanguage = 'fr';
public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Symfony\Component\Routing\RouterInterface $router)
{
$this->em = $em;
$this->session = $session;
$this->router = $router;
$this->currentLanguage = $this->session->get('_locale');
}
public function getByCode($code)
{
return $this->em->getRepository('App:Language')->findOneByCode($code);
}
public function switchTo($code)
{
$this->currentLanguage = $code;
$this->session->set('language', $code);
}
public function getCurrentLanguage()
{
return $this->currentLanguage;
}
public function getCurrent()
{
return $this->getCurrentLanguage();
}
public function getProductUrl($id, $lang = 'fr')
{
$product = $this->em->getRepository('App:Product')->find($id);
if($product){
$decription = $product->getProductDescription($lang);
return $this->router->generate('product',[
'id'=>$product->getId(),
'url'=>$decription->getUrl()
]);
}
return false;
}
public function getCategoryUrl($id, $lang = 'fr')
{
$category= $this->em->getRepository('App:Category')->find($id);
if($category){
$decription = $category->getCategoryDescription($lang);
$route = 'category';
if($category->isRoot()){
$route = 'universe';
}
return $this->router->generate($route,[
'id' => $category->getId(),
'url' => $decription->getUrl()
]);
}
return false;
}
}