src/Infrastructure/BlogBundle/Subscriber/SitemapBlogpagesSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace Whater\Infrastructure\BlogBundle\Subscriber;
  3. use Whater\Infrastructure\CommonBundle\Subscriber\AbstractBusSitemapSubscriber;
  4. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  5. use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
  6. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand;
  9. class SitemapBlogpagesSubscriber extends AbstractBusSitemapSubscriber
  10. {
  11. /**
  12. * @var UrlGeneratorInterface
  13. */
  14. private $urlGenerator;
  15. /**
  16. * @param UrlGeneratorInterface $urlGenerator
  17. */
  18. public function __construct(
  19. UrlGeneratorInterface $urlGenerator
  20. ) {
  21. $this->urlGenerator = $urlGenerator;
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function getSubscribedEvents()
  27. {
  28. return [
  29. SitemapPopulateEvent::class => 'registerBlogpagesPages',
  30. ];
  31. }
  32. /**
  33. * @param SitemapPopulateEvent $event
  34. */
  35. public function registerBlogpagesPages(SitemapPopulateEvent $event)
  36. {
  37. // blog page
  38. $blogUrl = new GoogleMultilangUrlDecorator(
  39. new UrlConcrete(
  40. $this->urlGenerator->generate(
  41. 'web_blog_list_articles',
  42. [
  43. '_locale' => 'es'
  44. ],
  45. UrlGeneratorInterface::ABSOLUTE_URL
  46. ),
  47. null,
  48. UrlConcrete::CHANGEFREQ_WEEKLY
  49. ),
  50. 'es'
  51. );
  52. // blog Page
  53. $event->getUrlContainer()->addUrl(
  54. $blogUrl,
  55. 'websiteblog'
  56. );
  57. $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand(1, 500, 'publish_at', 'desc');
  58. $page = $this->handle($publicListBlogArticlesCommand);
  59. $blogPosts = $page->getCurrentPageResults();
  60. foreach ($blogPosts as $blogPost) {
  61. // blog page
  62. $blogPageUrl = new GoogleMultilangUrlDecorator(
  63. new UrlConcrete(
  64. $this->urlGenerator->generate(
  65. 'web_blog_show_article',
  66. [
  67. '_locale' => 'es',
  68. 'articleSlug' => $blogPost->slug()
  69. ],
  70. UrlGeneratorInterface::ABSOLUTE_URL
  71. ),
  72. null,
  73. UrlConcrete::CHANGEFREQ_WEEKLY
  74. ),
  75. 'es'
  76. );
  77. // blog Page
  78. $event->getUrlContainer()->addUrl(
  79. $blogPageUrl,
  80. 'websiteblog'
  81. );
  82. }
  83. }
  84. }