|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / app / code / Swissup / Geoip / Model / Provider / |
Filename | /home/dev2.destoffenstraat.com/app/code/Swissup/Geoip/Model/Provider/AbstractProvider.php |
Size | 3.7 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 15-Apr-2022 17:45 |
Last accessed | 23-Aug-2025 00:44 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace Swissup\Geoip\Model\Provider;
use Swissup\Geoip\Api\Data\ProviderInterface;
use Swissup\Geoip\Model\Record;
use Swissup\Geoip\Model\RecordFactory;
use Swissup\Geoip\Model\ProviderContext;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
abstract class AbstractProvider implements ProviderInterface
{
/**
* @var RecordFactory
*/
protected $recordFactory;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var CacheInterface
*/
protected $cache;
/**
* @var mixed
*/
protected $rawRecord;
/**
* @param ProviderContext $context
*/
public function __construct(ProviderContext $context)
{
$this->recordFactory = $context->getRecordFactory();
$this->scopeConfig = $context->getScopeConfig();
$this->logger = $context->getLogger();
$this->cache = $context->getCache();
}
/**
* Get specific config value
*
* @param string $path
* @param string $scope
* @return string
*/
protected function getConfigValue($path, $scope = ScopeInterface::SCOPE_STORE)
{
return $this->scopeConfig->getValue($path, $scope);
}
/**
* @param string $ip
* @return Record
*/
public function resolve($ip)
{
// $ip = '128.101.101.101'; // US
// $ip = '54.195.241.132'; // IE
if (!$this->rawRecord = $this->loadCache($ip)) {
try {
$this->rawRecord = $this->detect($ip);
$this->saveCache($this->rawRecord, $ip);
} catch (\Throwable $e) {
$this->logException($e);
}
}
$record = $this->recordFactory->create();
if ($this->rawRecord) {
$record->update([
'rawRecord' => $this->rawRecord,
'cityName' => $this->getCityName(),
'regionCode' => $this->getRegionCode(),
'countryCode' => $this->getCountryCode(),
'postalCode' => $this->getPostalCode(),
]);
}
return $record;
}
/**
* @param \Throwable $e
* @return void
*/
protected function logException(\Throwable $e)
{
$this->logger->error('Swissup_Geoip: ' . $e->getMessage());
}
/**
* @param string $ip
* @return mixed
*/
protected function loadCache($ip)
{
if (!$this->isCacheable()) {
return false;
}
if (!$cached = $this->cache->load($this->getCacheKey($ip))) {
return false;
}
return $this->unserialize($cached);
}
/**
* @param mixed $data
* @param string $ip
* @return void
*/
protected function saveCache($data, $ip)
{
if (!$this->isCacheable() || !$data) {
return false;
}
if ($cached = $this->serialize($data)) {
$this->cache->save(
$cached,
$this->getCacheKey($ip),
[],
60 * 10
);
}
}
/**
* @param mixed $data
* @return string
*/
protected function serialize($data)
{
return json_encode($data);
}
/**
* @param string $data
* @return array
*/
protected function unserialize($data)
{
return json_decode($data, true);
}
/**
* @param string $ip
* @return string
*/
private function getCacheKey($ip)
{
return 'swissup_geoip_' . $ip;
}
}
namespace Swissup\Geoip\Model\Provider;
use Swissup\Geoip\Api\Data\ProviderInterface;
use Swissup\Geoip\Model\Record;
use Swissup\Geoip\Model\RecordFactory;
use Swissup\Geoip\Model\ProviderContext;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface;
abstract class AbstractProvider implements ProviderInterface
{
/**
* @var RecordFactory
*/
protected $recordFactory;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var CacheInterface
*/
protected $cache;
/**
* @var mixed
*/
protected $rawRecord;
/**
* @param ProviderContext $context
*/
public function __construct(ProviderContext $context)
{
$this->recordFactory = $context->getRecordFactory();
$this->scopeConfig = $context->getScopeConfig();
$this->logger = $context->getLogger();
$this->cache = $context->getCache();
}
/**
* Get specific config value
*
* @param string $path
* @param string $scope
* @return string
*/
protected function getConfigValue($path, $scope = ScopeInterface::SCOPE_STORE)
{
return $this->scopeConfig->getValue($path, $scope);
}
/**
* @param string $ip
* @return Record
*/
public function resolve($ip)
{
// $ip = '128.101.101.101'; // US
// $ip = '54.195.241.132'; // IE
if (!$this->rawRecord = $this->loadCache($ip)) {
try {
$this->rawRecord = $this->detect($ip);
$this->saveCache($this->rawRecord, $ip);
} catch (\Throwable $e) {
$this->logException($e);
}
}
$record = $this->recordFactory->create();
if ($this->rawRecord) {
$record->update([
'rawRecord' => $this->rawRecord,
'cityName' => $this->getCityName(),
'regionCode' => $this->getRegionCode(),
'countryCode' => $this->getCountryCode(),
'postalCode' => $this->getPostalCode(),
]);
}
return $record;
}
/**
* @param \Throwable $e
* @return void
*/
protected function logException(\Throwable $e)
{
$this->logger->error('Swissup_Geoip: ' . $e->getMessage());
}
/**
* @param string $ip
* @return mixed
*/
protected function loadCache($ip)
{
if (!$this->isCacheable()) {
return false;
}
if (!$cached = $this->cache->load($this->getCacheKey($ip))) {
return false;
}
return $this->unserialize($cached);
}
/**
* @param mixed $data
* @param string $ip
* @return void
*/
protected function saveCache($data, $ip)
{
if (!$this->isCacheable() || !$data) {
return false;
}
if ($cached = $this->serialize($data)) {
$this->cache->save(
$cached,
$this->getCacheKey($ip),
[],
60 * 10
);
}
}
/**
* @param mixed $data
* @return string
*/
protected function serialize($data)
{
return json_encode($data);
}
/**
* @param string $data
* @return array
*/
protected function unserialize($data)
{
return json_decode($data, true);
}
/**
* @param string $ip
* @return string
*/
private function getCacheKey($ip)
{
return 'swissup_geoip_' . $ip;
}
}