src/EventSubscriber/Seo/SeoSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Seo;
  3. use Doctrine\ORM\EntityManager;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class SeoSubscriber implements EventSubscriberInterface
  9. {
  10.     private $entityManager;
  11.     private $twig;
  12.     private $siteName;
  13.     /**
  14.      * SeoSubscriber constructor.
  15.      * @param EntityManager $entityManager
  16.      * @param \Twig_Environment $twig
  17.      * @param string $siteName
  18.      */
  19.     public function __construct(EntityManager $entityManager, \Twig_Environment $twigstring $siteName)
  20.     {
  21.         $this->entityManager    $entityManager;
  22.         $this->twig             $twig;
  23.         $this->siteName         $siteName;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::REQUEST => 'onKernelRequest',
  29.         ];
  30.     }
  31.     /**
  32.      * @param GetResponseEvent $event
  33.      */
  34.     public function onKernelRequest(GetResponseEvent $event): void
  35.     {
  36.         $request $event->getRequest();
  37.         // Si on est dans l'admin, on sort de la fonction
  38.         if (false !== strpos($request->get('_route'), 'admin_') || false !== strpos($request->get('_route'), 'sonata')) {
  39.             return;
  40.         }
  41.         // Si cê n'est pas la requête principale, on sort de la fonction
  42.         if (!$event->isMasterRequest() || $request->isXmlHttpRequest()) {
  43.             return;
  44.         }
  45.         // On récupère les infos nécessaires pour identifier la ligne dans la table seo
  46.         $entityId   $request->get('id'false);
  47.         $entitySlug $request->get('slug'false);
  48.         $entityName $request->get('seoEntity');
  49.         $metasToBind = [
  50.             'entityId'              => $entityId,
  51.             'entityName'            => $entityName,
  52.             'metaTitle'             => $this->siteName,
  53.             'metaDescription'       => null,
  54.             'metaKeyword'           => null,
  55.             'urlCanonical'          => null,
  56.             'robotIndex'            => 'index',
  57.             'robotFollow'           => 'follow',
  58.             'facebookDescription'   => null,
  59.             'image'                 => null,
  60.         ];
  61.         // Noindex sur les pages avec des paramètres dans l'URL
  62.         $filtres $request->query->get('search');
  63.         if (!is_null($filtres)) {
  64.             $metasToBind['robotIndex'] = 'noindex';
  65.             $metasToBind['robotFollow'] = 'nofollow';
  66.         }
  67.         if (empty($entityName)) {
  68.             $this->twig->addGlobal('metas'$metasToBind);
  69.             return;
  70.         }
  71.         $metasToBind['entityId'] = $entityId;
  72.         $metasToBind['entityName'] = $entityName;
  73.         // On récupère les données SEO
  74.         if (false !== $entityId) {
  75.             $params = ['id' => $entityId];
  76.         } elseif (false !== $entitySlug) {
  77.             $params = ['slug' => $entitySlug];
  78.         } else {
  79.             $this->twig->addGlobal('metas'$metasToBind);
  80.             return;
  81.         }
  82.         $locale $request->attributes->get('_locale');
  83.         $repository $this->entityManager->getRepository('App:'.$entityName);
  84.         if (method_exists($repository'findByTranslatedSlug') && (isset($params['slug']) && !empty($params['slug']))) {
  85.             $entity $repository->findByTranslatedSlug($params['slug'], $locale);
  86.         } elseif (method_exists($repository'findById') && (isset($params['id']) && !empty($params['id']))) {
  87.             $entity $repository->findById($params['id'], $locale);
  88.         } else {
  89.             $entity $repository->findOneBy($params);
  90.         }
  91.         if (!method_exists($entity'getSeo')) {
  92.             $this->twig->addGlobal('metas'$metasToBind);
  93.             return;
  94.         }
  95.         $seo $entity->getSeo();
  96.         // Si on a rien en base de données
  97.         if (null === $seo) {
  98.             $this->twig->addGlobal('metas'$metasToBind);
  99.             return;
  100.         }
  101.         // Si on a une redirection 301 on l'exécute
  102.         if (!is_null($seo->getSeoParametre())) {
  103.             if (!empty($seo->getSeoParametre()->getRedirection301())) {
  104.                 $event->setResponse(new RedirectResponse($seo->getSeoParametre()->getRedirection301(), 301));
  105.             }
  106.         }
  107.         if (!empty($seo->getMetaTitle())) {
  108.             $siteName $seo->getMetaTitle();
  109.             // On récupère la page si présent dans l'url
  110.             $page $request->get('pageNumber'false);
  111.             if (false !== $page && $page 1) {
  112.                 $siteName .= ' - Page '.$page;
  113.             }
  114.             $metasToBind['metaTitle'] = $siteName;
  115.         }
  116.         if (!empty($seo->getMetaDescription())) {
  117.             $metasToBind['metaDescription'] = $seo->getMetaDescription();
  118.         }
  119.         if (!empty($seo->getMetaKeywords())) {
  120.             $metasToBind['metaKeyword'] = $seo->getMetaKeywords();
  121.         }
  122.         if (!is_null($seo->getSeoParametre())) {
  123.             if (!empty($seo->getSeoParametre()->getUrlCanonical())) {
  124.                 $metasToBind['urlCanonical'] = $seo->getSeoParametre()->getUrlCanonical();
  125.             }
  126.             if (!empty($seo->getSeoParametre()->getRobotIndex())) {
  127.                 $metasToBind['robotIndex'] = $seo->getSeoParametre()->getRobotIndex();
  128.             }
  129.             if (!empty($seo->getSeoParametre()->getRobotFollow())) {
  130.                 $metasToBind['robotFollow'] = $seo->getSeoParametre()->getRobotFollow();
  131.             }
  132.         }
  133.         if (!is_null($seo->getSeoReseauxSociaux())) {
  134.             if (!empty($seo->getSeoReseauxSociaux()->getFacebookDescription())) {
  135.                 $metasToBind['facebookDescription'] = $seo->getSeoReseauxSociaux()->getFacebookDescription();
  136.             }
  137.             if (!is_null($seo->getSeoReseauxSociaux()->getImage())) {
  138.                 $metasToBind['image'] = $seo->getSeoReseauxSociaux()->getImage();
  139.             }
  140.         }
  141.         $this->twig->addGlobal('metas'$metasToBind);
  142.         return;
  143.     }
  144. }