custom/plugins/AcrisShopSwitchCS/src/Components/GeoTargeting/Ip2CountryService.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Components\GeoTargeting;
  3. use Acris\ShopSwitch\AcrisShopSwitchCS as AcrisShopSwitch;
  4. use Acris\ShopSwitch\Components\Simulation\SimulationService;
  5. use GeoIp2\Database\Reader as Reader;
  6. //require_once '../../Resources/GeoIp2/vendor/autoload.php';
  7. class Ip2CountryService
  8. {
  9.     /**
  10.      * @var AcrisShopSwitch
  11.      */
  12.     private $acrisShopSwitchPlugin;
  13.     private SimulationService $simulationService;
  14.     public function __construct(AcrisShopSwitch $acrisShopSwitchPluginSimulationService $simulationService)
  15.     {
  16.         $this->acrisShopSwitchPlugin $acrisShopSwitchPlugin;
  17.         include_once($this->acrisShopSwitchPlugin->getPath() . '/Resources/GeoIp2/vendor/autoload.php');
  18.         $this->simulationService $simulationService;
  19.     }
  20.     /**
  21.      * @param string $ip ip v4 address
  22.      * @return string with country data
  23.      * @throws \MaxMind\Db\Reader\InvalidDatabaseException
  24.      */
  25.     public function ip2country($ipstring $salesChannelId): ?string
  26.     {
  27.         $simulatedIsoFromConfig $this->simulationService->getSimulatedCountryIsoFromConfig($ip$salesChannelId);
  28.         if(empty($simulatedIsoFromConfig) === false) {
  29.             return $simulatedIsoFromConfig;
  30.         }
  31.         $database $this->acrisShopSwitchPlugin->getPath() . '/Resources/GeoIp2/GeoLite2-Country.mmdb';
  32.         if(file_exists($database) === false) {
  33.             return null;
  34.         }
  35.         $reader = new Reader($database);
  36.         try {
  37.             $record $reader->country($ip);
  38.             $iso $record->country->isoCode;
  39.         } catch (\Exception $e) {
  40.             $iso "";
  41.         }
  42.         return $iso;
  43.     }
  44. }