custom/plugins/SwagDynamicAccess/src/Core/Content/Category/SalesChannel/DecoratedCategoryRoute.php line 86

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\DynamicAccess\Core\Content\Category\SalesChannel;
  8. use OpenApi\Annotations as OA;
  9. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  10. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  11. use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
  12. use Shopware\Core\Content\Category\Service\CategoryBreadcrumbBuilder;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\CountResult;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  17. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  18. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. /**
  22.  * @RouteScope(scopes={"store-api"})
  23.  */
  24. class DecoratedCategoryRoute extends AbstractCategoryRoute
  25. {
  26.     private AbstractCategoryRoute $inner;
  27.     private SalesChannelRepositoryInterface $categoryRepository;
  28.     private CategoryBreadcrumbBuilder $breadcrumbBuilder;
  29.     public function __construct(
  30.         AbstractCategoryRoute $inner,
  31.         SalesChannelRepositoryInterface $categoryRepository,
  32.         CategoryBreadcrumbBuilder $breadcrumbBuilder
  33.     ) {
  34.         $this->inner $inner;
  35.         $this->categoryRepository $categoryRepository;
  36.         $this->breadcrumbBuilder $breadcrumbBuilder;
  37.     }
  38.     public function getDecorated(): AbstractCategoryRoute
  39.     {
  40.         return $this->inner;
  41.     }
  42.     /**
  43.      * @OA\Post(
  44.      *     path="/category/{categoryId}",
  45.      *     summary="Fetch a single category",
  46.      *     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.",
  47.      *     operationId="readCategory",
  48.      *     tags={"Store API", "Category"},
  49.      *     @OA\RequestBody(
  50.      *         @OA\JsonContent(
  51.      *             description="The product listing criteria only has an effect, if the category contains a product listing.",
  52.      *             ref="#/components/schemas/ProductListingCriteria"
  53.      *         )
  54.      *     ),
  55.      *     @OA\Parameter(
  56.      *         name="categoryId",
  57.      *         description="Identifier of the category to be fetched",
  58.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  59.      *         in="path",
  60.      *         required=true
  61.      *     ),
  62.      *     @OA\Parameter(
  63.      *         name="slots",
  64.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  65.      *         @OA\Schema(type="string"),
  66.      *         in="query",
  67.      *     ),
  68.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  69.      *     @OA\Response(
  70.      *          response="200",
  71.      *          description="The loaded category with cms page",
  72.      *          @OA\JsonContent(ref="#/components/schemas/Category")
  73.      *     )
  74.      * )
  75.      *
  76.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  77.      */
  78.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  79.     {
  80.         $response $this->inner->load($navigationId$request$context);
  81.         $breadcrumb $this->breadcrumbBuilder->build($response->getCategory(), $context->getSalesChannel());
  82.         if ($breadcrumb === null) {
  83.             return $response;
  84.         }
  85.         unset($breadcrumb[$response->getCategory()->getId()]);
  86.         if (empty($breadcrumb)) {
  87.             return $response;
  88.         }
  89.         $categoryIds \array_keys($breadcrumb);
  90.         $criteria = new Criteria($categoryIds);
  91.         $criteria->setLimit(1);
  92.         $criteria->addAggregation(new CountAggregation('categories''id'));
  93.         /** @var CountResult|null $countAggregation */
  94.         $countAggregation $this->categoryRepository->aggregate($criteria$context)->get('categories');
  95.         if ($countAggregation === null) {
  96.             throw new CategoryNotFoundException($response->getCategory()->getId());
  97.         }
  98.         if ($countAggregation->getCount() !== \count($categoryIds)) {
  99.             throw new CategoryNotFoundException($response->getCategory()->getId());
  100.         }
  101.         return $response;
  102.     }
  103. }