149 lines
4.0 KiB
PHP
149 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_GeoIPLookup
|
|
* @copyright Copyright (c) 2024 Plumrocket Inc. (https://plumrocket.com)
|
|
* @license https://plumrocket.com/license/ End-user License Agreement
|
|
*/
|
|
|
|
namespace Plumrocket\GeoIPLookup\Model\Services;
|
|
|
|
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
|
|
use Plumrocket\GeoIPLookup\Api\GeoIpServiceInterface;
|
|
use Plumrocket\GeoIPLookup\Library\Mmdb\Reader\InvalidDatabaseException;
|
|
use Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface;
|
|
use Plumrocket\GeoIPLookup\Model\MmdbReader;
|
|
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
class AbstractMmdb implements GeoIpServiceInterface
|
|
{
|
|
/**
|
|
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
|
|
*/
|
|
private $remoteAddress;
|
|
|
|
/**
|
|
* @var \Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface
|
|
*/
|
|
private $geoIpCache;
|
|
|
|
/**
|
|
* @var \Plumrocket\GeoIPLookup\Model\MmdbReader
|
|
*/
|
|
private $mmdbReader;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $cacheKeyPostfixes;
|
|
|
|
/**
|
|
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
|
|
* @param \Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface $geoIpCache
|
|
* @param \Plumrocket\GeoIPLookup\Model\MmdbReader $mmdbReader
|
|
* @param array $cacheKeyPostfixes
|
|
*/
|
|
public function __construct(
|
|
RemoteAddress $remoteAddress,
|
|
GeoIpInterface $geoIpCache,
|
|
MmdbReader $mmdbReader,
|
|
array $cacheKeyPostfixes
|
|
) {
|
|
$this->remoteAddress = $remoteAddress;
|
|
$this->geoIpCache = $geoIpCache;
|
|
$this->mmdbReader = $mmdbReader;
|
|
$this->cacheKeyPostfixes = $cacheKeyPostfixes;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getGeoLocation(?string $ip): ?array
|
|
{
|
|
if (! $ip) {
|
|
$ip = $this->remoteAddress->getRemoteAddress();
|
|
}
|
|
|
|
if ($ip) {
|
|
$cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)];
|
|
$geoIpCache = $this->geoIpCache->get();
|
|
if (isset($geoIpCache[$cacheKey])) {
|
|
return $geoIpCache[$cacheKey];
|
|
}
|
|
|
|
try {
|
|
$result = $this->mmdbReader->get($ip);
|
|
} catch (InvalidDatabaseException $e) {
|
|
$result = null;
|
|
}
|
|
|
|
$geoIpCache[$cacheKey] = $result;
|
|
$this->geoIpCache->set($geoIpCache);
|
|
}
|
|
|
|
return $result ?? null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getCountryCode(?string $ip): ?string
|
|
{
|
|
if (! $ip) {
|
|
$ip = $this->remoteAddress->getRemoteAddress();
|
|
}
|
|
|
|
if ($ip) {
|
|
$cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)];
|
|
$geoIpCache = $this->geoIpCache->get();
|
|
if (isset($geoIpCache[$cacheKey])) {
|
|
return $geoIpCache[$cacheKey];
|
|
}
|
|
|
|
$result = $this->getGeoLocation($ip);
|
|
$result = $result['country_code'] ?? null;
|
|
|
|
$geoIpCache[$cacheKey] = $result;
|
|
$this->geoIpCache->set($geoIpCache);
|
|
}
|
|
|
|
return $result ?? null;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getCurrentCountryState(?string $ip): ?string
|
|
{
|
|
if (! $ip) {
|
|
$ip = $this->remoteAddress->getRemoteAddress();
|
|
}
|
|
|
|
if ($ip) {
|
|
$cacheKey = $ip . $this->cacheKeyPostfixes[$this->getShortMethodName(__METHOD__)];
|
|
$geoIpCache = $this->geoIpCache->get();
|
|
if (isset($geoIpCache[$cacheKey])) {
|
|
return $geoIpCache[$cacheKey];
|
|
}
|
|
|
|
$result = $this->getGeoLocation($ip);
|
|
$result = $result['subdivision_1_name'] ?? null;
|
|
|
|
$geoIpCache[$cacheKey] = $result;
|
|
$this->geoIpCache->set($geoIpCache);
|
|
}
|
|
|
|
return $result ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param string $method
|
|
* @return string
|
|
*/
|
|
private function getShortMethodName(string $method): string
|
|
{
|
|
return substr($method, strpos($method, '::') + 2);
|
|
}
|
|
}
|