<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\Core;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Events\Core\SeoPageAddEvent;
use App\Events\Core\MetaInfoAddEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Model\Core\SeoPage;
use Twig\Environment;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\SEO;
use App\Settings\SettingsManager;
use Symfony\Component\HttpFoundation\RequestStack;
class SeoSubscriber implements EventSubscriberInterface
{
protected $translator;
protected $twig;
protected $em;
protected $settingsManager;
protected $request;
public function __construct(
Environment $twig,
EntityManagerInterface $em,
TranslatorInterface $translator,
SettingsManager $settingsManager,
RequestStack $request
) {
$this->translator = $translator;
$this->twig = $twig;
$this->em = $em;
$this->settingsManager = $settingsManager;
$this->request = $request;
}
public static function getSubscribedEvents(): array
{
return [
SeoPageAddEvent::NAME => 'onSeoPageAddEvent',
MetaInfoAddEvent::NAME => 'onMetaInfoAddEvent',
];
}
public function onMetaInfoAddEvent(MetaInfoAddEvent $event): void
{
$configs = $this->settingsManager->get([
SettingsManager::SEO_ALLOW_INDEXING,
SettingsManager::SEO_DEFAULT_TITLE,
SettingsManager::SEO_DEFAULT_DESCRIPTION,
SettingsManager::CORE_SITE_NAME,
SettingsManager::SEO_DEFAULT_KEYWORDS,
SettingsManager::CORE_SITE_LOGO,
], true);
$metaTags = $event->getTags();
$params = [];
$seo = $this->em->getRepository(SEO::class)->findOneBy(['route' => $event->getRoute()]);
if ($seo) {
$seoTitle = $seo->getTitle();
$seoDescription = $seo->getDescription();
$seoKeywords = $seo->getKeywords();
$metaData = $seo->getMetaData();
$params = [
'%sitename%' => $configs[SettingsManager::CORE_SITE_NAME],
];
if ($event->getParams()) {
$params = array_merge($event->getParams(), $params);
}
if ($params) {
foreach ($params as $search => $replace) {
$seoTitle = str_replace($search, $replace, $seoTitle);
$seoDescription = str_replace($search, $replace, $seoDescription);
}
}
} else {
$seoTitle = str_replace('%sitename%', $configs[SettingsManager::CORE_SITE_NAME], $configs[SettingsManager::SEO_DEFAULT_TITLE]);
$seoDescription = str_replace('%sitename%', $configs[SettingsManager::CORE_SITE_NAME], $configs[SettingsManager::SEO_DEFAULT_DESCRIPTION]);
$seoKeywords = $configs[SettingsManager::SEO_DEFAULT_KEYWORDS];
$metaData = [
'<meta charset="UTF-8">',
'<meta content="width=device-width, initial-scale=1.0" name="viewport">',
];
if ((int) $configs[SettingsManager::SEO_ALLOW_INDEXING]) {
$metaData[] = '<meta name=”robots” content=”index, follow”>';
} else {
$metaData[] = '<meta name="robots" content="noindex">';
}
$metaData = implode('', $metaData);
}
if (!isset($metaTags['og:type'])) {
$metaTags['og:type'] = 'website';
}
if (!isset($metaTags['og:image']) && $configs[SettingsManager::CORE_SITE_LOGO]) {
$metaTags['og:image'] = $this->request->getMasterRequest()->getSchemeAndHttpHost() . $configs[SettingsManager::CORE_SITE_LOGO];
}
$params['seoTitle'] = $seoTitle;
$params['seoDescription'] = $seoDescription;
$params['seoKeywords'] = $seoKeywords;
$params['metaData'] = $metaData;
$metaTags = array_merge($metaTags, [
'og:url' => $this->request->getMasterRequest()->getUri(),
'og:title' => $seoTitle,
'og:description' => $seoDescription,
]);
$params['metaTags'] = $metaTags;
$event->setSeo($params);
}
public function onSeoPageAddEvent(SeoPageAddEvent $event): void
{
$pages = [
(new SeoPage())->setName($this->translator->trans('Content Page', [], 'admin'))->setRoute('core_page_view')->setParams([
'%title%'
]),
(new SeoPage())->setName($this->translator->trans('Profile Page', [], 'admin'))->setRoute('profile')->setParams([
'%name%', '%type%'
]),
(new SeoPage())->setName($this->translator->trans('Profiles Page', [], 'admin'))->setRoute('profile_index'),
(new SeoPage())->setName($this->translator->trans('Contact Page', [], 'admin'))->setRoute('contact'),
(new SeoPage())->setName($this->translator->trans('Dashboard Page', [], 'admin'))->setRoute('dashboard'),
(new SeoPage())->setName($this->translator->trans('HomePage', [], 'admin'))->setRoute('home'),
];
foreach ($pages as $page) {
$event->addPage($page);
}
}
}