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

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