src/Service/PathBuilder.php line 112

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\Actualite;
  13. use App\Entity\Banniere;
  14. use App\Entity\Etablissement;
  15. use Cocur\Slugify\Slugify;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Routing\RouterInterface;
  18. final class PathBuilder
  19. {
  20.     /** @var RouterInterface */
  21.     private $router;
  22.     /** @var Slugify */
  23.     private $slugifier;
  24.     /** @var UserTransient */
  25.     private $userTransient;
  26.     public function __construct(RouterInterface $routerSlugify $slugifierUserTransient $userTransient)
  27.     {
  28.         $this->router $router;
  29.         $this->slugifier $slugifier;
  30.         $this->userTransient $userTransient;
  31.     }
  32.     // ----- Établissement -----------------------------------------------------
  33.     public function buildAnnuaire($quoi$lieu, array $parametres = [], string $route nullint $referenceType null): string
  34.     {
  35.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  36.         // Lien vers la liste de mots cles
  37.         $quoi $quoi instanceof QuoiInterface $quoi->getSlug() : (string) $quoi;
  38.         if (!$quoi) {
  39.             return $this->router->generate(
  40.                 'annuaire',
  41.                 $parametres,
  42.                 $referenceType
  43.             );
  44.         }
  45.         // Lien vers la page d'accueil d'un "quoi"
  46.         $lieu $lieu instanceof LieuInterface $lieu->getSlug() : (string) $lieu;
  47.         if (!$lieu) {
  48.             return $this->router->generate('annuaire_quoi'array_merge($parametres, [
  49.                 'quoi' => $quoi,
  50.             ]), $referenceType);
  51.         }
  52.         $route = !$route $this->userTransient->get(UserTransient::ROUTE_ANNUAIRE) : $route;
  53.         return $this->router->generate($routearray_merge($parametres, [
  54.             'quoi' => $quoi,
  55.             'lieu' => $lieu,
  56.         ]), $referenceType);
  57.     }
  58.     public function buildAnnuaireCarte($quoi$lieu, array $parametres = [], int $referenceType null): string
  59.     {
  60.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  61.         return $this->buildAnnuaire($quoi$lieu$parametres'annuaire_carte'$referenceType);
  62.     }
  63.     public function buildAnnuaireListe($quoi$lieu, array $parametres = [], int $referenceType null): string
  64.     {
  65.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  66.         return $this->buildAnnuaire($quoi$lieu$parametres'annuaire_liste'$referenceType);
  67.     }
  68.     public function buildFiche(Etablissement $etablissement, array $parametres = [], int $referenceType null): string
  69.     {
  70.         // TODO : lancer la cmd app:sync:etablissement:path lors de la 1ère utilisation, sinon pour info le consommateur va se charger d'init cet attribut
  71.         // PB : ça ne gère pas le 3ème param $referenceType ! On laisse tomber cette optimisation
  72.         /*
  73.         if ($etablissement->getPath() && $etablissement->getPath() != "") {
  74.             $url = $etablissement->getPath();
  75.             if ( $parametres && count($parametres) > 0 ) {
  76.                 $queryString = http_build_query($parametres);
  77.                 $url = $url . '?' . $queryString;
  78.             }
  79.             return $url;
  80.         }
  81.         */
  82.         
  83.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  84.         $denomination =
  85.             $etablissement->getDenomination() ??
  86.             $etablissement->getDenominationAbregee() ??
  87.             $etablissement->getDenominationSigle();
  88.         $ville $etablissement->getVille();
  89.         $slug $this->slugifier->slugify(implode('-'array_filter([
  90.             $denomination,
  91.             $ville $ville->getLibelle() : null,
  92.             // $ville ? $ville->getCodePostal() : null,
  93.         ])));
  94.         return $this->router->generate('fiche'array_merge($parametres, [
  95.             'id' => $etablissement->getId(),
  96.             'slug' => $slug ?: 'etablissement',
  97.         ]), $referenceType);
  98.     }
  99.     public function buildFicheInternat(Etablissement $etablissement, array $parametres = [], int $referenceType null): string
  100.     {
  101.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  102.         $denomination =
  103.             $etablissement->getDenomination() ??
  104.             $etablissement->getDenominationAbregee() ??
  105.             $etablissement->getDenominationSigle();
  106.         $ville $etablissement->getVille();
  107.         $slug $this->slugifier->slugify(implode('-'array_filter([
  108.             $denomination,
  109.             $ville $ville->getLibelle() : null,
  110.             // $ville ? $ville->getCodePostal() : null,
  111.         ])));
  112.         return $this->router->generate('fiche'array_merge($parametres, [
  113.             'id' => $etablissement->getInternat(),
  114.             'slug' => $slug ?: 'etablissement',
  115.         ]), $referenceType);
  116.     }
  117.     public function buildFicheExterne(Etablissement $etablissement): ?string
  118.     {
  119.         $url $etablissement->getUrl();
  120.         if (!$url) {
  121.             return null;
  122.         }
  123.         // Vérifie URL
  124.         $pieces parse_url($url);
  125.         if (!$pieces) {
  126.             return null;
  127.         }
  128.         // Vérifie domaines bloqué
  129.         $blacklist '#('.implode('|', [
  130.             '^paris\.fr$',
  131.             '\.paris\.fr$',
  132.             '\.adseniors\.com$',
  133.         ]).')#';
  134.         if (preg_match($blacklist$pieces['host'])) {
  135.             return $url;
  136.         }
  137.         // Ajoute utm_source, si possible
  138.         parse_str($pieces['query'] ?? ''$query);
  139.         if (empty($query['utm_source'])) {
  140.             $query['utm_source'] = 'enseignement-prive.info';
  141.         }
  142.         return
  143.             (!empty($pieces['scheme']) ? $pieces['scheme'].'://' '').
  144.             (!empty($pieces['host']) ? $pieces['host'] : '').
  145.             (!empty($pieces['port']) ? ':'.$pieces['port'] : '').
  146.             (!empty($pieces['path']) ? ':'.$pieces['path'] : '').
  147.             '?'.http_build_query($query).
  148.             (!empty($pieces['fragment']) ? '#'.$pieces['fragment'] : '');
  149.     }
  150.     public function buildLogoListe(?string $identifiant): ?string
  151.     {
  152.         if (!$identifiant) {
  153.             return null;
  154.         }
  155.         // return sprintf(
  156.         //     'https://www.enseignement-prive.info/jouve/jpg_logos_liste/%s.jpg',
  157.         //     $identifiant
  158.         // );
  159.         return sprintf(
  160.             'https://backoffice-jouve.onpc.fr/ano-images/logo-liste/%s.jpg',
  161.             $identifiant
  162.         );
  163.     }
  164.     public function buildLogoFiche(?string $identifiant): ?string
  165.     {
  166.         if (!$identifiant) {
  167.             return null;
  168.         }
  169.         // return sprintf(
  170.         //     'https://www.enseignement-prive.info/jouve/jpg_logos_fiche/%s.jpg',
  171.         //     $identifiant
  172.         // );
  173.         return sprintf(
  174.             'https://backoffice-jouve.onpc.fr/ano-images/logo-fiche/%s.jpg',
  175.             $identifiant
  176.         );
  177.     }
  178.     public function buildPhoto(?string $identifiant): ?string
  179.     {
  180.         if (!$identifiant) {
  181.             return null;
  182.         }
  183.         // return sprintf(
  184.         //     'https://www.enseignement-prive.info/jouve/jpg_photos_fiche/%s.jpg',
  185.         //     $identifiant
  186.         // );
  187.         return sprintf(
  188.             'https://backoffice-jouve.onpc.fr/ano-images/photo/%s.jpg',
  189.             $identifiant
  190.         );
  191.     }
  192.     // ----- Bannière ----------------------------------------------------------
  193.     public function buildBanniere(?Banniere $banniere): ?string
  194.     {
  195.         if (!$banniere) {
  196.             return null;
  197.         }
  198.         $image $banniere->getImage();
  199.         if (!$image) {
  200.             return null;
  201.         }
  202.         return sprintf(
  203.             'https://backoffice-jouve.onpc.fr/ano/mep/images/bannieres/%s',
  204.             $image
  205.         );
  206.     }
  207.     // ----- Actualite ---------------------------------------------------------
  208.     public function buildActualiteEtablissement(Actualite $actualite, array $parametres = [], int $referenceType null): string
  209.     {
  210.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  211.         $etablissement $actualite->getEtablissement();
  212.         if ($etablissement)
  213.             return $this->buildFiche($etablissement);
  214.         else
  215.             return $this->router->generate('homepage');
  216.     }
  217.     public function buildActualiteEtablissementAmp(Actualite $actualite, array $parametres = [], int $referenceType null): string
  218.     {
  219.         $referenceType null !== $referenceType $referenceType UrlGeneratorInterface::ABSOLUTE_PATH;
  220.         return $this->router->generate('actu-etab-amp'array_merge(
  221.             $parametres,
  222.             $this->buildActualiteEtablissementParameters($actualite)
  223.         ), $referenceType);
  224.     }
  225.     private function buildActualiteEtablissementParameters(Actualite $actualite): array
  226.     {
  227.         $date $actualite->getDatePublication();
  228.         $slug $this->slugifier->slugify($actualite->getTitre());
  229.         return [
  230.             'id' => $actualite->getId(),
  231.             'slug' => $slug,
  232.         ];
  233.     }
  234.     public function buildDefaultPath(Etablissement $e) {
  235.         if ($e->getTypeClient() == "internat")
  236.             return "/onglet/internat/";
  237.         if (null == $e->getType() || '' == $e->getType())
  238.             return "/#buildDefaultPath-404";
  239.         $niveau1 = [
  240.             '01-MATER' => 'ecole',
  241.             '02-ELEM' => 'ecole',
  242.             '03-PRIM' => 'ecole',
  243.             '04-COLL' => 'college',
  244.             '05-LGEN' => 'lycee',
  245.             '06-LTECH' => 'lycee',
  246.             '07-LEGT' => 'lycee',
  247.             '08-LAGRI' => 'professionnel',
  248.             '09-LPROF' => 'professionnel',
  249.             '10-LEAP' => 'professionnel',
  250.             '11-CFA' => 'superieur',
  251.             '12-SUP' => 'superieur',
  252.             '13-EDIST' => 'superieur',
  253.             '14-ELANG' => 'superieur',
  254.             '15-ERSCOL' => 'superieur',
  255.             '16-ESPEC' => 'superieur'
  256.         ];
  257.         $url "/onglet/" $niveau1[$e->getType()];
  258.         // TODO: spécifier la région|dpt|ville en fonction du type/onglet
  259.         return $url;
  260.     }
  261.     public function buildOngletPath(string $routestring $typeOngletstring $niveauOngletLieuInterface $lieu null) {
  262.         if ($typeOnglet != "superieur" ) {
  263.             $lieu $lieu instanceof LieuInterface $lieu->getSlug() : (string) $lieu;
  264.             if ($route === 'annuaire_liste_par_onglet' || $niveauOnglet === 'tout') {
  265.                 return $this->router->generate(
  266.                     'annuaire_liste_par_onglet',
  267.                     ['typeOnglet' => $typeOnglet'niveauOnglet' => $lieu $lieu $niveauOnglet],
  268.                     UrlGeneratorInterface::ABSOLUTE_URL
  269.                 );
  270.             }
  271.             else if ($route === 'annuaire_liste_par_onglet_localisation') {
  272.                 return $this->router->generate(
  273.                     'annuaire_liste_par_onglet_localisation',
  274.                     ['typeOnglet' => $typeOnglet'niveauOnglet' => $niveauOnglet'localisation' => $lieu],
  275.                     UrlGeneratorInterface::ABSOLUTE_URL
  276.                 );
  277.             }
  278.                 
  279.         }
  280.         
  281.     }
  282.     public function buildOngletPathEcole(string $typeOngletstring $niveauOngletLieuInterface $lieu null) {
  283.         if ($typeOnglet != "superieur" )
  284.             return $this->router->generate(
  285.                 'annuaire_liste_par_onglet_localisation',
  286.                 ['typeOnglet' => $typeOnglet'niveauOnglet' => $niveauOnglet 'localisation' => $lieu->getSlug()],
  287.                 UrlGeneratorInterface::ABSOLUTE_PATH
  288.             );
  289.     }
  290.     public function buildLocalisationPath($route$lieu): string
  291.     {
  292.         $lieu $lieu instanceof LieuInterface $lieu->getSlug() : (string) $lieu;
  293.         return $this->router->generate(
  294.             $route, ['lieu' => $lieu],
  295.             UrlGeneratorInterface::ABSOLUTE_URL
  296.         );
  297.     }
  298. }