src/Controller/Action/GeolocalisationControllerAction.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Action;
  4. use App\Service\GeoIPService;
  5. use Sylius\Component\Order\Context\CartContextInterface;
  6. use Sylius\Component\Resource\Repository\RepositoryInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Intl\Countries;
  11. use Twig\Environment;
  12. class GeolocalisationControllerAction extends AbstractController
  13. {
  14.     public function __construct(private CartContextInterface $cartContext, private Environment $twig, private GeoIPService $geoIPService, private RepositoryInterface $countryRepository)
  15.     {
  16.     }
  17.     public function __invoke(Request $request): Response
  18.     {
  19.         $cart $this->cartContext->getCart();
  20.         $countryCode null;
  21.         $countries $this->countryRepository->findBy(['enabled' => true]);
  22.         $countryName null;
  23.         if (null === $cart->getShippingAddress()) {
  24.             $ip $request->getClientIp();
  25.             $countryCode $this->geoIPService->getCountryCode($ip);
  26.             $countryName Countries::getName($countryCode'fr');
  27.         }
  28.         return new Response($this->twig->render("@SyliusShop/_geolocalisation.html.twig", [
  29.             'countryCode' => $countryCode,
  30.             'countries' => $countries,
  31.             'countryName' => $countryName
  32.         ]));
  33.     }
  34. }