<?php
declare(strict_types=1);
namespace Hitso\Bundle\SeoBundle\Service;
use Hitso\Bundle\SeoBundle\Provider\ProviderInterface;
use Hitso\Bundle\SeoBundle\Sitemap\SitemapIndex;
use Hitso\Bundle\SeoBundle\Sitemap\SitemapSectionPage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class SitemapGenerator
*
* @package Hitso\Bundle\SeoBundle\Service
*/
class SitemapGenerator
{
/**
* @var ProviderInterface[]
*/
private $providers = [];
/**
* @return SitemapIndex
*/
public function generateIndex()
{
$index = new SitemapIndex();
foreach ($this->providers as $provider) {
if ($provider->getNumberOfPages() > 0) {
$section = $provider->getSection();
$section->setPageCount($provider->getNumberOfPages());
$index->addSection($section);
}
}
return $index;
}
/**
* @param string $sectionName
* @param int $page
*
* @return SitemapSectionPage
*/
public function generateSectionPage($sectionName, $page)
{
$provider = $this->getProvider($sectionName);
if (null === $provider) {
throw new NotFoundHttpException('No provider found for the given sitemap section');
}
return $provider->getPage($page);
}
/**
* @param ProviderInterface $provider
*/
public function addProvider(ProviderInterface $provider)
{
$this->providers[$provider->getSectionName()] = $provider;
}
/**
* @param string $sectionName
*
* @return null|ProviderInterface
*/
private function getProvider($sectionName)
{
return isset($this->providers[$sectionName]) ? $this->providers[$sectionName] : null;
}
}