src/EventSubscriber/Core/SeoSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\Core;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use App\Events\Core\SeoPageAddEvent;
  12. use App\Events\Core\MetaInfoAddEvent;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use App\Model\Core\SeoPage;
  15. use Twig\Environment;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use App\Entity\SEO;
  18. use App\Settings\SettingsManager;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. class SeoSubscriber implements EventSubscriberInterface
  21. {
  22.     protected $translator;
  23.     protected $twig;
  24.     protected $em;
  25.     protected $settingsManager;
  26.     protected $request;
  27.     public function __construct(
  28.         Environment $twig,
  29.         EntityManagerInterface $em,
  30.         TranslatorInterface $translator,
  31.         SettingsManager $settingsManager,
  32.         RequestStack $request
  33.     ) {
  34.         $this->translator $translator;
  35.         $this->twig $twig;
  36.         $this->em $em;
  37.         $this->settingsManager $settingsManager;
  38.         $this->request $request;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             SeoPageAddEvent::NAME => 'onSeoPageAddEvent',
  44.             MetaInfoAddEvent::NAME => 'onMetaInfoAddEvent',
  45.         ];
  46.     }
  47.     public function onMetaInfoAddEvent(MetaInfoAddEvent $event): void
  48.     {
  49.         $configs $this->settingsManager->get([
  50.             SettingsManager::SEO_ALLOW_INDEXING,
  51.             SettingsManager::SEO_DEFAULT_TITLE,
  52.             SettingsManager::SEO_DEFAULT_DESCRIPTION,
  53.             SettingsManager::CORE_SITE_NAME,
  54.             SettingsManager::SEO_DEFAULT_KEYWORDS,
  55.             SettingsManager::CORE_SITE_LOGO,
  56.         ], true);
  57.         $metaTags $event->getTags();
  58.         $params = [];
  59.         $seo $this->em->getRepository(SEO::class)->findOneBy(['route' => $event->getRoute()]);
  60.         if ($seo) {
  61.             $seoTitle $seo->getTitle();
  62.             $seoDescription $seo->getDescription();
  63.             $seoKeywords $seo->getKeywords();
  64.             $metaData $seo->getMetaData();
  65.             $params = [
  66.                 '%sitename%' => $configs[SettingsManager::CORE_SITE_NAME],
  67.             ];
  68.             if ($event->getParams()) {
  69.                 $params array_merge($event->getParams(), $params);
  70.             }
  71.             if ($params) {
  72.                 foreach ($params as $search => $replace) {
  73.                     $seoTitle str_replace($search$replace$seoTitle);
  74.                     $seoDescription str_replace($search$replace$seoDescription);
  75.                 }
  76.             }
  77.         } else {
  78.             $seoTitle str_replace('%sitename%'$configs[SettingsManager::CORE_SITE_NAME], $configs[SettingsManager::SEO_DEFAULT_TITLE]);
  79.             $seoDescription str_replace('%sitename%'$configs[SettingsManager::CORE_SITE_NAME], $configs[SettingsManager::SEO_DEFAULT_DESCRIPTION]);
  80.             $seoKeywords $configs[SettingsManager::SEO_DEFAULT_KEYWORDS];
  81.             
  82.             $metaData = [
  83.                 '<meta charset="UTF-8">',
  84.                 '<meta content="width=device-width, initial-scale=1.0" name="viewport">',
  85.             ];
  86.             if ((int) $configs[SettingsManager::SEO_ALLOW_INDEXING]) {
  87.                 $metaData[] = '<meta name=”robots” content=”index, follow”>';
  88.             } else {
  89.                 $metaData[] = '<meta name="robots" content="noindex">';
  90.             }
  91.             $metaData implode(''$metaData);
  92.         }
  93.         if (!isset($metaTags['og:type'])) {
  94.             $metaTags['og:type'] = 'website';
  95.         }
  96.         if (!isset($metaTags['og:image']) && $configs[SettingsManager::CORE_SITE_LOGO]) {
  97.             $metaTags['og:image'] = $this->request->getMasterRequest()->getSchemeAndHttpHost() . $configs[SettingsManager::CORE_SITE_LOGO];
  98.         }
  99.         
  100.         $params['seoTitle'] = $seoTitle;
  101.         $params['seoDescription'] = $seoDescription;
  102.         $params['seoKeywords'] = $seoKeywords;
  103.         $params['metaData'] = $metaData;
  104.         $metaTags array_merge($metaTags, [
  105.             'og:url' => $this->request->getMasterRequest()->getUri(),
  106.             'og:title' => $seoTitle,
  107.             'og:description' => $seoDescription,
  108.         ]);
  109.         $params['metaTags'] = $metaTags;
  110.         $event->setSeo($params);
  111.     }
  112.     public function onSeoPageAddEvent(SeoPageAddEvent $event): void
  113.     {
  114.         $pages = [
  115.             (new SeoPage())->setName($this->translator->trans('Content Page', [], 'admin'))->setRoute('core_page_view')->setParams([
  116.             '%title%'
  117.             ]),
  118.             (new SeoPage())->setName($this->translator->trans('Profile Page', [], 'admin'))->setRoute('profile')->setParams([
  119.             '%name%''%type%'
  120.             ]),
  121.             (new SeoPage())->setName($this->translator->trans('Profiles Page', [], 'admin'))->setRoute('profile_index'),
  122.             (new SeoPage())->setName($this->translator->trans('Contact Page', [], 'admin'))->setRoute('contact'),
  123.             (new SeoPage())->setName($this->translator->trans('Dashboard Page', [], 'admin'))->setRoute('dashboard'),
  124.             (new SeoPage())->setName($this->translator->trans('HomePage', [], 'admin'))->setRoute('home'),
  125.         ];
  126.         foreach ($pages as $page) {
  127.             $event->addPage($page);
  128.         }
  129.     }
  130. }