-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into release/v1.0.1
- Loading branch information
Showing
6 changed files
with
142 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* @category Internship | ||
* @package Internship\BinancePay | ||
* @author Andrii Tomkiv <tomkivandrii18@gmail.com> | ||
* @copyright 2024 Tomkiv | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Internship\BinancePay\Model\Cache; | ||
|
||
class Currency | ||
{ | ||
private const CACHE_KEY = 'binance_supported_currencies'; | ||
private const CACHE_LIFETIME = 86400; | ||
|
||
/** | ||
* @param \Magento\Framework\App\CacheInterface $cache | ||
* @param \Internship\BinancePay\Service\BinancePay $binancePay | ||
* @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer | ||
*/ | ||
public function __construct( | ||
private readonly \Magento\Framework\App\CacheInterface $cache, | ||
private readonly \Internship\BinancePay\Service\BinancePay $binancePay, | ||
private readonly \Magento\Framework\Serialize\Serializer\Json $jsonSerializer, | ||
) { | ||
} | ||
|
||
/** | ||
* Fetches currencies from cache or updates the cache if empty or outdated. | ||
* | ||
* @return array | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function getCurrencies(): array | ||
{ | ||
try { | ||
$cachedData = $this->cache->load(self::CACHE_KEY); | ||
|
||
if ($cachedData) { | ||
return $this->jsonSerializer->unserialize($cachedData); | ||
} | ||
|
||
return $this->fetchAndCacheCurrencies(); | ||
} catch (\Exception $exception) { | ||
throw new \Magento\Framework\Exception\LocalizedException(__()); | ||
} | ||
} | ||
|
||
/** | ||
* Fetches currencies from Binance API. | ||
* | ||
* @return array | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
private function fetchAndCacheCurrencies(): array | ||
{ | ||
try { | ||
$currencies = $this->binancePay->getSupportedCurrencies(); | ||
|
||
$this->cache->save( | ||
$this->jsonSerializer->serialize($currencies), | ||
self::CACHE_KEY, | ||
[], | ||
self::CACHE_LIFETIME | ||
); | ||
|
||
return $currencies; | ||
} catch (\Exception $exception) { | ||
throw new \Magento\Framework\Exception\LocalizedException(__($exception->getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.