custom/static-plugins/NdStl/src/Core/Content/Look/LookRoute.php line 93

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Stl\Core\Content\Look;
  3. use Nd\Stl\Core\Content\Look\Exception\LookNotFoundException;
  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\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  14. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  15. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  16. use Shopware\Core\Framework\Routing\Annotation\Since;
  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.  * @Route(defaults={"_routeScope"={"store-api"}})
  23.  */
  24. class LookRoute extends AbstractLookRoute
  25. {
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private EntityRepositoryInterface $lookRepository;
  30.     /**
  31.      * @var SalesChannelCmsPageLoaderInterface
  32.      */
  33.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  34.     /**
  35.      * @internal
  36.      */
  37.     public function __construct(
  38.         EntityRepositoryInterface $lookRepository,
  39.         SalesChannelCmsPageLoaderInterface $cmsPageLoader
  40.     ) {
  41.         $this->lookRepository $lookRepository;
  42.         $this->cmsPageLoader $cmsPageLoader;
  43.     }
  44.     public function getDecorated(): AbstractLookRoute
  45.     {
  46.         throw new DecorationPatternException(self::class);
  47.     }
  48.     /**
  49.      * @Since("6.2.0.0")
  50.      * @OA\Post(
  51.      *     path="/look/{look}",
  52.      *     summary="Fetch a single product manufacturer",
  53.      *     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.",
  54.      *     operationId="readLook",
  55.      *     tags={"Store API", "Look"},
  56.      *     @OA\RequestBody(
  57.      *         @OA\JsonContent(
  58.      *             description="The product listing criteria only has an effect, if the category contains a product listing.",
  59.      *             ref="#/components/schemas/ProductListingCriteria"
  60.      *         )
  61.      *     ),
  62.      *     @OA\Parameter(
  63.      *         name="lookId",
  64.      *         description="Identifier of the category to be fetched",
  65.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  66.      *         in="path",
  67.      *         required=true
  68.      *     ),
  69.      *     @OA\Parameter(
  70.      *         name="slots",
  71.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  72.      *         @OA\Schema(type="string"),
  73.      *         in="query",
  74.      *     ),
  75.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  76.      *     @OA\Response(
  77.      *          response="200",
  78.      *          description="The loaded category with cms page",
  79.      *          @OA\JsonContent(ref="#/components/schemas/Category")
  80.      *     )
  81.      * )
  82.      *
  83.      * @Route("/store-api/look/{lookId}", name="store-api.look.detail", methods={"GET","POST"})
  84.      */
  85.     public function load(string $lookIdRequest $requestSalesChannelContext $context): LookRouteResponse
  86.     {
  87.         $look $this->loadLook($lookId$context);
  88.         $pageId $look->getCmsPageId();
  89.         $slotConfig $look->getTranslation('slotConfig');
  90.         if (!$pageId) {
  91.             return new LookRouteResponse($look);
  92.         }
  93.         $pages $this->cmsPageLoader->load(
  94.             $request,
  95.             $this->createCriteria($pageId$request),
  96.             $context,
  97.             $slotConfig
  98.         );
  99.         if (!$pages->has($pageId)) {
  100.             throw new PageNotFoundException($pageId);
  101.         }
  102.         $look->setCmsPage($pages->get($pageId));
  103.         $look->setCmsPageId($pageId);
  104.         return new LookRouteResponse($look);
  105.     }
  106.     private function loadLook(string $lookIdSalesChannelContext $context): LookEntity
  107.     {
  108.         $criteria = new Criteria([$lookId]);
  109.         $look $this->lookRepository
  110.             ->search($criteria$context->getContext())
  111.             ->get($lookId);
  112.         if (!$look) {
  113.             throw new LookNotFoundException($lookId);
  114.         }
  115.         return $look;
  116.     }
  117.     private function createCriteria(string $pageIdRequest $request): Criteria
  118.     {
  119.         $criteria = new Criteria([$pageId]);
  120.         $criteria->setTitle('look::cms-page');
  121.         $slots $request->get('slots');
  122.         if (\is_string($slots)) {
  123.             $slots explode('|'$slots);
  124.         }
  125.         if (!empty($slots) && \is_array($slots)) {
  126.             $criteria
  127.                 ->getAssociation('sections.blocks')
  128.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  129.         }
  130.         return $criteria;
  131.     }
  132. }