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

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