src/Service/Slugify.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (C) Office National de Publication et de Communication - All Rights Reserved
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited by law
  7.  * This file is proprietary and confidential
  8.  */
  9. namespace App\Service;
  10. use App\Common\LieuInterface;
  11. use App\Common\QuoiInterface;
  12. use App\Entity\AncienneRegion;
  13. use App\Entity\Departement;
  14. use App\Entity\QuoiMotCle;
  15. use App\Entity\Region;
  16. use App\Entity\Ville;
  17. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. final class Slugify
  20. {
  21.     private const CACHE_LIFETIME 60;
  22.     private const ALIAS_QUOI = [
  23.         'ehpad' => 'ehpad-maisons-de-retraite',
  24.         'formation-sanitaire-et-social' => 'formation',
  25.         'handicap-adultes' => 'adultes-handicapes',
  26.     ];
  27.     /** @var EntityManagerInterface */
  28.     private $entityManager;
  29.     public function __construct(EntityManagerInterface $entityManager)
  30.     {
  31.         $this->entityManager $entityManager;
  32.     }
  33.     public function revealQuoi(string $slug): QuoiInterface
  34.     {
  35.         $slug = \strtolower($slug);
  36.         $slug Tools::stripAccents($slug);
  37.         // Crée un objet qui représente la recherche par texte
  38.         return new QuoiMotCle($slug);
  39.     }
  40.     public function revealLieu(string $slug): ?LieuInterface
  41.     {
  42.         $slug = \strtolower($slug);
  43.         $slug Tools::stripAccents($slug);
  44.         $classes = [Departement::class, Region::class, AncienneRegion::class];
  45.         foreach ($classes as $class) {
  46.             $lieu $this->findBySlug($class$slug);
  47.             if ($lieu) {
  48.                 return $lieu;
  49.             }
  50.         }
  51.         return $this->findVilleBySlug($slug);
  52.     }
  53.     public function getAliasQuoi(string $slug): ?string
  54.     {
  55.         return self::ALIAS_QUOI[$slug] ?? null;
  56.     }
  57.     private function findVilleBySlug(string $slug): ?Ville
  58.     {
  59.         /** @var ServiceEntityRepository $repository */
  60.         $repository $this->entityManager->getRepository(Ville::class);
  61.         return $repository
  62.             ->createQueryBuilder('v')
  63.             ->where('v.slug = :slug')
  64.             ->setParameter('slug'$slug)
  65.             ->setMaxResults(1)
  66.             ->getQuery()
  67.             ->useResultCache(trueself::CACHE_LIFETIME)
  68.             ->getOneOrNullResult();
  69.     }
  70.     private function findBySlug(string $classstring $slug)
  71.     {
  72.         /** @var ServiceEntityRepository $repository */
  73.         $repository $this->entityManager->getRepository($class);
  74.         $entities $repository
  75.             ->createQueryBuilder('x')
  76.             ->getQuery()
  77.             ->useResultCache(trueself::CACHE_LIFETIME)
  78.             ->getResult();
  79.         foreach ($entities as $entity) {
  80.             if ($entity->getSlug() === $slug) {
  81.                 return $entity;
  82.             }
  83.         }
  84.     }
  85. }