src/Sitemap/Controller/SitemapController.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Klaravik\Sitemap\Controller;
  4. use Klaravik\Controller\Controller;
  5. use Klaravik\Sitemap\Service\SitemapServiceInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class SitemapController extends Controller
  8. {
  9.     private const CACHE_MAX_AGE 3600;
  10.     private SitemapServiceInterface $sitemapService;
  11.     public function __construct(SitemapServiceInterface $sitemapService)
  12.     {
  13.         $this->sitemapService $sitemapService;
  14.     }
  15.     /**
  16.      * Main sitemap index (sitemap.xml).
  17.      */
  18.     public function index(): Response
  19.     {
  20.         $xml $this->sitemapService->generateSitemapIndexXml(
  21.             $this->sitemapService->getSitemapIndex()
  22.         );
  23.         return $this->createXmlResponse($xml);
  24.     }
  25.     /**
  26.      * Pages sitemap (pages.xml).
  27.      */
  28.     public function pages(): Response
  29.     {
  30.         $xml $this->sitemapService->generateSitemapXml(
  31.             $this->sitemapService->getPagesSitemap()
  32.         );
  33.         return $this->createXmlResponse($xml);
  34.     }
  35.     /**
  36.      * Categories sitemap (categories.xml).
  37.      */
  38.     public function categories(): Response
  39.     {
  40.         $xml $this->sitemapService->generateSitemapXml(
  41.             $this->sitemapService->getCategoriesSitemap()
  42.         );
  43.         return $this->createXmlResponse($xml);
  44.     }
  45.     /**
  46.      * Active auctions sitemap (active_auctions.xml).
  47.      */
  48.     public function activeAuctions(): Response
  49.     {
  50.         $xml $this->sitemapService->generateSitemapXml(
  51.             $this->sitemapService->getActiveAuctionsSitemap()
  52.         );
  53.         return $this->createXmlResponse($xml);
  54.     }
  55.     /**
  56.      * Closed auctions index (closed_auctions.xml).
  57.      */
  58.     public function closedAuctionsIndex(): Response
  59.     {
  60.         $xml $this->sitemapService->generateSitemapIndexXml(
  61.             $this->sitemapService->getClosedAuctionsIndex()
  62.         );
  63.         return $this->createXmlResponse($xml);
  64.     }
  65.     /**
  66.      * Closed auctions for a specific month (closedAuctions{YYYYMM}.xml).
  67.      */
  68.     public function closedAuctionsMonth(string $date): Response
  69.     {
  70.         $items $this->sitemapService->getClosedAuctionsForMonth($date);
  71.         if ($items === null) {
  72.             throw $this->createNotFoundException('No closed auctions for this month');
  73.         }
  74.         $xml $this->sitemapService->generateSitemapXml($items);
  75.         return $this->createXmlResponse($xml);
  76.     }
  77.     /**
  78.      * Create XML response with proper headers and caching.
  79.      */
  80.     private function createXmlResponse(string $xml): Response
  81.     {
  82.         $response = new Response($xml);
  83.         $response->headers->set('Content-Type''application/xml; charset=UTF-8');
  84.         $response->setPublic();
  85.         $response->setMaxAge(self::CACHE_MAX_AGE);
  86.         $response->headers->set('X-Robots-Tag''noindex');
  87.         return $response;
  88.     }
  89. }