src/Manager/LanguageManager.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Entity\Language;
  5. class LanguageManager {
  6.     
  7.     private $em;
  8.     private $session;
  9.     private $router;
  10.     protected $currentLanguage 'fr';
  11.     public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Symfony\Component\Routing\RouterInterface $router)
  12.     {
  13.         $this->em $em;
  14.         $this->session $session;
  15.         $this->router $router;
  16.         $this->currentLanguage $this->session->get('_locale');
  17.     }
  18.     
  19.     public function getByCode($code)
  20.     {
  21.         return $this->em->getRepository('App:Language')->findOneByCode($code);
  22.     }
  23.     
  24.     public function switchTo($code)
  25.     {
  26.         $this->currentLanguage $code;
  27.         $this->session->set('language'$code);
  28.     }
  29.     
  30.     public function getCurrentLanguage()
  31.     {
  32.         return $this->currentLanguage;
  33.     }
  34.     
  35.     public function getCurrent()
  36.     {
  37.         return $this->getCurrentLanguage();
  38.     }
  39.     
  40.     public function getProductUrl($id$lang 'fr')
  41.     {
  42.         $product $this->em->getRepository('App:Product')->find($id);
  43.         if($product){
  44.             $decription $product->getProductDescription($lang);
  45.             return $this->router->generate('product',[
  46.                 'id'=>$product->getId(),
  47.                 'url'=>$decription->getUrl()
  48.             ]);
  49.         }
  50.         return false;
  51.     }
  52.     
  53.     public function getCategoryUrl($id$lang 'fr')
  54.     {
  55.         $category$this->em->getRepository('App:Category')->find($id);
  56.         if($category){
  57.             $decription $category->getCategoryDescription($lang);
  58.             $route 'category';
  59.             if($category->isRoot()){
  60.                 $route 'universe';
  61.             }
  62.             return $this->router->generate($route,[
  63.                 'id' => $category->getId(),
  64.                 'url' => $decription->getUrl()
  65.             ]);
  66.         }
  67.         return false;
  68.     }
  69.     
  70. }