Skip to content

Commit

Permalink
Merge pull request #1 from dmitrykazak/feature/apikey
Browse files Browse the repository at this point in the history
Added API KEY and a new Exception
  • Loading branch information
dmitrykazak authored Dec 16, 2018
2 parents c02e08f + 8de411c commit adaee7c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ public function getAjaxUrl()
*/
protected function _toHtml()
{
if (!$this->helper('dk_customerip')->isCustomerIpEnabled()) {
return '';
}

if (!$this->getCustomer() || !$this->getCustomer()->getId()) {
return '';
}

return parent::_toHtml();
}
}
8 changes: 8 additions & 0 deletions app/code/community/DK/CustomerIP/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* Class DK_CustomerIP_Exception
*/
class DK_CustomerIP_Exception extends Mage_Core_Exception
{
}
41 changes: 35 additions & 6 deletions app/code/community/DK/CustomerIP/Model/Ip/Service/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,54 @@

abstract class DK_CustomerIP_Model_Ip_Service_Abstract extends Varien_Object
{
const XML_API_KEY_PATH = '';

/**
* @var DK_CustomerIP_Helper_Data $helper
*/
private $helper;

/**
* @var Varien_Http_Client $httpClient
*/
protected $httpClient;
private $httpClient;

/**
* @var array $error
*/
protected $error = [];

public function _construct()
{
$this->httpClient = new Varien_Http_Client();
}

/**
* @var DK_CustomerIP_Helper_Data $helper
* @return DK_CustomerIP_Helper_Data
*/
protected $helper;
public function getHelper()
{
if (!$this->helper) {
$this->helper = Mage::helper('dk_customerip');
}

public function _construct()
return $this->helper;
}

/**
* @return Varien_Http_Client
*/
public function getHttpClient()
{
$this->httpClient = new Varien_Http_Client();
$this->helper = Mage::helper('dk_customerip');
return $this->httpClient;
}

/**
* @return mixed
*/
public function getApiKey()
{
return Mage::getStoreConfig(static::XML_API_KEY_PATH);
}

abstract public function call();
Expand Down
18 changes: 12 additions & 6 deletions app/code/community/DK/CustomerIP/Model/Ip/Service/IpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
class DK_CustomerIP_Model_Ip_Service_IpApi
extends DK_CustomerIP_Model_Ip_Service_Abstract
{
const XML_API_KEY_PATH = 'dk_customerip/ipapi/apikey';

/**
* @var string $url
*/
protected $url = 'https://ipapi.co/{{IP_ADDRESS}}/json/';
private $url = 'https://ipapi.co/{{IP_ADDRESS}}/json/';

/**
* @return array|mixed
Expand All @@ -19,19 +21,23 @@ public function call()
$url = str_replace('{{IP_ADDRESS}}', $this->getIp(), $this->url);

try {
$response = $this->httpClient
$client = $this->getHttpClient()
->setUri($url)
->setConfig(['timeout' => Mage::getStoreConfig('dk_customerip/ipapi/timeout')])
->request('GET')
->getBody();
->setConfig(['timeout' => Mage::getStoreConfig('dk_customerip/ipapi/timeout')]);

if ($apiKey = $this->getApiKey()) {
$client->setParameterGet('key', $apiKey);
}

$response = $client->request('GET')->getBody();

if ($response) {
return $response;
}

return [];
} catch (Exception $e) {
array_push($this->error, $this->helper->__('Cannot get IP address %s.', $url));
array_push($this->error, $this->getHelper()->__('Cannot get IP address %s.', $url));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

class DK_CustomerIP_Model_System_Config_Source_Backend_Cron extends Mage_Core_Model_Config_Data
/**
* Class DK_CustomerIP_Model_System_Config_Source_Backend_Cron
*/
class DK_CustomerIP_Model_System_Config_Source_Backend_Cron
extends Mage_Core_Model_Config_Data
{
const CRON_PATH = 'crontab/jobs/ip_customer_update/schedule/cron_expr';

Expand All @@ -16,8 +20,11 @@ protected function _afterSave()
->setPath(self::CRON_PATH)
->save();

} catch (Exception $e) {
throw new Exception(Mage::helper('cron')->__('Unable to save the cron expression.'));
} catch (Mage_Core_Exception $e) {
throw Mage::exception(
'DK_CustomerIP',
Mage::helper('cron')->__('Unable to save the cron expression.')
);
}
}
}
8 changes: 8 additions & 0 deletions app/code/community/DK/CustomerIP/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</timeout>
<apikey translate="label">
<label>Api Key</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</apikey>
</fields>
</ipapi>
<import translate="label">
Expand Down

0 comments on commit adaee7c

Please sign in to comment.