src/Manager/RedirectionManager.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use App\Entity\Category;
  6. use App\Entity\Product;
  7. use App\Entity\Redirection;
  8. class RedirectionManager {
  9. private $em;
  10. private $router;
  11. public function __construct(EntityManagerInterface $em, \Symfony\Component\Routing\RouterInterface $router)
  12. {
  13. $this->em = $em;
  14. $this->router = $router;
  15. }
  16. public function getRedirectUrl(Redirection $redirect) {
  17. $toUrl = $redirect->getToUrl();
  18. if(!empty($toUrl)){
  19. return $this->translateUrl($toUrl);
  20. }
  21. $category = $redirect->getToCategory();
  22. if($category){
  23. $route = 'universe';
  24. if($category->getParent()){
  25. $route = 'category';
  26. }
  27. return $this->router->generate($route, ['id'=>$category->getId(), 'url'=>$category->getUrl()]);
  28. }
  29. $product = $redirect->getToProduct();
  30. if($product){
  31. return $this->router->generate('product', ['id'=>$product->getId(), 'url'=>$product->getUrl()]);
  32. }
  33. $targetPath = $redirect->getTargetPath();
  34. if($targetPath){
  35. $tmp = explode('_',$targetPath);
  36. $category = $this->em->getRepository('App:Category')->find($tmp[count($tmp)-1]);
  37. if($category){
  38. $route = 'universe';
  39. if($category->getParent()){
  40. $route = 'category';
  41. }
  42. return $this->router->generate($route, ['id'=>$category->getId(), 'url'=>$category->getUrl()]);
  43. }
  44. }
  45. return $this->router->generate('index');
  46. }
  47. public function redirectCategoryToCategory(Category $categoryFrom, Category $categoryTo) {
  48. $this->removeByCategory($categoryFrom);
  49. $redirect = new Redirection();
  50. $redirect->setFromCategory($categoryFrom);
  51. $redirect->setToCategory($categoryTo);
  52. $this->em->persist($redirect);
  53. $this->em->flush();
  54. }
  55. public function delete(Redirection $redirect) {
  56. $this->em->remove($redirect);
  57. $this->em->flush();
  58. }
  59. public function removeByCategory(Category $category) {
  60. $categories = $this->em->getRepository('App:Redirection')->findByFromCategory($category);
  61. foreach($categories as $cat){
  62. $this->em->remove($cat);
  63. }
  64. $this->em->flush();
  65. }
  66. public function translateUrl($url) {
  67. if(strpos($url, 'product_info.php?products_id=')!==false){
  68. $urlInfos = parse_url($url);
  69. $params = [];
  70. parse_str($urlInfos['query'], $params);
  71. $product = $this->em->getRepository('App:Product')->find($params['products_id']);
  72. if($product){
  73. $description = $product->getProductDescription($params['language']);
  74. return $this->router->generate('product', [
  75. '_locale' => $params['language'],
  76. 'id' => $product->getId(),
  77. 'url' => $description->getUrl()
  78. ]);
  79. }
  80. }
  81. if(strpos($url, 'index.php?cPath=')!==false){
  82. $urlInfos = parse_url($url);
  83. $params = [];
  84. parse_str($urlInfos['query'], $params);
  85. $tmp = explode('_',$params['cPath']);
  86. $category = $this->em->getRepository('App:Category')->find(array_pop($tmp));
  87. if($category){
  88. $description = $category->getCategoryDescription($params['language']);
  89. return $this->router->generate('category', [
  90. '_locale' => $params['language'],
  91. 'id' => $category->getId(),
  92. 'url' => $description->getUrl()
  93. ]);
  94. }
  95. }
  96. if(strpos($url, '/professionnel/index.php')!==false)
  97. return $this->router->generate('index');
  98. return $url;
  99. }
  100. }