custom/static-plugins/NdFashion/src/Page/Manufacturer/ProductManufacturerRoute.php line 125

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Fashion\Page\Manufacturer;
  3. use Nd\Fashion\Page\Manufacturer\Exception\ProductManufacturerNotFoundException;
  4. use OpenApi\Annotations as OA;
  5. use Shopware\Core\Content\Category\CategoryDefinition;
  6. use Shopware\Core\Content\Category\CategoryEntity;
  7. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  9. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  10. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  11. use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  15. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  16. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  17. use Shopware\Core\Framework\Routing\Annotation\Since;
  18. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. /**
  23.  * @Route(defaults={"_routeScope"={"store-api"}})
  24.  */
  25. class ProductManufacturerRoute extends AbstractProductManufacturerRoute
  26. {
  27.     /**
  28.      * @var EntityRepositoryInterface
  29.      */
  30.     private EntityRepositoryInterface $productManufacturerRepository;
  31.     /**
  32.      * @var SalesChannelCmsPageLoaderInterface
  33.      */
  34.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(
  39.         EntityRepositoryInterface $productManufacturerRepository,
  40.         SalesChannelCmsPageLoaderInterface $cmsPageLoader
  41.     ) {
  42.         $this->productManufacturerRepository $productManufacturerRepository;
  43.         $this->cmsPageLoader $cmsPageLoader;
  44.     }
  45.     public function getDecorated(): AbstractProductManufacturerRoute
  46.     {
  47.         throw new DecorationPatternException(self::class);
  48.     }
  49.     /**
  50.      * @Since("6.2.0.0")
  51.      * @OA\Post(
  52.      *     path="/productManufacturer/{productManufacturer}",
  53.      *     summary="Fetch a single product manufacturer",
  54.      *     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.",
  55.      *     operationId="readProductManufacturer",
  56.      *     tags={"Store API", "ProductManufacturer"},
  57.      *     @OA\RequestBody(
  58.      *         @OA\JsonContent(
  59.      *             description="The product listing criteria only has an effect, if the category contains a product listing.",
  60.      *             ref="#/components/schemas/ProductListingCriteria"
  61.      *         )
  62.      *     ),
  63.      *     @OA\Parameter(
  64.      *         name="productManufacturerId",
  65.      *         description="Identifier of the category to be fetched",
  66.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  67.      *         in="path",
  68.      *         required=true
  69.      *     ),
  70.      *     @OA\Parameter(
  71.      *         name="slots",
  72.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  73.      *         @OA\Schema(type="string"),
  74.      *         in="query",
  75.      *     ),
  76.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  77.      *     @OA\Response(
  78.      *          response="200",
  79.      *          description="The loaded category with cms page",
  80.      *          @OA\JsonContent(ref="#/components/schemas/Category")
  81.      *     )
  82.      * )
  83.      *
  84.      * @Route("/store-api/productManufacturer/{productManufacturerId}", name="store-api.productManufacturer.detail", methods={"GET","POST"})
  85.      */
  86.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): ProductManufacturerRouteResponse
  87.     {
  88.         $productManufacturer $this->loadProductManufacturer($navigationId$context);
  89.         $productManufacturerPageExtension $productManufacturer->getExtension('productPageExtension');
  90.         $pageId $productManufacturerPageExtension->getCmsPageId();
  91.         $slotConfig $productManufacturerPageExtension->getTranslation('slotConfig');
  92.         if (!$pageId) {
  93.             return new ProductManufacturerRouteResponse($productManufacturer);
  94.         }
  95. //        $resolverContext = new EntityResolverContext($context, $request, $this->productManufacturerDefinition, $productManufacturer);
  96.         $pages $this->cmsPageLoader->load(
  97.             $request,
  98.             $this->createCriteria($pageId$request),
  99.             $context,
  100.             $slotConfig
  101.         );
  102.         if (!$pages->has($pageId)) {
  103.             throw new PageNotFoundException($pageId);
  104.         }
  105.         $productManufacturerPageExtension->setCmsPage($pages->get($pageId));
  106.         $productManufacturerPageExtension->setCmsPageId($pageId);
  107.         return new ProductManufacturerRouteResponse($productManufacturer);
  108.     }
  109.     private function loadProductManufacturer(string $productManufacturerIdSalesChannelContext $context): ProductManufacturerEntity
  110.     {
  111.         $criteria = new Criteria([$productManufacturerId]);
  112.         $criteria->addAssociation('baseExtension');
  113.         $criteria->addAssociation('listingPageExtension');
  114.         $criteria->addAssociation('productPageExtension');
  115.         $productManufacturer $this->productManufacturerRepository
  116.             ->search($criteria$context->getContext())
  117.             ->get($productManufacturerId);
  118.         if (!$productManufacturer) {
  119.             throw new ProductManufacturerNotFoundException($productManufacturerId);
  120.         }
  121.         return $productManufacturer;
  122.     }
  123.     private function createCriteria(string $pageIdRequest $request): Criteria
  124.     {
  125.         $criteria = new Criteria([$pageId]);
  126.         $criteria->setTitle('productManufacturer::cms-page');
  127.         $slots $request->get('slots');
  128.         if (\is_string($slots)) {
  129.             $slots explode('|'$slots);
  130.         }
  131.         if (!empty($slots) && \is_array($slots)) {
  132.             $criteria
  133.                 ->getAssociation('sections.blocks')
  134.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  135.         }
  136.         return $criteria;
  137.     }
  138. }