<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\Controller;
use AppBundle\QueryType\LocationQueryType;
use AppBundle\QueryType\SitemapQueryType;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class SeoController
* @package AppBundle\Controller
*/
class SeoController extends Controller
{
private $sitemapQueryType;
private $locationQueryType;
public function __construct(
SitemapQueryType $sitemapQueryType,
LocationQueryType $locationQueryType
) {
$this->sitemapQueryType = $sitemapQueryType;
$this->locationQueryType =$locationQueryType;
}
/**
* @Route("/sitemap.xml", name="sitemap", defaults={"_format" : "xml" })
*/
public function sitemapAction()
{
$rootLocationId = $this->getConfigResolver()->getParameter('content.tree_root.location_id');
$locationService = $this->get('ezpublish.api.service.location');
$searchService = $this->get('ezpublish.api.service.search');
$rootLocation = $locationService->loadLocation($rootLocationId);
$excludedLocations = $this->excludedLocations($rootLocationId);
$parameters = ['rootLocation' => $rootLocation, 'excludedLocations' => $excludedLocations];
$query = $this->sitemapQueryType->getQuery($parameters);
$query->performCount = false;
$query->limit = 1000;
$searchResults = $searchService->findLocations($query);
$response = $this->render('sitemap.xml.twig', ['searchHits' => $searchResults->searchHits]);
$response->setMaxAge(68000);
$response->setPublic();
$response->headers->add(['X-Location-Id' => $rootLocationId]);
return $response;
}
/**
* @Route("/robots.txr", name="robots", defaults={"_format" : "txt" })
*/
public function robotsAction(): Response
{
$response = $this->render('robots.txt.twig');
$response->setMaxAge(68000);
$response->setPublic();
return $response;
}
private function excludedLocations($locationId)
{
$searchService = $this->get('ezpublish.api.service.search');
$parameters = ['locationId' => $locationId, 'identifier' => 'serie'];
$locationQuery = $this->locationQueryType->getQuery($parameters);
$results = $searchService->findLocations($locationQuery);
return $results->searchHits;
}
}