<?php declare(strict_types=1);
namespace Nd\Stl\Storefront\Page\Look;
use Nd\Stl\Core\Content\Look\AbstractLookRoute;
use Nd\Stl\Core\Content\Look\Exception\LookNotFoundException;
use Nd\Stl\Core\Content\Look\LookEntity;
use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @Decoratable()
*/
class LookPageLoader implements LookPageLoaderInterface
{
/**
* @var GenericPageLoaderInterface
*/
private $genericLoader;
/**
* @var AbstractCategoryRoute
*/
private $cmsPageRoute;
/**
* @var SeoUrlPlaceholderHandlerInterface
*/
private $seoUrlReplacer;
/**
* @internal
*/
public function __construct(
GenericPageLoaderInterface $genericLoader,
AbstractLookRoute $cmsPageRoute,
SeoUrlPlaceholderHandlerInterface $seoUrlReplacer
) {
$this->genericLoader = $genericLoader;
$this->cmsPageRoute = $cmsPageRoute;
$this->seoUrlReplacer = $seoUrlReplacer;
}
public function load(Request $request, SalesChannelContext $context): LookPage
{
$page = $this->genericLoader->load($request, $context);
$page = LookPage::createFrom($page);
$lookId = $request->get('lookId');
if(!$lookId){
throw new LookNotFoundException($lookId);
}
$look = $this->cmsPageRoute
->load($lookId, $request, $context)
->getLook();
// if (!$look->isActive()) {
// throw new LookNotFoundException($look->getId());
// }
$this->loadMetaData($look, $page, $context->getSalesChannel());
if (empty($look->getCmsPage())) {
throw new PageNotFoundException($lookId);
}
$page->setCmsPage($look->getCmsPage());
if ($page->getMetaInformation()) {
$canonical = $this->seoUrlReplacer->generate('frontend.look.detail', ['lookId' => $lookId]);
$page->getMetaInformation()->assign(['canonical' => $canonical]);
}
return $page;
}
private function loadMetaData(LookEntity $look, LookPage $page, SalesChannelEntity $salesChannel): void
{
$metaInformation = $page->getMetaInformation();
if ($metaInformation === null) {
return;
}
$metaDescription = $look->getTranslation('metaDescription');
$metaInformation->setMetaDescription((string) $metaDescription);
$metaTitle = $look->getTranslation('metaTitle');
$metaInformation->setMetaTitle((string) $metaTitle);
$keywords =$look->getTranslation('keywords');
$metaInformation->setMetaKeywords((string) $keywords);
}
}