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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Content\Category\CategoryDefinition;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  7. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  8. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  9. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  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.  * @RouteScope(scopes={"store-api"})
  21.  */
  22. class CategoryRoute extends AbstractCategoryRoute
  23. {
  24.     public const HOME 'home';
  25.     /**
  26.      * @var SalesChannelRepositoryInterface
  27.      */
  28.     private $categoryRepository;
  29.     /**
  30.      * @var SalesChannelCmsPageLoaderInterface
  31.      */
  32.     private $cmsPageLoader;
  33.     /**
  34.      * @var CategoryDefinition
  35.      */
  36.     private $categoryDefinition;
  37.     public function __construct(
  38.         SalesChannelRepositoryInterface $categoryRepository,
  39.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  40.         CategoryDefinition $categoryDefinition
  41.     ) {
  42.         $this->categoryRepository $categoryRepository;
  43.         $this->cmsPageLoader $cmsPageLoader;
  44.         $this->categoryDefinition $categoryDefinition;
  45.     }
  46.     public function getDecorated(): AbstractCategoryRoute
  47.     {
  48.         throw new DecorationPatternException(self::class);
  49.     }
  50.     /**
  51.      * @Since("6.2.0.0")
  52.      * @OA\Post(
  53.      *     path="/category/{categoryId}",
  54.      *     summary="Fetch a single category",
  55.      *     description="This endpoint returns information about the category, as well as a fully resolved (hydrated with mapping values) CMS page, if one is assigned to the category. You can pass slots which should be resolved exclusively.",
  56.      *     operationId="readCategory",
  57.      *     tags={"Store API", "Category"},
  58.      *     @OA\RequestBody(
  59.      *         @OA\JsonContent(
  60.      *             description="The product listing criteria only has an effect, if the category contains a product listing.",
  61.      *             ref="#/components/schemas/ProductListingCriteria"
  62.      *         )
  63.      *     ),
  64.      *     @OA\Parameter(
  65.      *         name="categoryId",
  66.      *         description="Identifier of the category to be fetched",
  67.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  68.      *         in="path",
  69.      *         required=true
  70.      *     ),
  71.      *     @OA\Parameter(
  72.      *         name="slots",
  73.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  74.      *         @OA\Schema(type="string"),
  75.      *         in="query",
  76.      *     ),
  77.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  78.      *     @OA\Response(
  79.      *          response="200",
  80.      *          description="The loaded category with cms page",
  81.      *          @OA\JsonContent(ref="#/components/schemas/Category")
  82.      *     )
  83.      * )
  84.      *
  85.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  86.      */
  87.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  88.     {
  89.         if ($navigationId === self::HOME) {
  90.             $navigationId $context->getSalesChannel()->getNavigationCategoryId();
  91.             $request->attributes->set('navigationId'$navigationId);
  92.             $routeParams $request->attributes->get('_route_params', []);
  93.             $routeParams['navigationId'] = $navigationId;
  94.             $request->attributes->set('_route_params'$routeParams);
  95.         }
  96.         $category $this->loadCategory($navigationId$context);
  97.         if (($category->getType() === CategoryDefinition::TYPE_FOLDER
  98.                 || $category->getType() === CategoryDefinition::TYPE_LINK)
  99.             && $context->getSalesChannel()->getNavigationCategoryId() !== $navigationId
  100.         ) {
  101.             throw new CategoryNotFoundException($navigationId);
  102.         }
  103.         $pageId $category->getCmsPageId();
  104.         $slotConfig $category->getTranslation('slotConfig');
  105.         $salesChannel $context->getSalesChannel();
  106.         if ($category->getId() === $salesChannel->getNavigationCategoryId() && $salesChannel->getHomeCmsPageId()) {
  107.             $pageId $salesChannel->getHomeCmsPageId();
  108.             $slotConfig $salesChannel->getTranslation('homeSlotConfig');
  109.         }
  110.         if (!$pageId) {
  111.             return new CategoryRouteResponse($category);
  112.         }
  113.         $resolverContext = new EntityResolverContext($context$request$this->categoryDefinition$category);
  114.         $pages $this->cmsPageLoader->load(
  115.             $request,
  116.             $this->createCriteria($pageId$request),
  117.             $context,
  118.             $slotConfig,
  119.             $resolverContext
  120.         );
  121.         if (!$pages->has($pageId)) {
  122.             throw new PageNotFoundException($pageId);
  123.         }
  124.         $category->setCmsPage($pages->get($pageId));
  125.         $category->setCmsPageId($pageId);
  126.         return new CategoryRouteResponse($category);
  127.     }
  128.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  129.     {
  130.         $criteria = new Criteria([$categoryId]);
  131.         $criteria->setTitle('category::data');
  132.         $criteria->addAssociation('media');
  133.         $category $this->categoryRepository
  134.             ->search($criteria$context)
  135.             ->get($categoryId);
  136.         if (!$category) {
  137.             throw new CategoryNotFoundException($categoryId);
  138.         }
  139.         return $category;
  140.     }
  141.     private function createCriteria(string $pageIdRequest $request): Criteria
  142.     {
  143.         $criteria = new Criteria([$pageId]);
  144.         $criteria->setTitle('category::cms-page');
  145.         $slots $request->get('slots');
  146.         if (\is_string($slots)) {
  147.             $slots explode('|'$slots);
  148.         }
  149.         if (!empty($slots) && \is_array($slots)) {
  150.             $criteria
  151.                 ->getAssociation('sections.blocks')
  152.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  153.         }
  154.         return $criteria;
  155.     }
  156. }