custom/plugins/TemplaidInvoices/src/Storefront/Page/Account/TemplaidAssortmentPageLoader.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TemplaidInvoices\Storefront\Page\Account;
  3. use CoeWishlistSw6\Core\Content\Wishlist\WishlistEntity;
  4. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  6. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use TemplaidInvoices\Service\AssortmentService;
  17. class TemplaidAssortmentPageLoader
  18. {
  19.     private GenericPageLoaderInterface $genericLoader;
  20.     private EventDispatcherInterface $eventDispatcher;
  21.     private AssortmentService $assortmentService;
  22.     private ProductListingLoader $listingLoader;
  23.     private TranslatorInterface $translator;
  24.     public function __construct(
  25.         GenericPageLoaderInterface  $genericLoader,
  26.         EventDispatcherInterface    $eventDispatcher,
  27.         AssortmentService           $assortmentService,
  28.         ProductListingLoader        $listingLoader,
  29.         TranslatorInterface         $translator
  30.     ) {
  31.         $this->genericLoader $genericLoader;
  32.         $this->eventDispatcher $eventDispatcher;
  33.         $this->assortmentService $assortmentService;
  34.         $this->listingLoader $listingLoader;
  35.         $this->translator $translator;
  36.     }
  37.     /**
  38.      * @throws InconsistentCriteriaIdsException
  39.      * @throws MissingRequestParameterException
  40.      */
  41.     public function load(Request $requestSalesChannelContext $salesChannelContext): TemplaidAssortmentPage
  42.     {
  43.         $page $this->genericLoader->load($request$salesChannelContext);
  44.         /**
  45.          * @var TemplaidAssortmentPage $page
  46.          */
  47.         $page TemplaidAssortmentPage::createFrom($page);
  48.         if ($page->getMetaInformation()) {
  49.             $page->getMetaInformation()->setRobots('noindex,follow');
  50.         }
  51.         if ($salesChannelContext->getCustomer() && $request->get("listId")) {
  52.             $criteria $this->assortmentService->createAssortmentCriteria($page$salesChannelContext$request);
  53.             $this->eventDispatcher->dispatch(
  54.                 new ProductListingCriteriaEvent($request$criteria$salesChannelContext)
  55.             );
  56.             $products $this->listingLoader->load($criteria$salesChannelContext);
  57.             /** @var ProductListingResult $result */
  58.             $result ProductListingResult::createFrom($products);
  59.             $result->addState(...$products->getStates());
  60.             $this->eventDispatcher->dispatch(
  61.                 new ProductListingResultEvent($request$result$salesChannelContext)
  62.             );
  63.             $result->removeExtension('banners');
  64.             $result->removeExtension('banners_end');
  65.             $page->setAssortmentProducts($result);
  66.             $page->setPageStrings($this->getPageStrings($request$page));
  67.         }
  68.         $this->eventDispatcher->dispatch(
  69.             new TemplaidAssortmentPageLoadedEvent($page$salesChannelContext$request)
  70.         );
  71.         return $page;
  72.     }
  73.     private function getPageStrings(Request $requestTemplaidAssortmentPage $page): array
  74.     {
  75.         /**
  76.          * @var ArrayStruct $lists
  77.          * @var ArrayStruct $properties
  78.          */
  79.         $lists $page->getExtension('assortmentLists');
  80.         $properties $page->getExtension('assortmentProperties');
  81.         if ($request->get('list') === 'tips' && $properties->has($request->get('listId'))) {
  82.             $list $properties->get($request->get('listId'));
  83.             return [
  84.                 'header' => $list['label'],
  85.                 'intro' => ''
  86.             ];
  87.         }
  88.         elseif ($lists->has($request->get('listId'))) {
  89.             /**
  90.              * @var WishlistEntity $list
  91.              */
  92.             $list $lists->get($request->get('listId'));
  93.             return [
  94.                 'header' => $list->getName(),
  95.                 'intro' => ''
  96.             ];
  97.         }
  98.         return [
  99.             'header' => $this->translator->trans('templaidInvoices.account.assortment.assortmentHeader' ucfirst($request->get('listId'))),
  100.             'intro' => $this->translator->trans('templaidInvoices.account.assortment.assortmentIntro' ucfirst($request->get('listId')))
  101.         ];
  102.     }
  103. }