<?php
namespace App\Controller\Front;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Language;
use App\Manager\System\BrandLanguageManager;
use App\Manager\System\ProductManager;
use App\Manager\System\TagManager;
use App\Manager\System\TaxonomyLanguageManager;
use App\Service\LegacyControllerService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ShopController extends AbstractController
{
private TaxonomyLanguageManager $taxonomyLanguageManager;
private BrandLanguageManager $brandLanguageManager;
private TagManager $tagManager;
private LegacyControllerService $legacyControllerService;
private SessionService $sessionService;
private ProductManager $productManager;
public function __construct(
LegacyControllerService $legacyControllerService,
TaxonomyLanguageManager $taxonomyLanguageManager,
BrandLanguageManager $brandLanguageManager,
TagManager $tagManager,
SessionService $sessionService,
ProductManager $productManager
) {
$this->taxonomyLanguageManager = $taxonomyLanguageManager;
$this->brandLanguageManager = $brandLanguageManager;
$this->tagManager = $tagManager;
$this->legacyControllerService = $legacyControllerService;
$this->sessionService = $sessionService;
$this->productManager = $productManager;
}
/**
* @Route("/{lang}/shop/category/{url}", name="shop_category", requirements={"lang"="[a-zA-Z]{2}", "url"="^((?!rest/).)*$"})
*/
public function shopCategory(Request $request, string $url): Response
{
$taxonomyLanguage = $this->taxonomyLanguageManager->findOneByLinkRewriteAndLanguage(
$url,
$request->getSession()->get('id_lang', Language::ENGLISH_LANGUAGE_ID)
);
if (!$taxonomyLanguage) {
throw $this->createNotFoundException();
}
try {
$this->sessionService->set('controller', 'taxonomyController');
$this->sessionService->set('id_model', $taxonomyLanguage->getTaxonomy()->getId());
$legacyController = new \taxonomyController($taxonomyLanguage->getTaxonomy()->getId());
} catch (\Throwable $t) {
throw $this->createNotFoundException();
}
$legacyController->view($taxonomyLanguage->getTaxonomy()->getId());
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
/**
* @Route("/{lang}/shop/brand/{url}", name="shop_brand", requirements={"lang"="[a-zA-Z]{2}", "url"="^((?!rest/).)*$"})
*/
public function shopBrand(Request $request, string $url): Response
{
$brandLanguage = $this->brandLanguageManager->findOneByLinkRewriteAndLanguage(
$url,
$request->getSession()->get('id_lang', Language::ENGLISH_LANGUAGE_ID)
);
if (!$brandLanguage) {
throw $this->createNotFoundException();
}
try {
$legacyController = new \manufacturerController();
$this->sessionService->set('controller', 'manufacturerController');
$this->sessionService->set('id_model', $brandLanguage->getBrand()->getId());
} catch (\Throwable $t) {
throw $this->createNotFoundException();
}
$legacyController->view($brandLanguage->getBrand()->getId());
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
/**
* @Route("/{lang}/shop/tag/{url}", name="shop_tag", requirements={"lang"="[a-zA-Z]{2}", "url"="^((?!rest/).)*$"})
*/
public function shopTag(Request $request, string $url): Response
{
$url = trim($url, '/');
$tag = $this->tagManager->findOneByLinkRewriteAndLanguage(
$url,
$request->getSession()->get('id_lang', Language::ENGLISH_LANGUAGE_ID)
);
if (!$tag) {
throw $this->createNotFoundException();
}
try {
$legacyController = new \tagController();
$this->sessionService->set('controller', 'tagController');
} catch (\Throwable $t) {
throw $this->createNotFoundException();
}
$legacyController->view($url);
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
/**
* @Route("/{lang}/shop/product/{url}", name="shop_product", requirements={"lang"="[a-zA-Z]{2}", "url"="^((?!rest/).)*$"})
*/
public function shopProduct(Request $request, string $url): Response
{
$url = trim($url, '/');
$lang = $request->getSession()->get('lang', Language::ENGLISH_ISO);
$product = $this->productManager->findOneByLinkRewriteAndLanguage(
$url,
$lang
);
if (!$product) {
throw $this->createNotFoundException();
}
if ($product->isVirtual()) {
return $this->redirectToRoute('shop_service', ['url' => $url, 'lang' => $lang]);
}
try {
$legacyController = new \productController($product->getId());
$this->sessionService->set('controller', 'productController');
$this->sessionService->set('id_model', $product->getId());
} catch (\Throwable $t) {
throw $this->createNotFoundException();
}
$legacyController->view();
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
/**
* @Route("/{lang}/shop/service/{url}", name="shop_service", requirements={"lang"="[a-zA-Z]{2}", "url"="^((?!rest/).)*$"})
*/
public function shopService(Request $request, string $url): Response
{
$url = trim($url, '/');
$product = $this->productManager->findOneByLinkRewriteAndLanguage(
$url,
$request->getSession()->get('lang', Language::ENGLISH_ISO)
);
if (!$product || !$product->isVirtual()) {
throw $this->createNotFoundException();
}
try {
$legacyController = new \productController($product->getId());
$this->sessionService->set('controller', 'productController');
$this->sessionService->set('id_model', $product->getId());
} catch (\Throwable $t) {
throw $this->createNotFoundException();
}
$legacyController->view();
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
}