Commit 6143c8b9 authored by bussnet's avatar bussnet
Browse files

Merge pull request #1 from Petah/patch-1

Added some validation and sanity checks to CurrencyLookupRubyMoney
parents dac8c3e4 9a3150e9
......@@ -29,15 +29,31 @@ class CurrencyLookupRubyMoney implements CurrencyLookup {
function __construct($file=null, $cache_dir='/tmp/') {
$file = $file ? : self::CURRENCY_FILE;
// read from cache
if (file_exists($cache_dir.'/'.basename($file)))
$file = $cache_dir . '/' . basename($file);
// try and read from cache
$cache_file = $cache_dir . '/' . basename($file);
if (file_exists($cache_file)) {
$content = file_get_contents($cache_file);
if ($content) {
$this->currencies = json_decode($content, true);
if ($this->currencies) {
return;
}
}
}
$this->currencies = json_decode(file_get_contents($file), true);
// try and read from server
$content = file_get_contents($file);
if (!$content) {
throw new \Exception('Could not read currencies file from cache or server.');
}
$this->currencies = json_decode($content, true);
if (!$this->currencies) {
throw new \Exception('Could decode currencies file from server.');
}
// write to cache
if ($cache_dir && file_exists($cache_dir)) {
file_put_contents($cache_dir . '/' . basename($file), file_get_contents($file));
if ($cache_dir && is_dir($cache_dir)) {
file_put_contents($cache_file, $content);
}
}
......@@ -52,4 +68,4 @@ class CurrencyLookupRubyMoney implements CurrencyLookup {
throw new UnknownCurrencyException(sprintf('currency with iso-code %s not found', $iso_code));
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment