vendor/store.shopware.com/h1webblog/src/Provider/BlogProvider.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace H1web\Blog\Provider;
  4. use Doctrine\DBAL\Connection;
  5. use H1web\Blog\Blog\BlogCollection;
  6. use H1web\Blog\Blog\BlogEntity;
  7. use H1web\Blog\Blog\SalesChannel\BlogAvailableFilter;
  8. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  9. use Shopware\Core\Content\Sitemap\Provider\AbstractUrlProvider;
  10. use Shopware\Core\Content\Sitemap\Struct\Url;
  11. use Shopware\Core\Content\Sitemap\Struct\UrlResult;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Routing\RouterInterface;
  20. class BlogProvider extends AbstractUrlProvider
  21. {
  22.     public const CHANGE_FREQ 'monthly';
  23.     private EntityRepositoryInterface $blogRepository;
  24.     private SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler;
  25.     private Connection $connection;
  26.     private RouterInterface $router;
  27.     public function __construct(
  28.         EntityRepositoryInterface $blogRepository,
  29.         SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
  30.         RouterInterface $router,
  31.         Connection $connection
  32.     ) {
  33.         $this->blogRepository $blogRepository;
  34.         $this->seoUrlPlaceholderHandler $seoUrlPlaceholderHandler;
  35.         $this->router $router;
  36.         $this->connection $connection;
  37.     }
  38.     public function getName(): string
  39.     {
  40.         return 'blog';
  41.     }
  42.     public function getUrls(SalesChannelContext $contextint $limit, ?int $offset null): UrlResult
  43.     {
  44.         $blogs $this->getBlogposts($context$limit$offset);
  45.         if (empty($blogs)) {
  46.             return new UrlResult([], null);
  47.         }
  48.         $seoUrls $this->getSeoUrls($blogs->getIds(), 'frontend.h1webblog.blog'$context$this->connection);
  49.         $seoUrls FetchModeHelper::groupUnique($seoUrls);
  50.         $tagUrls = [];
  51.         $urls = [];
  52.         $url = new Url();
  53.         /** @var BlogEntity $blog */
  54.         foreach ($blogs as $blog) {
  55.             /** @var \DateTimeInterface $lastmod */
  56.             $lastmod $blog->getUpdatedAt() ?: $blog->getCreatedAt();
  57.             $newUrl = clone $url;
  58.             if (isset($seoUrls[$blog->getId()])) {
  59.                 $newUrl->setLoc($seoUrls[$blog->getId()]['seo_path_info']);
  60.             } else {
  61.                 $newUrl->setLoc($this->router->generate('frontend.h1webblog.blog', ['id' => $blog->getId()], UrlGeneratorInterface::ABSOLUTE_PATH));
  62.             }
  63.             $newUrl->setLastmod($lastmod);
  64.             $newUrl->setChangefreq(self::CHANGE_FREQ);
  65.             $newUrl->setResource(BlogEntity::class);
  66.             $newUrl->setIdentifier($blog->getId());
  67.             $newUrl->setPriority(0.2);
  68.             $urls[] = $newUrl;
  69.             if ($blog->getTags() === null) {
  70.                 continue;
  71.             }
  72.             $tagSeoUrls $this->getSeoUrls($blog->getTags()->getIds(), 'frontend.h1webblog.tag_overview'$context$this->connection);
  73.             $tagSeoUrls FetchModeHelper::groupUnique($tagSeoUrls);
  74.             foreach ($blog->getTags() as $tag) {
  75.                 if (array_key_exists($tag->getId(), $tagUrls)) {
  76.                     continue;
  77.                 }
  78.                 $tagUrl = new Url();
  79.                 $tagUrl->setLoc($tagSeoUrls[$tag->getId()]['seo_path_info']);
  80.                 $tagUrl->setLastmod(new \DateTime());
  81.                 $tagUrl->setChangefreq(self::CHANGE_FREQ);
  82.                 $tagUrl->setResource('tag');
  83.                 $tagUrl->setIdentifier($tag->getId());
  84.                 $tagUrl->setPriority(0.2);
  85.                 $tagUrls[$tag->getId()] = $tagUrl;
  86.             }
  87.         }
  88.         if (\count($urls) < $limit) { // last run
  89.             $nextOffset null;
  90.         } elseif ($offset === null) { // first run
  91.             $nextOffset $limit;
  92.         } else { // 1+n run
  93.             $nextOffset $offset $limit;
  94.         }
  95.         $urls array_merge($urlsarray_values($tagUrls));
  96.         return new UrlResult($urls$nextOffset);
  97.     }
  98.     private function getBlogposts(SalesChannelContext $salesChannelContextint $limit, ?int $offset): BlogCollection
  99.     {
  100.         $blogCriteria = new Criteria();
  101.         $blogCriteria->setLimit($limit);
  102.         $blogCriteria->addFilter(new BlogAvailableFilter($salesChannelContext->getSalesChannelId()))
  103.             ->addFilter(new EqualsFilter('isActive'1))
  104.             ->addAssociation('visibilities')->addAssociation('tags');
  105.         if ($offset !== null) {
  106.             $blogCriteria->setOffset($offset);
  107.         }
  108.         return $this->blogRepository->search($blogCriteria$salesChannelContext->getContext())->getEntities();
  109.     }
  110.     public function getDecorated(): AbstractUrlProvider
  111.     {
  112.         throw new DecorationPatternException(self::class);
  113.     }
  114. }