vendor/shopware/core/Content/Category/SalesChannel/CategoryRoute.php line 101

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  6. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  7. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  8. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\Log\Package;
  12. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Shopware\Core\Framework\Routing\Annotation\Since;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"store-api"}})
  21.  */
  22. #[Package('content')]
  23. class CategoryRoute extends AbstractCategoryRoute
  24. {
  25.     public const HOME 'home';
  26.     /**
  27.      * @var SalesChannelRepositoryInterface
  28.      */
  29.     private $categoryRepository;
  30.     /**
  31.      * @var SalesChannelCmsPageLoaderInterface
  32.      */
  33.     private $cmsPageLoader;
  34.     /**
  35.      * @var CategoryDefinition
  36.      */
  37.     private $categoryDefinition;
  38.     /**
  39.      * @internal
  40.      */
  41.     public function __construct(
  42.         SalesChannelRepositoryInterface $categoryRepository,
  43.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  44.         CategoryDefinition $categoryDefinition
  45.     ) {
  46.         $this->categoryRepository $categoryRepository;
  47.         $this->cmsPageLoader $cmsPageLoader;
  48.         $this->categoryDefinition $categoryDefinition;
  49.     }
  50.     public function getDecorated(): AbstractCategoryRoute
  51.     {
  52.         throw new DecorationPatternException(self::class);
  53.     }
  54.     /**
  55.      * @Since("6.2.0.0")
  56.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  57.      */
  58.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  59.     {
  60.         if ($navigationId === self::HOME) {
  61.             $navigationId $context->getSalesChannel()->getNavigationCategoryId();
  62.             $request->attributes->set('navigationId'$navigationId);
  63.             $routeParams $request->attributes->get('_route_params', []);
  64.             $routeParams['navigationId'] = $navigationId;
  65.             $request->attributes->set('_route_params'$routeParams);
  66.         }
  67.         $category $this->loadCategory($navigationId$context);
  68.         if (($category->getType() === CategoryDefinition::TYPE_FOLDER
  69.                 || $category->getType() === CategoryDefinition::TYPE_LINK)
  70.             && $context->getSalesChannel()->getNavigationCategoryId() !== $navigationId
  71.         ) {
  72.             throw new CategoryNotFoundException($navigationId);
  73.         }
  74.         $pageId $category->getCmsPageId();
  75.         $slotConfig $category->getTranslation('slotConfig');
  76.         $salesChannel $context->getSalesChannel();
  77.         if ($category->getId() === $salesChannel->getNavigationCategoryId() && $salesChannel->getHomeCmsPageId()) {
  78.             $pageId $salesChannel->getHomeCmsPageId();
  79.             $slotConfig $salesChannel->getTranslation('homeSlotConfig');
  80.         }
  81.         if (!$pageId) {
  82.             return new CategoryRouteResponse($category);
  83.         }
  84.         $resolverContext = new EntityResolverContext($context$request$this->categoryDefinition$category);
  85.         $pages $this->cmsPageLoader->load(
  86.             $request,
  87.             $this->createCriteria($pageId$request),
  88.             $context,
  89.             $slotConfig,
  90.             $resolverContext
  91.         );
  92.         if (!$pages->has($pageId)) {
  93.             throw new PageNotFoundException($pageId);
  94.         }
  95.         $category->setCmsPage($pages->get($pageId));
  96.         $category->setCmsPageId($pageId);
  97.         return new CategoryRouteResponse($category);
  98.     }
  99.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  100.     {
  101.         $criteria = new Criteria([$categoryId]);
  102.         $criteria->setTitle('category::data');
  103.         $criteria->addAssociation('media');
  104.         $category $this->categoryRepository
  105.             ->search($criteria$context)
  106.             ->get($categoryId);
  107.         if (!$category) {
  108.             throw new CategoryNotFoundException($categoryId);
  109.         }
  110.         return $category;
  111.     }
  112.     private function createCriteria(string $pageIdRequest $request): Criteria
  113.     {
  114.         $criteria = new Criteria([$pageId]);
  115.         $criteria->setTitle('category::cms-page');
  116.         $slots $request->get('slots');
  117.         if (\is_string($slots)) {
  118.             $slots explode('|'$slots);
  119.         }
  120.         if (!empty($slots) && \is_array($slots)) {
  121.             $criteria
  122.                 ->getAssociation('sections.blocks')
  123.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  124.         }
  125.         return $criteria;
  126.     }
  127. }