custom/plugins/SasBlogModule/src/Core/Content/Sitemap/Provider/BlogUrlProvider.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Core\Content\Sitemap\Provider;
  3. use Doctrine\DBAL\Connection;
  4. use Sas\BlogModule\Content\Blog\BlogEntriesCollection;
  5. use Sas\BlogModule\Content\Blog\BlogEntriesEntity;
  6. use Sas\BlogModule\Content\Blog\Events\BlogIndexerEvent;
  7. use Shopware\Core\Content\Sitemap\Provider\AbstractUrlProvider;
  8. use Shopware\Core\Content\Sitemap\Struct\Url;
  9. use Shopware\Core\Content\Sitemap\Struct\UrlResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  15. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. class BlogUrlProvider extends AbstractUrlProvider
  19. {
  20.     public const CHANGE_FREQ 'daily';
  21.     public const PRIORITY 1.0;
  22.     private EntityRepositoryInterface $blogRepository;
  23.     private Connection $connection;
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     public function __construct(
  26.         EntityRepositoryInterface $blogRepository,
  27.         Connection $connection,
  28.         EventDispatcherInterface $eventDispatcher
  29.     ) {
  30.         $this->blogRepository $blogRepository;
  31.         $this->connection $connection;
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     public function getDecorated(): AbstractUrlProvider
  35.     {
  36.         throw new DecorationPatternException(self::class);
  37.     }
  38.     public function getName(): string
  39.     {
  40.         return 'sasBlog';
  41.     }
  42.     public function getUrls(SalesChannelContext $contextint $limit, ?int $offset null): UrlResult
  43.     {
  44.         $criteria = new Criteria();
  45.         $dateTime = new \DateTime();
  46.         $criteria->setLimit($limit);
  47.         $criteria->setOffset($offset);
  48.         $criteria->addFilter(
  49.             new EqualsFilter('active'true),
  50.             new RangeFilter('publishedAt', [RangeFilter::LTE => $dateTime->format(\DATE_ATOM)])
  51.         );
  52.         /** @var BlogEntriesCollection $blogEntities */
  53.         $blogEntities $this->blogRepository->search($criteria$context->getContext())->getEntities();
  54.         if ($blogEntities->count() === 0) {
  55.             return new UrlResult([], null);
  56.         }
  57.         $this->eventDispatcher->dispatch(new BlogIndexerEvent($blogEntities->getIds(), $context->getContext()));
  58.         $seoUrls $this->getSeoUrls($blogEntities->getIds(), 'sas.frontend.blog.detail'$context$this->connection);
  59.         $seoUrls FetchModeHelper::groupUnique($seoUrls);
  60.         $urls = [];
  61.         /*  @var BlogEntriesEntity  $blogEntity */
  62.         foreach ($blogEntities as $blogEntity) {
  63.             if (!\array_key_exists($blogEntity->getId(), $seoUrls)) {
  64.                 continue;
  65.             }
  66.             $seoUrl $seoUrls[$blogEntity->getId()];
  67.             if (!\array_key_exists('seo_path_info'$seoUrl)) {
  68.                 continue;
  69.             }
  70.             if (!\is_string($seoUrl['seo_path_info'])) {
  71.                 continue;
  72.             }
  73.             $blogUrl = new Url();
  74.             $blogUrl->setLastmod($blogEntity->getUpdatedAt() ?? new \DateTime());
  75.             $blogUrl->setChangefreq(self::CHANGE_FREQ);
  76.             $blogUrl->setPriority(self::PRIORITY);
  77.             $blogUrl->setResource(BlogEntriesEntity::class);
  78.             $blogUrl->setIdentifier($blogEntity->getId());
  79.             $blogUrl->setLoc($seoUrl['seo_path_info']);
  80.             $urls[] = $blogUrl;
  81.         }
  82.         return new UrlResult($urlsnull);
  83.     }
  84. }