src/Service/SEOService.php line 46
<?php
namespace App\Service;
use App\Entity\Category;
use App\Entity\Magazine;
use App\Entity\News;
use App\Entity\Product;
use App\Entity\ProductCategory;
use App\Entity\ProductType;
use App\Entity\Recipe;
use App\Repository\CategoryRepository;
use App\Repository\MagazineRepository;
use App\Repository\NewsRepository;
use App\Repository\ProductCategoryRepository;
use App\Repository\ProductRepository;
use App\Repository\ProductTypeRepository;
use App\Repository\RecipeRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
class SEOService
{
/** @var EntityManager */
private $em;
/** @var Router */
private $router;
/** @var Translator */
private $translator;
public function __construct(EntityManagerInterface $em, RouterInterface $router, TranslatorInterface $translator)
{
$this->em = $em;
$this->router = $router;
$this->translator = $translator;
}
/**
* Genera dinamicamente la sitemap
*/
public function generateSitemap($locale)
{
$xml = new \SimpleXMLElement('<urlset/>');
$xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->addAttribute('xmlns:xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
// PAGINE FISSE
$baseRoutes = [
['homepage', 1.00],
// ['app_configurator', 0.90],
['app_customer_service', 0.70],
['about', 0.70],
['app_faq', 0.50],
];
if (in_array($locale, [
'it',
])) {
$baseRoutes = array_merge($baseRoutes, [
['app_wheretobuy', 0.80],
['app_magazine_index', 0.70],
['app_news_index', 0.70],
['app_sponsor_index', 0.70],
['app_recipes', 0.70],
['app_work_with_us', 0.40],
]);
}
foreach ($baseRoutes as $baseRoute) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate($baseRoute[0], [
'_locale' => $locale
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', $baseRoute[1]);
}
// PRODUCTS
/** @var ProductTypeRepository $productTypeRepo */
$productTypeRepo = $this->em->getRepository(ProductType::class);
$productTypes = $productTypeRepo->findBy([
'locale' => $locale,
'enabled' => true,
]);
/** @var ProductType $productType */
foreach ($productTypes as $productType) {
if ($locale == 'it' && $productType->getSlug() == 'ortofrutta') {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_show_category_ortofrutta', [
'_locale' => $locale,
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
} elseif ($locale == 'it' && $productType->getSlug() == 'kroccanti') {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('kroccanti', [
'_locale' => $locale,
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
} else {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_show_category', [
'_locale' => $locale,
'category_slug' => $productType->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
}
/** @var ProductRepository $productRepo */
$productRepo = $this->em->getRepository(Product::class);
$products = $productRepo->findBy([
'locale' => $locale,
'isEnabled' => true,
]);
/** @var Product $product */
foreach ($products as $product) {
if ($locale == 'it' && $product->getType()->getSlug() == 'ortofrutta') {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_show_product_ortofrutta', [
'_locale' => $locale,
'product_slug' => $product->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
// } elseif ($locale == 'it' && $product->getType()->getSlug() == 'kroccanti') {
// $urlXml = $xml->addChild('url');
// $url = $this->router->generate('app_show_product', [
// '_locale' => $locale,
// 'category_slug' => $product->getType()->getProductType()->getSlug(),
// 'product_slug' => $product->getSlug()
// ], Router::ABSOLUTE_URL);
// $urlXml->addChild('loc', $url);
// $urlXml->addChild('changefreq', 'weekly');
// $urlXml->addChild('priority', 0.80);
} else {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_show_product', [
'_locale' => $locale,
'category_slug' => $product->getType()->getProductType()->getSlug(),
'product_slug' => $product->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
}
// MAGAZINE
if (in_array($locale, [
'it',
])) {
/** @var ProductRepository $productRepo */
$magazineRepo = $this->em->getRepository(Magazine::class);
$magazines = $magazineRepo->findBy([
'locale' => $locale,
'isEnabled' => true,
'type' => 'MAGAZINE',
]);
/** @var Magazine $magazine */
foreach ($magazines as $magazine) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_magazine_detail', [
'_locale' => $locale,
'slug' => $magazine->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
$magazines = $magazineRepo->findBy([
'locale' => $locale,
'isEnabled' => true,
'type' => 'SPONSOR',
]);
/** @var Magazine $magazine */
foreach ($magazines as $magazine) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_sponsor_detail', [
'_locale' => $locale,
'slug' => $magazine->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
$magazines = $magazineRepo->findBy([
'locale' => $locale,
'isEnabled' => true,
'type' => 'NEWS',
]);
/** @var Magazine $magazine */
foreach ($magazines as $magazine) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_news_detail', [
'_locale' => $locale,
'slug' => $magazine->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
}
// RECIPES
if (in_array($locale, [
'it',
])) {
/** @var RecipeRepository $recipeRepo */
$recipeRepo = $this->em->getRepository(Recipe::class);
$recipes = $recipeRepo->findBy([
'locale' => $locale,
'isEnabled' => true,
]);
/** @var Recipe $recipe */
foreach ($recipes as $recipe) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate('app_recipes_detail', [
'_locale' => $locale,
'slug' => $recipe->getSlug()
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', 0.80);
}
}
return $xml->asXML();
}
/**
* Genera dinamicamente la sitemap
*/
public function generateSitemapDE($locale)
{
$xml = new \SimpleXMLElement('<urlset/>');
$xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->addAttribute('xmlns:xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
// PAGINE FISSE
$baseRoutes = [
['homepage', 1.00]
];
foreach ($baseRoutes as $baseRoute) {
$urlXml = $xml->addChild('url');
$url = $this->router->generate($baseRoute[0], [
'_locale' => $locale
], Router::ABSOLUTE_URL);
$urlXml->addChild('loc', $url);
$urlXml->addChild('changefreq', 'weekly');
$urlXml->addChild('priority', $baseRoute[1]);
}
return $xml->asXML();
}
}