<?php declare(strict_types=1);
namespace Nd\Fashion\Page\Manufacturer;
use Nd\Fashion\Page\Manufacturer\Exception\ProductManufacturerNotFoundException;
use OpenApi\Annotations as OA;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
class ProductManufacturerRoute extends AbstractProductManufacturerRoute
{
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $productManufacturerRepository;
/**
* @var SalesChannelCmsPageLoaderInterface
*/
private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
/**
* @internal
*/
public function __construct(
EntityRepositoryInterface $productManufacturerRepository,
SalesChannelCmsPageLoaderInterface $cmsPageLoader
) {
$this->productManufacturerRepository = $productManufacturerRepository;
$this->cmsPageLoader = $cmsPageLoader;
}
public function getDecorated(): AbstractProductManufacturerRoute
{
throw new DecorationPatternException(self::class);
}
/**
* @Since("6.2.0.0")
* @OA\Post(
* path="/productManufacturer/{productManufacturer}",
* summary="Fetch a single product manufacturer",
* 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.",
* operationId="readProductManufacturer",
* tags={"Store API", "ProductManufacturer"},
* @OA\RequestBody(
* @OA\JsonContent(
* description="The product listing criteria only has an effect, if the category contains a product listing.",
* ref="#/components/schemas/ProductListingCriteria"
* )
* ),
* @OA\Parameter(
* name="productManufacturerId",
* description="Identifier of the category to be fetched",
* @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
* in="path",
* required=true
* ),
* @OA\Parameter(
* name="slots",
* description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
* @OA\Schema(type="string"),
* in="query",
* ),
* @OA\Parameter(name="Api-Basic-Parameters"),
* @OA\Response(
* response="200",
* description="The loaded category with cms page",
* @OA\JsonContent(ref="#/components/schemas/Category")
* )
* )
*
* @Route("/store-api/productManufacturer/{productManufacturerId}", name="store-api.productManufacturer.detail", methods={"GET","POST"})
*/
public function load(string $navigationId, Request $request, SalesChannelContext $context): ProductManufacturerRouteResponse
{
$productManufacturer = $this->loadProductManufacturer($navigationId, $context);
$productManufacturerPageExtension = $productManufacturer->getExtension('productPageExtension');
$pageId = $productManufacturerPageExtension->getCmsPageId();
$slotConfig = $productManufacturerPageExtension->getTranslation('slotConfig');
if (!$pageId) {
return new ProductManufacturerRouteResponse($productManufacturer);
}
// $resolverContext = new EntityResolverContext($context, $request, $this->productManufacturerDefinition, $productManufacturer);
$pages = $this->cmsPageLoader->load(
$request,
$this->createCriteria($pageId, $request),
$context,
$slotConfig
);
if (!$pages->has($pageId)) {
throw new PageNotFoundException($pageId);
}
$productManufacturerPageExtension->setCmsPage($pages->get($pageId));
$productManufacturerPageExtension->setCmsPageId($pageId);
return new ProductManufacturerRouteResponse($productManufacturer);
}
private function loadProductManufacturer(string $productManufacturerId, SalesChannelContext $context): ProductManufacturerEntity
{
$criteria = new Criteria([$productManufacturerId]);
$criteria->addAssociation('baseExtension');
$criteria->addAssociation('listingPageExtension');
$criteria->addAssociation('productPageExtension');
$productManufacturer = $this->productManufacturerRepository
->search($criteria, $context->getContext())
->get($productManufacturerId);
if (!$productManufacturer) {
throw new ProductManufacturerNotFoundException($productManufacturerId);
}
return $productManufacturer;
}
private function createCriteria(string $pageId, Request $request): Criteria
{
$criteria = new Criteria([$pageId]);
$criteria->setTitle('productManufacturer::cms-page');
$slots = $request->get('slots');
if (\is_string($slots)) {
$slots = explode('|', $slots);
}
if (!empty($slots) && \is_array($slots)) {
$criteria
->getAssociation('sections.blocks')
->addFilter(new EqualsAnyFilter('slots.id', $slots));
}
return $criteria;
}
}