<?php declare(strict_types=1);
namespace TemplaidInvoices\Storefront\Page\Account;
use CoeWishlistSw6\Core\Content\Wishlist\WishlistEntity;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use TemplaidInvoices\Service\AssortmentService;
class TemplaidAssortmentPageLoader
{
private GenericPageLoaderInterface $genericLoader;
private EventDispatcherInterface $eventDispatcher;
private AssortmentService $assortmentService;
private ProductListingLoader $listingLoader;
private TranslatorInterface $translator;
public function __construct(
GenericPageLoaderInterface $genericLoader,
EventDispatcherInterface $eventDispatcher,
AssortmentService $assortmentService,
ProductListingLoader $listingLoader,
TranslatorInterface $translator
) {
$this->genericLoader = $genericLoader;
$this->eventDispatcher = $eventDispatcher;
$this->assortmentService = $assortmentService;
$this->listingLoader = $listingLoader;
$this->translator = $translator;
}
/**
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function load(Request $request, SalesChannelContext $salesChannelContext): TemplaidAssortmentPage
{
$page = $this->genericLoader->load($request, $salesChannelContext);
/**
* @var TemplaidAssortmentPage $page
*/
$page = TemplaidAssortmentPage::createFrom($page);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,follow');
}
if ($salesChannelContext->getCustomer() && $request->get("listId")) {
$criteria = $this->assortmentService->createAssortmentCriteria($page, $salesChannelContext, $request);
$this->eventDispatcher->dispatch(
new ProductListingCriteriaEvent($request, $criteria, $salesChannelContext)
);
$products = $this->listingLoader->load($criteria, $salesChannelContext);
/** @var ProductListingResult $result */
$result = ProductListingResult::createFrom($products);
$result->addState(...$products->getStates());
$this->eventDispatcher->dispatch(
new ProductListingResultEvent($request, $result, $salesChannelContext)
);
$result->removeExtension('banners');
$result->removeExtension('banners_end');
$page->setAssortmentProducts($result);
$page->setPageStrings($this->getPageStrings($request, $page));
}
$this->eventDispatcher->dispatch(
new TemplaidAssortmentPageLoadedEvent($page, $salesChannelContext, $request)
);
return $page;
}
private function getPageStrings(Request $request, TemplaidAssortmentPage $page): array
{
/**
* @var ArrayStruct $lists
* @var ArrayStruct $properties
*/
$lists = $page->getExtension('assortmentLists');
$properties = $page->getExtension('assortmentProperties');
if ($request->get('list') === 'tips' && $properties->has($request->get('listId'))) {
$list = $properties->get($request->get('listId'));
return [
'header' => $list['label'],
'intro' => ''
];
}
elseif ($lists->has($request->get('listId'))) {
/**
* @var WishlistEntity $list
*/
$list = $lists->get($request->get('listId'));
return [
'header' => $list->getName(),
'intro' => ''
];
}
return [
'header' => $this->translator->trans('templaidInvoices.account.assortment.assortmentHeader' . ucfirst($request->get('listId'))),
'intro' => $this->translator->trans('templaidInvoices.account.assortment.assortmentIntro' . ucfirst($request->get('listId')))
];
}
}