<?php
namespace Whater\Infrastructure\BlogBundle\Subscriber;
use Whater\Infrastructure\CommonBundle\Subscriber\AbstractBusSitemapSubscriber;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand;
class SitemapBlogpagesSubscriber extends AbstractBusSitemapSubscriber
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
/**
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(
UrlGeneratorInterface $urlGenerator
) {
$this->urlGenerator = $urlGenerator;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::class => 'registerBlogpagesPages',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function registerBlogpagesPages(SitemapPopulateEvent $event)
{
// blog page
$blogUrl = new GoogleMultilangUrlDecorator(
new UrlConcrete(
$this->urlGenerator->generate(
'web_blog_list_articles',
[
'_locale' => 'es'
],
UrlGeneratorInterface::ABSOLUTE_URL
),
null,
UrlConcrete::CHANGEFREQ_WEEKLY
),
'es'
);
// blog Page
$event->getUrlContainer()->addUrl(
$blogUrl,
'websiteblog'
);
$publicListBlogArticlesCommand = new PublicListBlogArticlesCommand(1, 500, 'publish_at', 'desc');
$page = $this->handle($publicListBlogArticlesCommand);
$blogPosts = $page->getCurrentPageResults();
foreach ($blogPosts as $blogPost) {
// blog page
$blogPageUrl = new GoogleMultilangUrlDecorator(
new UrlConcrete(
$this->urlGenerator->generate(
'web_blog_show_article',
[
'_locale' => 'es',
'articleSlug' => $blogPost->slug()
],
UrlGeneratorInterface::ABSOLUTE_URL
),
null,
UrlConcrete::CHANGEFREQ_WEEKLY
),
'es'
);
// blog Page
$event->getUrlContainer()->addUrl(
$blogPageUrl,
'websiteblog'
);
}
}
}