Skip to content

Commit

Permalink
issue#386 sitemap google (afup#1588)
Browse files Browse the repository at this point in the history
* issue#386 sitemap google

* issue#386 sitemap google video
  • Loading branch information
stakovicz authored Jan 20, 2025
1 parent 7e64cca commit cf646d3
Showing 1 changed file with 64 additions and 28 deletions.
92 changes: 64 additions & 28 deletions sources/AppBundle/Subscriber/SitemapXmlSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace AppBundle\Subscriber;

use Afup\Site\Corporate\Branche;
use Afup\Site\Corporate\Feuille;
use AppBundle\Association\Model\CompanyMember;
use AppBundle\Association\Model\Repository\CompanyMemberRepository;
use AppBundle\Event\Model\Repository\TalkRepository;
use AppBundle\Event\Model\Talk;
Expand All @@ -10,17 +13,15 @@
use CCMBenchmark\TingBundle\Repository\RepositoryFactory;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\GoogleVideoUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class SitemapXmlSubscriber implements EventSubscriberInterface
{
/** @var RepositoryFactory */
private $ting;

/** @var UrlGeneratorInterface */
private $urlGenerator;
private RepositoryFactory $ting;
private UrlGeneratorInterface $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator, RepositoryFactory $ting)
{
Expand All @@ -35,40 +36,52 @@ public static function getSubscribedEvents(): array
];
}

public function populate(SitemapPopulateEvent $event)
public function populate(SitemapPopulateEvent $event): void
{
$this->registerTalksUrls($event->getUrlContainer());
$this->registerNewsUrls($event->getUrlContainer());
$this->registerMembers($event->getUrlContainer());
$this->registerDefaultPages($event->getUrlContainer());
}

public function registerTalksUrls(UrlContainerInterface $urls)
public function registerTalksUrls(UrlContainerInterface $urls): void
{
/** @var Talk[] $talks */
$talks = $this->ting->get(TalkRepository::class)->getAllPastTalks(new \DateTime());

/** @var Talk $talk */
foreach ($talks as $talk) {
if ($talk->isDisplayedOnHistory()) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'talks_show',
['id' => $talk->getId(), 'slug' => $talk->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
),
$talk->getSubmittedOn()
),
'talks'
if (!$talk->isDisplayedOnHistory()) {
continue;
}

$url = new UrlConcrete(
$this->urlGenerator->generate(
'talks_show',
['id' => $talk->getId(), 'slug' => $talk->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
),
$talk->getSubmittedOn()
);
$urls->addUrl($url,'talks');

if ($talk->hasYoutubeId()) {
$urlVideo = new GoogleVideoUrlDecorator(
$url,
sprintf('https://img.youtube.com/vi/%s/0.jpg', $talk->getYoutubeId()),
$talk->getTitle(),
strip_tags(html_entity_decode($talk->getDescription())),
['content_loc' => $talk->getYoutubeUrl()]
);
$urls->addUrl($urlVideo,'video');
}
}
}

public function registerNewsUrls(UrlContainerInterface $urls)
public function registerNewsUrls(UrlContainerInterface $urls): void
{
/** @var Article[] $news */
$news = $this->ting->get(ArticleRepository::class)->findAllPublishedNews();

/** @var Article $article */
foreach ($news as $article) {
$urls->addUrl(
new UrlConcrete(
Expand All @@ -84,15 +97,12 @@ public function registerNewsUrls(UrlContainerInterface $urls)
}
}

private function registerMembers(UrlContainerInterface $urls)
private function registerMembers(UrlContainerInterface $urls): void
{
/**
* @var CompanyMemberRepository $companyRepository
*/
$companyRepository = $this->ting->get(CompanyMemberRepository::class);
$displayableCompanies = $companyRepository->findDisplayableCompanies();
/** @var CompanyMember[] $members */
$members = $this->ting->get(CompanyMemberRepository::class)->findDisplayableCompanies();

foreach ($displayableCompanies as $member) {
foreach ($members as $member) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
Expand All @@ -108,4 +118,30 @@ private function registerMembers(UrlContainerInterface $urls)
);
}
}

private function registerDefaultPages(UrlContainerInterface $urls): void
{
$this->fromFeuilleId(Feuille::ID_FEUILLE_HEADER, $urls);
$this->fromFeuilleId(Feuille::ID_FEUILLE_FOOTER, $urls);
$this->fromFeuilleId(Feuille::ID_FEUILLE_NOS_ACTIONS, $urls);
$this->fromFeuilleId(Feuille::ID_FEUILLE_ANTENNES, $urls);
$this->fromFeuilleId(Feuille::ID_FEUILLE_ASSOCIATION, $urls);
}

private function fromFeuilleId(int $id, UrlContainerInterface $urls): void
{
$branche = new Branche();

$leafs = $branche->feuillesEnfants($id);
foreach ($leafs as $leaf) {
if (!$leaf['lien'] || 0 !== strpos($leaf['lien'], 'http')) {
continue;
}

$urls->addUrl(
new UrlConcrete($leaf['lien']),
'default'
);
}
}
}

0 comments on commit cf646d3

Please sign in to comment.