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. private $em;
  7. private $session;
  8. private $router;
  9. protected $currentLanguage = 'fr';
  10. public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Symfony\Component\Routing\RouterInterface $router)
  11. {
  12. $this->em = $em;
  13. $this->session = $session;
  14. $this->router = $router;
  15. $this->currentLanguage = $this->session->get('_locale');
  16. }
  17. public function getByCode($code)
  18. {
  19. return $this->em->getRepository('App:Language')->findOneByCode($code);
  20. }
  21. public function switchTo($code)
  22. {
  23. $this->currentLanguage = $code;
  24. $this->session->set('language', $code);
  25. }
  26. public function getCurrentLanguage()
  27. {
  28. return $this->currentLanguage;
  29. }
  30. public function getCurrent()
  31. {
  32. return $this->getCurrentLanguage();
  33. }
  34. public function getProductUrl($id, $lang = 'fr')
  35. {
  36. $product = $this->em->getRepository('App:Product')->find($id);
  37. if($product){
  38. $decription = $product->getProductDescription($lang);
  39. return $this->router->generate('product',[
  40. 'id'=>$product->getId(),
  41. 'url'=>$decription->getUrl()
  42. ]);
  43. }
  44. return false;
  45. }
  46. public function getCategoryUrl($id, $lang = 'fr')
  47. {
  48. $category= $this->em->getRepository('App:Category')->find($id);
  49. if($category){
  50. $decription = $category->getCategoryDescription($lang);
  51. $route = 'category';
  52. if($category->isRoot()){
  53. $route = 'universe';
  54. }
  55. return $this->router->generate($route,[
  56. 'id' => $category->getId(),
  57. 'url' => $decription->getUrl()
  58. ]);
  59. }
  60. return false;
  61. }
  62. }