vendor/rawafed/commons-bundle/Service/LocationManager.php line 80

Open in your IDE?
  1. <?php
  2. namespace Rawafed\CommonsBundle\Service;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  8. class LocationManager
  9. {
  10.     private $requestStack;
  11.     private $tokenStorage;
  12.     private $authorizationChecker;
  13.     private $countryCode null;
  14.     public function __construct(RequestStack $requestStackTokenStorageInterface $tokenStorageAuthorizationChecker $authorizationChecker)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->tokenStorage $tokenStorage;
  18.         $this->authorizationChecker $authorizationChecker;
  19.     }
  20.     public function getCountryCode()
  21.     {
  22.         $request $this->requestStack->getMasterRequest();
  23.         if ($request && $request->query->has('__country')) {
  24.             $this->countryCode $request->query->get('__country');
  25.         } elseif ($request && $request->cookies->get('__country')) {
  26.             $this->countryCode $request->cookies->get('__country');
  27.         } elseif (!$this->countryCode) {
  28.             $token $this->tokenStorage->getToken();
  29.             if ($token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  30.                 $user $token->getUser();
  31.                 if (method_exists($user->getUserProfile(), 'getCountry')) {
  32.                     $userCountry $user->getUserProfile()->getCountry();
  33.                     if ($userCountry) {
  34.                         $this->countryCode $userCountry->getId();
  35.                     }
  36.                 }
  37.             }
  38.             if (!$this->countryCode) {
  39.                 $ip $request->getClientIp();
  40.                 if (class_exists('\GeoIp2\Database\Reader')) {
  41.                     $reader = new \GeoIp2\Database\Reader(__DIR__ '/../../../../src/Resources/geodb/GeoLite2-Country.mmdb');
  42.                     try {
  43.                         $record $reader->country($ip);
  44.                         $this->countryCode $record->country->isoCode;
  45.                     } catch (\GeoIp2\Exception\AddressNotFoundException $ex) {
  46.                     }
  47.                 } else {
  48.                     throw new \Exception('GeoIp2 reader class is not found. Add "geoip2/geoip2" to composer.json');
  49.                 }
  50.                 // During development:
  51.                 if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
  52.                     $this->countryCode 'SA';
  53.                 }
  54.             }
  55.         }
  56.         if (!$this->countryCode) {
  57.             $this->countryCode 'WW';
  58.         } elseif ($this->countryCode == 'IL') {
  59.             $this->countryCode 'PS';
  60.         } elseif ($this->countryCode == 'EH') {
  61.             $this->countryCode 'MA';
  62.         } elseif ($this->countryCode == 'ZR') {
  63.             $this->countryCode 'CD';
  64.         }
  65.         return $this->countryCode;
  66.     }
  67.     public function onKernelResponse(ResponseEvent $event)
  68.     {
  69.         if ($this->countryCode) {
  70.             $cookie = new Cookie('__country'$this->countryCodetime() + 60 60 24 365);
  71.             $response $event->getResponse();
  72.             $response->headers->setCookie($cookie);
  73.         }
  74.     }
  75. }