src/Controller/Frontend/OdController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\model\Conveyance;
  4. use App\Entity\model\RouteSeo;
  5. use App\Entity\Stop;
  6. use App\Repository\RouteRepository;
  7. use App\Service\Client\ClientRepository;
  8. use App\Service\OdSeoService;
  9. use App\Service\UrlRewrite;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class OdController extends AbstractController {
  16.     // Check Router.js
  17.     //use in twig as const
  18.     public const REDIRECT_TAB 'tab'//autoload=0
  19.     public const REDIRECT_NONE 'none'//autoload=1
  20.     public const REDIRECT_FAKEURL 'fakeurl'//autoload=1
  21.     public const REDIRECT_URL 'url'//default, autoload=0
  22.     /** @required */
  23.     public UrlRewrite $rewriteService;
  24.     /** @required */
  25.     public ClientRepository $clientRepo;
  26.     /** @required */
  27.     public OdSeoService $seoService;
  28.     /** @required */
  29.     public RouteRepository $routeRepo;
  30.     /**
  31.      * @Route("/bus-{slug}-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_bus_od",
  32.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+", "slug" = "[a-zA-Z1-9\-_\/]+"},
  33.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport" = "bus"})
  34.      *
  35.      * @Route("/train/{slug}-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_train_od",
  36.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+", "slug" = "[a-zA-Z1-9\-_\/]+"},
  37.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport" = "train"}, options={"i18n_locales"={"fr", "es", "it", "en", "en_GB", "cs", "pl", "ru", "uk_UA"}})
  38.      *
  39.      * @Route("/covoiturage/{slug}-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_carpooling_od",
  40.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+", "slug" = "[a-zA-Z1-9\-_\/]+"},
  41.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport" = "carpooling"}, options={"i18n_locales"={"fr"}})
  42.      *
  43.      * @Route("/avion/vol-{slug}-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_flight_od",
  44.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+", "slug" = "[a-zA-Z1-9\-_\/]+"},
  45.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport" = "flight"}, options={"i18n_locales"={"fr"}})
  46.      * @Route("/avion/o-d-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_flight_od_fake",
  47.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+"},
  48.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport" = "flight"}, options={"i18n_locales"={"fr"}})
  49.      *
  50.      * @Route("/billet/{slug}-{depStopId}-{arrStopId}/{dateFrom}/{dateTo}", name="page_ticket_od",
  51.      *     requirements = {"depStopId" = "\d+", "arrStopId" = "\d+", "slug" = "[a-zA-Z1-9\-_\/]+"},
  52.      *     defaults={"dateFrom" = null, "dateTo" = null, "transport"="all"}, options={"i18n_locales"={"en","fr","es","it","cs","ru","uk_UA","tr","pl"}})
  53.      * // that list of lang is in widget_doc.html.twig
  54.      *
  55.      * @Entity("depStop", expr="repository.findNotRemovedTranslated(depStopId, _locale)")
  56.      * @Entity("arrStop", expr="repository.findNotRemovedTranslated(arrStopId, _locale)")
  57.      */
  58.     public function odAction(Request $rUrlRewrite $rewriteStop $depStopStop $arrStop, ?string $dateFrom, ?string $dateTostring $_localestring $transport) {
  59.         $noIndex $dateFrom !== null// no index if has dates
  60.         $currentUriNoQueryString self::getUriWithoutQueryString($r);
  61.         $expectedUri $this->rewriteService->generateFromAtoBUrl($depStop$arrStop$dateFrom$dateTofalse$_locale$transport);
  62.         if($currentUriNoQueryString !== $expectedUri) {
  63.             return $this->redirect($expectedUri);
  64.         }
  65.         $canonicalUrl $this->rewriteService->generateFromAtoBUrlWithoutDate($depStop$arrStopfalsenull$transport);
  66.         $seoOd $this->seoService->getSeoOd($depStop$arrStop$transport$noIndex$_locale);
  67.         $popularRoutes $noIndex ? [] : $this->getPopularRouteFrom($depStop$transport$_locale);
  68.         $infoStop =  $noIndex null $this->seoService->getInfoStop($arrStop->getId(), $transport$_locale);
  69.         $response $this->render('Default/odpage/odpage.html.twig', [
  70.             'depStop' => $depStop,
  71.             'arrStop' => $arrStop,
  72.             'search' => [
  73.                 'depStopId' => $depStop->getId(),
  74.                 'depStopName' => $depStop->getName(),
  75.                 'arrStopId' => $arrStop->getId(),
  76.                 'arrStopName' => $arrStop->getName(),
  77.                 'dateFrom' => $dateFrom,
  78.                 'dateTo' => $dateTo,
  79.                 'type' => $transport
  80.             ],
  81.             'redirect' => self::REDIRECT_FAKEURL//only change url in js
  82.             'odpage' => $rewrite->generateODWebappLink($transport),
  83.             'noIndex' => $noIndex,
  84.             'canonical' => $canonicalUrl,
  85.             'transport' => $transport,
  86.             'cheapest' => $seoOd->cheapest,
  87.             //commun seo content (bus & train)
  88.             'seoOd' => $seoOd,
  89.             //bus related content
  90.             'infostop' => $infoStop,
  91.             'popularRoutes' => $popularRoutes,
  92.         ]);
  93.         if(!$noIndexself::setCache($response);
  94.         return $response;
  95.     }
  96.     //TODO use varnish : https://www.slideshare.net/lisachenko/handling-10k-requests-per-second-with-symfony-and-varnish
  97.     /**
  98.      * Do not use Cache annotation because cache only in some case
  99.      */
  100.     protected static function setCache(Response $response) {
  101.         $date = new \DateTime();
  102.         $date->modify('+3600 seconds');
  103.         $response->setExpires($date);
  104.         //difference with $response->setSharedMaxAge(3600); ?
  105.         $response->setPublic();
  106.     }
  107.     public static function getUriWithoutQueryString(Request $r): string {
  108.         $currentUri $r->getUri();
  109.         $pos strpos($currentUri'?');
  110.         return $pos substr($currentUri,0,$pos) : $currentUri;
  111.     }
  112.     protected function generateAlternate(Stop $depStopStop $arrStop): array {
  113.         $locales = ['fr''es''en'];
  114.         $alternates = [];
  115.         foreach($locales as $locale){
  116.             $url $this->rewriteService->generateFromAtoBUrlWithoutDate($depStop$arrStopfalse$locale);
  117.             $alternates[] = [
  118.                 'hreflang' => $locale//transform to locale
  119.                 'href' => $url
  120.             ];
  121.         }
  122.         return $alternates;
  123.     }
  124.     /**
  125.      * @return Link[]
  126.      */
  127.     protected function getPopularRouteFrom(Stop $depStopstring $conveyancestring $_locale): array {
  128.         /** @var RouteSeo[] $routes */
  129.         $routes = [];
  130.         if ($_locale === 'fr' && $conveyance === Conveyance::BUS) { //FIXME use only full localell
  131.             $companyIds $this->clientRepo->findIdsByType($conveyance);
  132.             $routes $this->routeRepo->findTopFrenchByDepStopId($depStop->getId(), $companyIds);
  133.         } else {
  134.             $routes $this->seoService->getPopularRoutesFromCsv($depStop$conveyance$_locale);
  135.         }
  136.         return $this->rewriteService->createRoutesLink($routes);
  137.     }
  138. }