コード例 #1
0
ファイル: CurrencyHelper.php プロジェクト: vetoni/toko
 /**
  * @return Currency[]
  */
 public static function all()
 {
     if (!isset(static::$_currencies)) {
         /** @var Currency[] $list */
         static::$_currencies = Currency::find()->all();
     }
     return static::$_currencies;
 }
コード例 #2
0
 /**
  * Fetch the current exchange rates from the server
  * @param boolean Ignore the cache on this fetch, essentially force update from server
  * @return bool
  *
  * @todo Probably should try/catch this in case a network connection goes down or times out, then it can fail gracefully
  */
 public static function fetch($ignore_cache = false)
 {
     $latest_scrape = null;
     $currencies = null;
     if (!$ignore_cache) {
         $latest_scrape = \Cache::get('open_exchange_latest');
     }
     if (empty($latest_scrape)) {
         $latest_scrape = json_decode(file_get_contents('http://openexchangerates.org/latest.json'));
         // If this fails we might be timing out
         if (!$latest_scrape) {
             $difference = 61;
         } else {
             // Cache is set to expire on the next hourly update of openexchangerates.org
             $difference = intval(abs(($latest_scrape->timestamp - time()) / 60));
         }
         // If the openxchangerates site fails, fall back to github!
         if ($difference > 60) {
             $latest_scrape = json_decode(file_get_contents('https://raw.github.com/currencybot/open-exchange-rates/master/latest.json'));
             // If this fails then we're really in trouble, likely no internet connection
             if (!$latest_scrape) {
                 // Unsure what to do here...
             } else {
                 $difference = intval(abs(($latest_scrape->timestamp - time()) / 60));
             }
         }
         // If the expiry is negative, set the expiry to check in another 15 mins
         // If not, set it to 60 mins - the difference of now and the timestamp
         $expires = 60 - $difference > -1 ? 60 - $difference : 15;
         \Cache::put('open_exchange_latest', $latest_scrape, $expires);
     }
     // Fetch the available currencies, have them expire once every 3 hours
     $currencies = \Cache::get('open_exchange_currencies');
     if (empty($currencies)) {
         $currencies = json_decode(file_get_contents('http://openexchangerates.org/currencies.json'));
         \Cache::put('open_exchange_currencies', $currencies, 180);
     }
     static::$_disclaimer = $latest_scrape->disclaimer;
     static::$_license = $latest_scrape->license;
     static::$_timestamp = $latest_scrape->timestamp;
     static::$_base = $latest_scrape->base;
     static::$_rates = $latest_scrape->rates;
     static::$_currencies = $currencies;
     return true;
 }