src/Hitso/Bundle/SeoBundle/Service/SitemapGenerator.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\SeoBundle\Service;
  4. use Hitso\Bundle\SeoBundle\Provider\ProviderInterface;
  5. use Hitso\Bundle\SeoBundle\Sitemap\SitemapIndex;
  6. use Hitso\Bundle\SeoBundle\Sitemap\SitemapSectionPage;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. /**
  9.  * Class SitemapGenerator
  10.  *
  11.  * @package Hitso\Bundle\SeoBundle\Service
  12.  */
  13. class SitemapGenerator
  14. {
  15.     /**
  16.      * @var ProviderInterface[]
  17.      */
  18.     private $providers = [];
  19.     /**
  20.      * @return SitemapIndex
  21.      */
  22.     public function generateIndex()
  23.     {
  24.         $index = new SitemapIndex();
  25.         foreach ($this->providers as $provider) {
  26.             if ($provider->getNumberOfPages() > 0) {
  27.                 $section $provider->getSection();
  28.                 $section->setPageCount($provider->getNumberOfPages());
  29.                 $index->addSection($section);
  30.             }
  31.         }
  32.         return $index;
  33.     }
  34.     /**
  35.      * @param string $sectionName
  36.      * @param int    $page
  37.      *
  38.      * @return SitemapSectionPage
  39.      */
  40.     public function generateSectionPage($sectionName$page)
  41.     {
  42.         $provider $this->getProvider($sectionName);
  43.         if (null === $provider) {
  44.             throw new NotFoundHttpException('No provider found for the given sitemap section');
  45.         }
  46.         return $provider->getPage($page);
  47.     }
  48.     /**
  49.      * @param ProviderInterface $provider
  50.      */
  51.     public function addProvider(ProviderInterface $provider)
  52.     {
  53.         $this->providers[$provider->getSectionName()] = $provider;
  54.     }
  55.     /**
  56.      * @param string $sectionName
  57.      *
  58.      * @return null|ProviderInterface
  59.      */
  60.     private function getProvider($sectionName)
  61.     {
  62.         return isset($this->providers[$sectionName]) ? $this->providers[$sectionName] : null;
  63.     }
  64. }