79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Authors:
|
|
* krybbio <https://magento.stackexchange.com/users/15051/krybbio>
|
|
* Alex Gusev <alex@flancer64.com>
|
|
* Since: 2018
|
|
*/
|
|
|
|
namespace Oreolek\MagentoLym\Model\Currency\Import;
|
|
|
|
|
|
class Fcc
|
|
extends \Magento\Directory\Model\Currency\Import\AbstractImport
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
const CURRENCY_CONVERTER_URL = 'https://coindata.lympo.io/etherspy/public/api/fetch/currency/market_data?currency=lympo&ticker_symbol=lym';
|
|
|
|
/** @var \Oreolek\MagentoLym\Helper\Config */
|
|
private $hlpCfg;
|
|
/** @var \Magento\Framework\HTTP\ZendClient */
|
|
private $httpClient;
|
|
/** @var \Magento\Framework\Json\Helper\Data */
|
|
private $jsonHelper;
|
|
/** @var \Psr\Log\LoggerInterface */
|
|
private $logger;
|
|
|
|
public function __construct(
|
|
\Psr\Log\LoggerInterface $logger,
|
|
\Magento\Framework\HTTP\ZendClient $httpClient,
|
|
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
|
|
\Magento\Framework\Json\Helper\Data $jsonHelper,
|
|
\Oreolek\MagentoLym\Helper\Config $hlpCfg
|
|
) {
|
|
parent::__construct($currencyFactory);
|
|
$this->logger = $logger;
|
|
$this->httpClient = $httpClient;
|
|
$this->jsonHelper = $jsonHelper;
|
|
$this->hlpCfg = $hlpCfg;
|
|
}
|
|
|
|
/**
|
|
* @param string $currencyFrom
|
|
* @param string $currencyTo
|
|
* @param int $retry
|
|
* @return float|null
|
|
*/
|
|
protected function _convert($currencyFrom, $currencyTo, $retry = 0)
|
|
{
|
|
$result = null;
|
|
$url = self::CURRENCY_CONVERTER_URL;
|
|
$this->logger->info("Currency rates request: $url");
|
|
$delay = $this->hlpCfg->getDelay();
|
|
$timeout = $this->hlpCfg->getTimeout();
|
|
try {
|
|
sleep($delay);
|
|
$this->httpClient->setUri($url);
|
|
$this->httpClient->setConfig(['timeout' => $timeout,]);
|
|
$response = $this->httpClient->request('GET');
|
|
$body = $response->getBody();
|
|
$data = $this->jsonHelper->jsonDecode($body);
|
|
$results = $data['results']['currency_market_data'];
|
|
if (!isset($results)) {
|
|
$this->_messages[] = __('We can\'t retrieve a rate from %1.', $url);
|
|
} else {
|
|
$result = (float)$results[$currencyFrom]['current_price'];
|
|
}
|
|
} catch (\Exception $e) {
|
|
if ($retry == 0) {
|
|
$this->_convert($currencyFrom, $currencyTo, 1);
|
|
} else {
|
|
$this->_messages[] = __('We can\'t retrieve a rate from %1.', $url);
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
}
|