custom/plugins/ShopStudioBlog/src/Storefront/Page/BlogPost/Comment/BlogPostCommentLoader.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopStudio\Blog\Storefront\Page\BlogPost\Comment;
  3. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Comment\AbstractBlogPostCommentRoute;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Framework\Page\StorefrontSearchResult;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13.  * @since 1.0.0
  14.  */
  15. class BlogPostCommentLoader
  16. {
  17.     /**
  18.      * @since 1.0.0
  19.      */
  20.     private const LIMIT 10;
  21.     /**
  22.      * @since 1.0.0
  23.      */
  24.     private const DEFAULT_PAGE 1;
  25.     /**
  26.      * @since 1.0.0
  27.      */
  28.     private const FILTER_LANGUAGE 'filter-language';
  29.     /**
  30.      * @since 1.0.0
  31.      */
  32.     private EventDispatcherInterface $eventDispatcher;
  33.     /**
  34.      * @since 1.0.0
  35.      */
  36.     private AbstractBlogPostCommentRoute $route;
  37.     /**
  38.      * @since 1.0.0
  39.      */
  40.     public function __construct(
  41.         AbstractBlogPostCommentRoute $route,
  42.         EventDispatcherInterface $eventDispatcher
  43.     ) {
  44.         $this->eventDispatcher $eventDispatcher;
  45.         $this->route $route;
  46.     }
  47.     /**
  48.      * @since 1.0.0
  49.      */
  50.     public function load(Request $requestSalesChannelContext $context): BlogPostCommentLoaderResult
  51.     {
  52.         $blogPostId $request->get('blogPostId');
  53.         if (empty($blogPostId)) {
  54.             throw new MissingRequestParameterException('blogPostId');
  55.         }
  56.         $criteria $this->createCriteria($request$context);
  57.         $comments $this->route
  58.             ->load($blogPostId$request$context$criteria)
  59.             ->getResult()
  60.         ;
  61.         $comments StorefrontSearchResult::createFrom($comments);
  62.         $this->eventDispatcher->dispatch(new BlogPostCommentsLoadedEvent($comments$context$request));
  63.         $result BlogPostCommentLoaderResult::createFrom($comments);
  64.         $result->setBlogPostId($blogPostId);
  65.         $result->setTotalComments($comments->getTotal());
  66.         return $result;
  67.     }
  68.     /**
  69.      * @since 1.0.0
  70.      */
  71.     private function createCriteria(Request $requestSalesChannelContext $context): Criteria
  72.     {
  73.         $limit = (int) $request->get('limit'self::LIMIT);
  74.         $page = (int) $request->get('p'self::DEFAULT_PAGE);
  75.         $offset $limit * ($page 1);
  76.         $criteria = new Criteria();
  77.         $criteria->setLimit($limit);
  78.         $criteria->setOffset($offset);
  79.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  80.         if ($request->get('language') === self::FILTER_LANGUAGE) {
  81.             $criteria->addPostFilter(
  82.                 new EqualsFilter('languageId'$context->getContext()->getLanguageId())
  83.             );
  84.         }
  85.         return $criteria;
  86.     }
  87. }