src/EventListener/PageSeoEventListener.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\MetaTemplates;
  4. use App\Event\PageEvent;
  5. use App\Service\ParameterService;
  6. use App\Util\Seo;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoGenerator;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. class PageSeoEventListener
  11. {
  12.     protected BasicSeoGenerator $basicSeoGenerator;
  13.     protected Seo $seoUtil;
  14.     protected EntityManagerInterface $entityManager;
  15.     protected ParameterBagInterface $parameterBag;
  16.     /**
  17.      * PageSeoEventListener constructor.
  18.      */
  19.     public function __construct(
  20.         BasicSeoGenerator $basicSeoGenerator,
  21.         Seo $seoUtil,
  22.         EntityManagerInterface $entityManager,
  23.         ParameterBagInterface $parameterBag,
  24.         private readonly ParameterService $parameterService,
  25.     ) {
  26.         $this->basicSeoGenerator $basicSeoGenerator;
  27.         $this->seoUtil $seoUtil;
  28.         $this->entityManager $entityManager;
  29.         $this->parameterBag $parameterBag;
  30.     }
  31.     public function onPagePreBuild(PageEvent $event)
  32.     {
  33.         if (!$page $event->getPage()) {
  34.             return;
  35.         }
  36.         $locale $event->getRequest()->getLocale();
  37.         $titleSuffixes $this->parameterService->toArray('site_title_suffix');
  38.         if (!empty($titleSuffixes[$locale])) {
  39.             $titleSuffix $titleSuffixes[$locale];
  40.         }
  41.         if (empty($titleSuffix)) {
  42.             $titleSuffix $this->parameterBag->get('site_company_name');
  43.         }
  44.         $lastResortFallbackTitle sprintf(
  45.             '%s | %s',
  46.             $page->getTitle(),
  47.             $titleSuffix
  48.         );
  49.         $metaTitle null;
  50.         if ($page->getMetaTitle()) {
  51.             // Sprintf for backwards compatibility from App\Util\Seo
  52.             $metaTitle sprintf(
  53.                 '%s | %s',
  54.                 $page->getMetaTitle(),
  55.                 $titleSuffix
  56.             );
  57.         }
  58.         $metaDescription $page->getMetaDescription();
  59.         // When the event has no entity or the entity does not implement OpenGraphInterface
  60.         // or the entity has no function to get the identifier, we cannot proceed
  61.         // the page's meta title and description are used
  62.         if (
  63.             null === $event->getEntity() ||
  64.             !method_exists($event->getEntity(), 'getMetaTitle') ||
  65.             !method_exists($event->getEntity(), 'getId')
  66.         ) {
  67.             if (!empty($event->getMetadata()['metaTitle'])) {
  68.                 $metaTitle sprintf(
  69.                     '%s | %s',
  70.                     $event->getMetadata()['metaTitle'],
  71.                     $titleSuffix
  72.                 );
  73.             }
  74.             if (!empty($event->getMetadata()['metaDescription'])) {
  75.                 $metaDescription sprintf(
  76.                     '%s | %s',
  77.                     $event->getMetadata()['metaDescription'],
  78.                     $titleSuffix
  79.                 );
  80.             }
  81.             $this->basicSeoGenerator
  82.                 ->setTitle($metaTitle ?? $lastResortFallbackTitle)
  83.                 ->setDescription(strip_tags($metaDescription))
  84.             ;
  85.             return;
  86.         }
  87.         $entity $event->getEntity();
  88.         $entityMetaTitle null;
  89.         if ($entity->getMetaTitle()) {
  90.             // Sprintf for backwards compatibility from App\Util\Seo
  91.             $entityMetaTitle sprintf(
  92.                 '%s | %s',
  93.                 $entity->getMetaTitle(),
  94.                 $titleSuffix
  95.             );
  96.         }
  97.         $entityMetaDescription null;
  98.         if (method_exists($event->getEntity(), 'getMetaDescription')) {
  99.             $entityMetaDescription $entity->getMetaDescription();
  100.         }
  101.         if (!$templates $this->entityManager->getRepository(MetaTemplates::class)
  102.             ->findOneBy(['objectClass' => \get_class($entity), 'foreignKey' => $entity->getId()])) {
  103.             $templates $this->entityManager->getRepository(MetaTemplates::class)
  104.                 ->findTemplatesForObjectClass(\get_class($entity));
  105.         }
  106.         if (!$templates) {
  107.             $title $this->getFallbackValue([
  108.                 $entityMetaTitle,
  109.                 $metaTitle,
  110.             ]);
  111.             $description $this->getFallbackValue([
  112.                 $entityMetaDescription,
  113.                 $metaDescription,
  114.             ]);
  115.             $this->basicSeoGenerator
  116.                 ->setTitle($title ?? $lastResortFallbackTitle)
  117.                 ->setDescription($description strip_tags($description) : '')
  118.             ;
  119.             return;
  120.         }
  121.         // Do not do sprintf's with company name, this can easily be managed in the templates
  122.         [$renderedMetaTitle$renderedMetaDescription] = $this->seoUtil->extractMetaData($templates$entity);
  123.         $title $this->getFallbackValue([
  124.             $entityMetaTitle,
  125.             $renderedMetaTitle,
  126.             $metaTitle,
  127.         ]);
  128.         $description $this->getFallbackValue([
  129.             $entityMetaDescription,
  130.             $renderedMetaDescription,
  131.             $metaDescription,
  132.         ]);
  133.         $this->basicSeoGenerator
  134.             ->setTitle($title ?? $lastResortFallbackTitle)
  135.             ->setDescription($description strip_tags($description) : '')
  136.         ;
  137.     }
  138.     /**
  139.      * @param string[] $values
  140.      */
  141.     private function getFallbackValue(array $values): ?string
  142.     {
  143.         $values array_filter($values);
  144.         if (empty($values)) {
  145.             return null;
  146.         }
  147.         return reset($values);
  148.     }
  149. }