vendor/shopware/storefront/Pagelet/Header/HeaderPageletLoader.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Pagelet\Header;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
  5. use Shopware\Core\Content\Category\Tree\TreeItem;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  10. use Shopware\Core\System\Annotation\Concept\ExtensionPattern\Decoratable;
  11. use Shopware\Core\System\Currency\SalesChannel\AbstractCurrencyRoute;
  12. use Shopware\Core\System\Language\LanguageCollection;
  13. use Shopware\Core\System\Language\SalesChannel\AbstractLanguageRoute;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Event\RouteRequest\CurrencyRouteRequestEvent;
  16. use Shopware\Storefront\Event\RouteRequest\LanguageRouteRequestEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. /**
  20.  * @Decoratable()
  21.  */
  22. class HeaderPageletLoader implements HeaderPageletLoaderInterface
  23. {
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     private AbstractCurrencyRoute $currencyRoute;
  26.     private AbstractLanguageRoute $languageRoute;
  27.     private NavigationLoaderInterface $navigationLoader;
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(
  32.         EventDispatcherInterface $eventDispatcher,
  33.         AbstractCurrencyRoute $currencyRoute,
  34.         AbstractLanguageRoute $languageRoute,
  35.         NavigationLoaderInterface $navigationLoader
  36.     ) {
  37.         $this->eventDispatcher $eventDispatcher;
  38.         $this->currencyRoute $currencyRoute;
  39.         $this->languageRoute $languageRoute;
  40.         $this->navigationLoader $navigationLoader;
  41.     }
  42.     /**
  43.      * @throws MissingRequestParameterException
  44.      */
  45.     public function load(Request $requestSalesChannelContext $context): HeaderPagelet
  46.     {
  47.         $salesChannel $context->getSalesChannel();
  48.         $navigationId $request->get('navigationId'$salesChannel->getNavigationCategoryId());
  49.         if (!$navigationId) {
  50.             throw new MissingRequestParameterException('navigationId');
  51.         }
  52.         $languages $this->getLanguages($context$request);
  53.         $event = new CurrencyRouteRequestEvent($request, new Request(), $context);
  54.         $this->eventDispatcher->dispatch($event);
  55.         $navigation $this->navigationLoader->load(
  56.             (string) $navigationId,
  57.             $context,
  58.             $salesChannel->getNavigationCategoryId(),
  59.             $salesChannel->getNavigationCategoryDepth()
  60.         );
  61.         $criteria = new Criteria();
  62.         $criteria->setTitle('header::currencies');
  63.         $currencies $this->currencyRoute
  64.             ->load($event->getStoreApiRequest(), $context$criteria)
  65.             ->getCurrencies();
  66.         $contextLanguage $languages->get($context->getContext()->getLanguageId());
  67.         if (!$contextLanguage) {
  68.             throw new \RuntimeException(sprintf('Context language with id %s not found'$context->getContext()->getLanguageId()));
  69.         }
  70.         $page = new HeaderPagelet(
  71.             $navigation,
  72.             $languages,
  73.             $currencies,
  74.             $contextLanguage,
  75.             $context->getCurrency(),
  76.             $this->getServiceMenu($context)
  77.         );
  78.         $this->eventDispatcher->dispatch(new HeaderPageletLoadedEvent($page$context$request));
  79.         return $page;
  80.     }
  81.     private function getServiceMenu(SalesChannelContext $context): CategoryCollection
  82.     {
  83.         $serviceId $context->getSalesChannel()->getServiceCategoryId();
  84.         if ($serviceId === null) {
  85.             return new CategoryCollection();
  86.         }
  87.         $navigation $this->navigationLoader->load($serviceId$context$serviceId1);
  88.         return new CategoryCollection(array_map(static function (TreeItem $treeItem) {
  89.             return $treeItem->getCategory();
  90.         }, $navigation->getTree()));
  91.     }
  92.     private function getLanguages(SalesChannelContext $contextRequest $request): LanguageCollection
  93.     {
  94.         $criteria = new Criteria();
  95.         $criteria->setTitle('header::languages');
  96.         $criteria->addFilter(
  97.             new EqualsFilter('language.salesChannelDomains.salesChannelId'$context->getSalesChannel()->getId())
  98.         );
  99.         $criteria->addSorting(new FieldSorting('name'FieldSorting::ASCENDING));
  100.         $criteria->addAssociation('productSearchConfig');
  101.         $apiRequest = new Request();
  102.         $event = new LanguageRouteRequestEvent($request$apiRequest$context$criteria);
  103.         $this->eventDispatcher->dispatch($event);
  104.         return $this->languageRoute->load($event->getStoreApiRequest(), $context$criteria)->getLanguages();
  105.     }
  106. }