Skip to content

Commit

Permalink
Refactoring last PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed May 2, 2024
1 parent 49be9e3 commit 9e825f9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 47 deletions.
6 changes: 6 additions & 0 deletions Api/IpToCountryRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public function getCountryCode($ip);
* @return mixed
*/
public function getVisitorCountryCode();

/**
* Retrieve current IP
* @return string
*/
public function getRemoteAddress();
}
15 changes: 13 additions & 2 deletions Api/IpToRegionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@
interface IpToRegionRepositoryInterface
{
/**
* @param string $ip
* @param $ip
* @return mixed
*/
public function getRegionCode($ip);

/**
* @return mixed
*/
public function getVisitorRegionCode();

/**
* Retrieve current IP
* @return string
*/
public function getRegionCode(string $ip = ''): string;
public function getRemoteAddress();
}
4 changes: 2 additions & 2 deletions Block/Adminhtml/System/Config/Form/IpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
if ($country == "ZZ") {
$country = 'Undefined';
}

$ip = $this->ipToCountryRepository->getRemoteAddress();
$regionId = $this->ipToRegionRepository->getRegionCode();
$regionId = $this->ipToRegionRepository->getVisitorRegionCode();

$html = '<div style="padding:10px;background-color:#f8f8f8;border:1px solid #ddd;margin-bottom:7px;">
Your IP Address: ' . $ip . '<br/>
Expand Down
15 changes: 6 additions & 9 deletions Model/IpToCountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/

namespace Magefan\GeoIp\Model;
declare(strict_types=1);

namespace Magefan\GeoIp\Model;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\DirectoryList;
Expand All @@ -15,10 +16,6 @@
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magefan\GeoIp\Api\IpToCountryRepositoryInterface;

/**
* Class IpToCountryRepository
* @package Magefan\GeoIp\Model
*/
class IpToCountryRepository implements IpToCountryRepositoryInterface
{
/**
Expand Down Expand Up @@ -67,7 +64,6 @@ class IpToCountryRepository implements IpToCountryRepositoryInterface
private $moduleDir;

/**
* IpToCountryRepository constructor.
* @param RemoteAddress $remoteAddress
* @param DirectoryList $directoryList
* @param ModuleDir $moduleDir
Expand All @@ -89,9 +85,9 @@ public function __construct(
}

/**
* [getCountryCode description]
* @param string $ip
* @return string | false
* Get Country Code by IP
* @param string $ip
* @return mixed
*/
public function getCountryCode($ip)
{
Expand Down Expand Up @@ -139,6 +135,7 @@ public function getCountryCode($ip)
$datFile = $filename;
} else {
$datFile = $this->moduleDir->getDir('Magefan_GeoIp') . '/data/GeoLite2-Country.mmdb';
//throw new \Exception('No .mmdb file');
}
$reader = new \GeoIp2\Database\Reader($datFile);
$record = $reader->country($ip);
Expand Down
66 changes: 34 additions & 32 deletions Model/IpToRegionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace Magefan\GeoIp\Model;

use Magefan\GeoIp\Api\IpToRegionRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Module\Dir as ModuleDir;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magefan\GeoIp\Api\IpToRegionRepositoryInterface;

class IpToRegionRepository implements IpToRegionRepositoryInterface
{
Expand Down Expand Up @@ -46,46 +46,42 @@ class IpToRegionRepository implements IpToRegionRepositoryInterface
/**
* @var DirectoryList
*/
protected $directoryList;
private $directoryList;

/**
* @var ModuleDir
*/
protected $moduleDir;
private $moduleDir;

/**
* @param ScopeConfigInterface $config
* @param RemoteAddress $remoteAddress
* @param RequestInterface $request
* @param DirectoryList $directoryList
* @param ModuleDir $moduleDir
* @param ScopeConfigInterface $config
* @param RequestInterface $httpRequest
*/
public function __construct(
ScopeConfigInterface $config,
RemoteAddress $remoteAddress,
RequestInterface $request,
DirectoryList $directoryList,
ModuleDir $moduleDir
)
{
$this->config = $config;
ModuleDir $moduleDir,
ScopeConfigInterface $config,
RequestInterface $httpRequest
) {
$this->remoteAddress = $remoteAddress;
$this->request = $request;
$this->directoryList = $directoryList;
$this->moduleDir = $moduleDir;
$this->config = $config;
$this->request = $httpRequest;
}

/**
* Get Region Code by IP
* @param string $ip
* @return string
* @return mixed
*/
public function getRegionCode(string $ip = ''): string
public function getRegionCode($ip)
{
if (!$ip) {
$ip = $this->getIp();
}

if (!isset($this->ipToRegion[$ip])) {
if (!isset($this->ipToRegion[$ip])) {
$this->ipToRegion[$ip] = '';

try {
Expand All @@ -94,11 +90,12 @@ public function getRegionCode(string $ip = ''): string
$datFile = $filename;
} else {
$datFile = $this->moduleDir->getDir('Magefan_GeoIp') . '/data/GeoLite2-City.mmdb';
//throw new \Exception('No .mmdb file');
}

$reader = new \GeoIp2\Database\Reader($datFile);
$record = $reader->city($ip);
if (isset($record->subdivisions[0])) {
if ($record && $record->subdivisions && isset($record->subdivisions[0])) {
$this->ipToRegion[$ip] = $record->subdivisions[0]->isoCode;
}
} catch (\Exception $e) {}
Expand All @@ -108,15 +105,20 @@ public function getRegionCode(string $ip = ''): string
}

/**
* Retrieve current visitor country code by IP
* @return string | false
*/
public function getVisitorRegionCode()
{
return $this->getRegionCode($this->getRemoteAddress());
}

/**
* Retrieve current IP
* @return string
*/
private function getIp(): string
public function getRemoteAddress()
{
$cloudflareEnable = $this->config->getValue(self::XML_PATH_CLOUDFLARE_ENABLED, ScopeInterface::SCOPE_STORE);
if ($cloudflareEnable) {
return (string)$this->request->getServer('HTTP_CF_CONNECTING_IP');
} else {
return (string)$this->remoteAddress->getRemoteAddress();
}
return $this->remoteAddress->getRemoteAddress();
}
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "magefan/module-geoip",
"description": "Allows to get country code by IP",
"require": {
"magefan/module-community" : ">=2.1.16",
"magefan/module-community" : ">=2.1.21",
"geoip2/geoip2": "^2.9.0"
},
"suggest": {
"magefan/module-auto-currency-switcher": "Install GeoIP Auto Currency Switcher Extension for Magento 2 (https://magefan.com/magento-2-currency-switcher-auto-currency-by-country). Use coupon code COMPOSER-FAN to get a 10% discount on magefan.com.",
"magefan/module-auto-language-switcher": "Install GeoIP Auto Language/Store Switcher Extension for Magento 2 (https://magefan.com/magento-2-auto-language-switcher). Use coupon code COMPOSER-FAN to get a 10% discount on magefan.com."
},
"type": "magento2-module",
"version": "2.2.3",
"version": "2.3.0",
"autoload": {
"psr-4": {
"Magefan\\GeoIp\\": ""
Expand Down
Binary file modified data/GeoLite2-Country.mmdb
Binary file not shown.

0 comments on commit 9e825f9

Please sign in to comment.