<?php declare(strict_types=1);
namespace ShopStudio\Blog\Content\BlogPost\SalesChannel\Detail;
use OpenApi\Annotations as OA;
use ShopStudio\Blog\Content\BlogPost\Exception\BlogPostNotFoundException;
use ShopStudio\Blog\Content\BlogPost\SalesChannel\BlogPostAvailableFilter;
use ShopStudio\Blog\Content\BlogPost\SalesChannel\SalesChannelBlogPostEntity;
use ShopStudio\Blog\Content\BlogPost\SalesChannel\Schema\Detail\BlogPostDetailSchemaFactory;
use ShopStudio\Blog\Content\Category\Service\CategoryBreadcrumbBuilder;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since as OASince;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @since 1.0.0
*
* @RouteScope(scopes={"store-api"})
*/
class BlogPostDetailRoute extends AbstractBlogPostDetailRoute
{
/**
* @since 1.0.0
*/
private SalesChannelRepositoryInterface $repository;
/**
* @since 1.0.0
*/
private BlogPostDetailSchemaFactory $schemaFactory;
/**
* @since 2.12.0
*/
private CategoryBreadcrumbBuilder $breadcrumbBuilder;
/**
* @since 2.12.0
*/
public function __construct(
SalesChannelRepositoryInterface $repository,
BlogPostDetailSchemaFactory $schemaFactory,
CategoryBreadcrumbBuilder $breadcrumbBuilder
) {
$this->repository = $repository;
$this->schemaFactory = $schemaFactory;
$this->breadcrumbBuilder = $breadcrumbBuilder;
}
/**
* @since 1.0.0
*/
public function getDecorated(): AbstractBlogPostDetailRoute
{
throw new DecorationPatternException(self::class);
}
/**
* @since 1.0.0
*
* @Entity("shopstudio_blog_blog_post")
*
* @OASince("2.0.0")
*
* @Route("/store-api/shop-studio-blog/blog-post-detail/{blogPostId}", name="store-api.shop-studio-blog.blog-post-detail", methods={"POST"})
*
* @OA\Post(
* path="/shop-studio-blog/blog-post-detail/{blogPostId}",
* summary="This route is used to load a single blog post with the corresponding details.",
* operationId="shopStudioBlogReadBlogPostDetail",
* tags={"Store API","Shop Studio Blog"},
* @OA\Parameter(name="blogPostId", description="Blog post ID", @OA\Schema(type="string"), in="path", required=true),
* @OA\Response(
* response="200",
* description="Found blog post"
* )
* )
*/
public function load(string $blogPostId, Request $request, SalesChannelContext $context, Criteria $criteria): BlogPostDetailRouteResponse
{
$this->addFilters($context, $criteria);
$criteria->setIds([$blogPostId]);
$blogPost = $this->repository
->search($criteria, $context)
->first()
;
if (!$blogPost instanceof SalesChannelBlogPostEntity) {
throw new BlogPostNotFoundException($blogPostId);
}
$blogPost->setSeoCategory(
$this->breadcrumbBuilder->getBlogPostSeoCategory($blogPost, $context)
);
return new BlogPostDetailRouteResponse(
$blogPost,
$this->schemaFactory->create($blogPost, $context)
);
}
/**
* @since 1.0.0
*/
private function addFilters(SalesChannelContext $context, Criteria $criteria): void
{
$criteria->addFilter(
new BlogPostAvailableFilter(
$context->getSalesChannel()->getId(),
$context->getLanguageId()
)
);
}
}