vendor/shopware/core/Content/Category/SalesChannel/TreeBuildingNavigationRoute.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\Routing\Annotation\Entity;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\Framework\Routing\Annotation\Since;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route(defaults={"_routeScope"={"store-api"}})
  14.  */
  15. class TreeBuildingNavigationRoute extends AbstractNavigationRoute
  16. {
  17.     private AbstractNavigationRoute $decorated;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(AbstractNavigationRoute $decorated)
  22.     {
  23.         $this->decorated $decorated;
  24.     }
  25.     public function getDecorated(): AbstractNavigationRoute
  26.     {
  27.         return $this->decorated;
  28.     }
  29.     /**
  30.      * @Since("6.2.0.0")
  31.      * @Entity("category")
  32.      * @Route("/store-api/navigation/{activeId}/{rootId}", name="store-api.navigation", methods={"GET", "POST"})
  33.      */
  34.     public function load(string $activeIdstring $rootIdRequest $requestSalesChannelContext $contextCriteria $criteria): NavigationRouteResponse
  35.     {
  36.         $activeId $this->resolveAliasId($activeId$context->getSalesChannel());
  37.         $rootId $this->resolveAliasId($rootId$context->getSalesChannel());
  38.         $response $this->getDecorated()->load($activeId$rootId$request$context$criteria);
  39.         $buildTree $request->query->getBoolean('buildTree'$request->request->getBoolean('buildTree'true));
  40.         if (!$buildTree) {
  41.             return $response;
  42.         }
  43.         $categories $this->buildTree($rootId$response->getCategories()->getElements());
  44.         return new NavigationRouteResponse($categories);
  45.     }
  46.     private function buildTree(?string $parentId, array $categories): CategoryCollection
  47.     {
  48.         $children = new CategoryCollection();
  49.         foreach ($categories as $key => $category) {
  50.             if ($category->getParentId() !== $parentId) {
  51.                 continue;
  52.             }
  53.             unset($categories[$key]);
  54.             $children->add($category);
  55.         }
  56.         $children->sortByPosition();
  57.         $items = new CategoryCollection();
  58.         foreach ($children as $child) {
  59.             if (!$child->getActive() || !$child->getVisible()) {
  60.                 continue;
  61.             }
  62.             $child->setChildren($this->buildTree($child->getId(), $categories));
  63.             $items->add($child);
  64.         }
  65.         return $items;
  66.     }
  67.     private function resolveAliasId(string $idSalesChannelEntity $salesChannelEntity): string
  68.     {
  69.         switch ($id) {
  70.             case 'main-navigation':
  71.                 return $salesChannelEntity->getNavigationCategoryId();
  72.             case 'service-navigation':
  73.                 if ($salesChannelEntity->getServiceCategoryId() === null) {
  74.                     throw new \RuntimeException(\sprintf('Service category, for sales channel %s, is not set'$salesChannelEntity->getTranslation('name')));
  75.                 }
  76.                 return $salesChannelEntity->getServiceCategoryId();
  77.             case 'footer-navigation':
  78.                 if ($salesChannelEntity->getFooterCategoryId() === null) {
  79.                     throw new \RuntimeException(\sprintf('Footer category, for sales channel %s, is not set'$salesChannelEntity->getTranslation('name')));
  80.                 }
  81.                 return $salesChannelEntity->getFooterCategoryId();
  82.             default:
  83.                 return $id;
  84.         }
  85.     }
  86. }