src/Controller/PostCMPController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class PostCMPController extends AbstractController
  10. {
  11.     #[Route('/post-cmp'name'app_post_cmp'methods: ['GET'])]
  12.     public function index(Request $request): RedirectResponse Response
  13.     {
  14.         // Check if the user has accepted the consent cookie
  15.         $consentCookie $request->cookies->get('consent');
  16.         if ($consentCookie === 'yes') {
  17.             // Check if the First-ID cookie exists
  18.             $firstIdCookie $request->cookies->get('firstid');
  19.             if (!$firstIdCookie) {
  20.                 // Redirect to the First-ID gate
  21.                 $redirectHostUrlEncode urlencode($request->getSchemeAndHttpHost());
  22.                 $redirectPath '/handle-first-id-redirect';
  23.                 $redirectUriUrlEncode urlencode($redirectHostUrlEncode $redirectPath);
  24.                 return new RedirectResponse(
  25.                     "https://gate.first-id.fr?redirectHost=$redirectHostUrlEncode&redirectUri=$redirectUriUrlEncode",
  26.                     Response::HTTP_FOUND
  27.                 );
  28.             }
  29.         }
  30.         // Render your base template
  31.         return $this->render('post-cmp/post-cmp.html.twig');
  32.     }
  33.     /**
  34.      * @Route("/handle-first-id-redirect", name="app_handle_first_id_redirect")
  35.      */
  36.     #[Route('/post-cmp/handle-first-id-redirect'name'app_post_cmp_handle_first_id_redirect'methods: ['GET'])]
  37.     public function handleFirstIdRedirect(Request $request): Response
  38.     {
  39.         // Check if the "firstId" query parameter is present in the URL
  40.         if ($request->query->has('firstId') && $request->query->has('redirectUri')) {
  41.             $firstId $request->query->get('firstId');
  42.             $redirectUri $request->query->get('redirectUri');
  43.             // Create the First-ID cookie
  44.             $response $this->redirect(urldecode($redirectUri));
  45.             $response->headers->setCookie(
  46.                 new Cookie(
  47.                     'firstid',
  48.                     $firstId,
  49.                     time() + (60 60 24 180), // 180 days
  50.                 )
  51.             );
  52.             // Remove the "firstId" and "redirectUri" query parameters from the URL
  53.             $params $request->query->all();
  54.             unset($params['firstId']);
  55.             unset($params['redirectUri']);
  56.             $finalUrl urldecode($redirectUri);
  57.             if (!empty($params)) {
  58.                 $finalUrl .= '?' http_build_query($params);
  59.             }
  60.             $response->setTargetUrl($finalUrl);
  61.             return $response;
  62.         }
  63.         // If the "firstId" query parameter is not present, redirect to the homepage
  64.         return $this->render('base.html.twig');
  65.     }
  66. }