<?php declare(strict_types=1);
namespace Nd\Fashion\Page\Manufacturer;
use Nd\Fashion\Extension\Content\Product\Aggregate\ProductManufacturer\ProductPageExtension\ProductPageExtension;
use Nd\Fashion\Extension\Content\Product\Aggregate\ProductManufacturer\ProductPageExtension\ProductPageExtensionEntity;
use Nd\Fashion\Page\Manufacturer\Exception\ProductManufacturerNotFoundException;
use Nd\Fashion\Service\ProductManufacturerUrlService;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerEntity;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\System\Annotation\Concept\ExtensionPattern\Decoratable;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @Decoratable()
*/
class ProductManufacturerPageLoader implements ProductManufacturerPageLoaderInterface
{
private GenericPageLoaderInterface $genericLoader;
private $cmsPageRoute;
private SeoUrlPlaceholderHandlerInterface $seoUrlReplacer;
private ProductManufacturerUrlService $productManufacturerUrlService;
/**
* @internal
*/
public function __construct(
GenericPageLoaderInterface $genericLoader,
AbstractProductManufacturerRoute $cmsPageRoute,
SeoUrlPlaceholderHandlerInterface $seoUrlReplacer,
ProductManufacturerUrlService $productManufacturerUrlService
) {
$this->genericLoader = $genericLoader;
$this->cmsPageRoute = $cmsPageRoute;
$this->seoUrlReplacer = $seoUrlReplacer;
$this->productManufacturerUrlService = $productManufacturerUrlService;
}
public function load(Request $request, SalesChannelContext $context): ProductManufacturerPage
{
$page = $this->genericLoader->load($request, $context);
$page = ProductManufacturerPage::createFrom($page);
$productManufacturerId = $request->get('productManufacturerId');
if(!$productManufacturerId){
throw new ProductManufacturerNotFoundException($productManufacturerId);
}
$page->setProductManufacturerId($productManufacturerId);
$productManufacturer = $this->cmsPageRoute
->load($productManufacturerId, $request, $context)
->getProductManufacturer();
if ( $productManufacturer->getExtension('productPageExtension')->getCmsPageType() == "none" ) {
throw new ProductManufacturerNotFoundException($productManufacturer->getId());
}
if($productManufacturer->getExtension('productPageExtension')->getCmsPageType() == "general") {
header("Location: ". $this->productManufacturerUrlService->retrieveGeneralListingUrl($productManufacturer, $context));
die();
}
if ($productManufacturer->getExtension('productPageExtension')->getCmsPage()) {
$this->loadMetaData($productManufacturer->getExtension('productPageExtension'), $page, $context->getSalesChannel());
$page->setCmsPage($productManufacturer->getExtension('productPageExtension')->getCmsPage());
}else{
header("Location: ". $this->productManufacturerUrlService->retrieveGeneralListingUrl($productManufacturer, $context));
die();
}
if ($page->getMetaInformation()) {
$canonical = $this->seoUrlReplacer->generate('frontend.productManufacturer.detail', ['productManufacturerId' => $productManufacturerId]);
$page->getMetaInformation()->assign(['canonical' => $canonical]);
}
return $page;
}
private function loadMetaData(ProductPageExtensionEntity $productPageExtension, ProductManufacturerPage $page, SalesChannelEntity $salesChannel): void
{
$metaInformation = $page->getMetaInformation();
if ($metaInformation === null) {
return;
}
$metaDescription = $productPageExtension->getTranslation('metaDescription');
$metaInformation->setMetaDescription((string) $metaDescription);
$metaTitle = $productPageExtension->getTranslation('metaTitle');
$metaInformation->setMetaTitle((string) $metaTitle);
$keywords =$productPageExtension->getTranslation('keywords');
$metaInformation->setMetaKeywords((string) $keywords);
}
}