Skip to content

Commit

Permalink
Don't save RateQuery errors to the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pcatlin committed Sep 21, 2020
1 parent 32bf299 commit 999adfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ protected function init($fromCurrency, $toCurrency, $value, $ttl = 86400, $old =
if (!$cachedRateData) {
try {
$this->rateQuery = new RateQuery($fromCurrency, $toCurrency);
$this->cache->saveToCache($cacheFilename, $this->rateQuery->getData());
$cachedRateData = $this->rateQuery->getData();
$rateData = $this->rateQuery->getData();
if($rateData){
$this->cache->saveToCache($cacheFilename, $rateData);
}
$cachedRateData = $rateData;
} catch (\Exception $e) {
if ($old == false) {
return $this->init($fromCurrency, $toCurrency, $value, $ttl, true);
Expand Down
17 changes: 13 additions & 4 deletions src/RateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ public function __construct($fromCurrency, $toCurrency)
{

$this->query = new Query();
$this->data = $this->query->execute($fromCurrency, $toCurrency)->getData();
if (!$this->data) {
throw new \Exception('No Exchange Rate Data Received!');
}
$data = $this->query->execute($fromCurrency, $toCurrency)->getData();

if (!$data) {
$this->data = null;
throw new \Exception('No Exchange Rate Data Received!');
}

if( !isset($data->status) || $data->status != "ok"){
$this->data = null;
throw new \Exception('Error in received exchange rate data');
}

$this->data = $data;
}
}

0 comments on commit 999adfa

Please sign in to comment.