src/AppBundle/Controller/SeoController.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * Copyright (c) 2017. The Cocktail
  5.  *
  6.  * This file is part of the Movistar Originales package.
  7.  *
  8.  */
  9. namespace AppBundle\Controller;
  10. use AppBundle\QueryType\LocationQueryType;
  11. use AppBundle\QueryType\SitemapQueryType;
  12. use eZ\Bundle\EzPublishCoreBundle\Controller;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * Class SeoController
  17.  * @package AppBundle\Controller
  18.  */
  19. class SeoController extends Controller
  20. {
  21.     private $sitemapQueryType;
  22.     private $locationQueryType;
  23.     public function __construct(
  24.         SitemapQueryType $sitemapQueryType,
  25.         LocationQueryType $locationQueryType
  26.     ) {
  27.         $this->sitemapQueryType $sitemapQueryType;
  28.         $this->locationQueryType =$locationQueryType;
  29.     }
  30.     /**
  31.      * @Route("/sitemap.xml", name="sitemap", defaults={"_format" : "xml" })
  32.      */
  33.     public function sitemapAction()
  34.     {
  35.         $rootLocationId $this->getConfigResolver()->getParameter('content.tree_root.location_id');
  36.         $locationService $this->get('ezpublish.api.service.location');
  37.         $searchService =  $this->get('ezpublish.api.service.search');
  38.         $rootLocation $locationService->loadLocation($rootLocationId);
  39.         $excludedLocations $this->excludedLocations($rootLocationId);
  40.         $parameters =  ['rootLocation' => $rootLocation'excludedLocations' => $excludedLocations];
  41.         $query $this->sitemapQueryType->getQuery($parameters);
  42.         $query->performCount false;
  43.         $query->limit 1000;
  44.         $searchResults $searchService->findLocations($query);
  45.         $response $this->render('sitemap.xml.twig', ['searchHits' => $searchResults->searchHits]);
  46.         $response->setMaxAge(68000);
  47.         $response->setPublic();
  48.         $response->headers->add(['X-Location-Id' => $rootLocationId]);
  49.         return $response;
  50.     }
  51.     /**
  52.      * @Route("/robots.txr", name="robots", defaults={"_format" : "txt" })
  53.      */
  54.     public function robotsAction(): Response
  55.     {
  56.         $response $this->render('robots.txt.twig');
  57.         $response->setMaxAge(68000);
  58.         $response->setPublic();
  59.         return $response;
  60.     }
  61.     private function excludedLocations($locationId)
  62.     {
  63.         $searchService $this->get('ezpublish.api.service.search');
  64.         $parameters = ['locationId' => $locationId'identifier' => 'serie'];
  65.         $locationQuery $this->locationQueryType->getQuery($parameters);
  66.         $results $searchService->findLocations($locationQuery);
  67.         return $results->searchHits;
  68.     }
  69. }