Changeset 329


Ignore:
Timestamp:
May 6, 2008 9:11:59 AM (16 years ago)
Author:
quinn
Message:

Slight mods to Currency class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Currency.inc.php

    r328 r329  
    2222    // Configuration parameters for this object.
    2323    var $_params = array(
    24         'cache_result' => false,
     24        'cache_result' => true,
    2525        'cache_dir' => '',
    2626        'cache_age' => 28800, // 8 hours.
     
    4747       
    4848        // Setup cache directory.
    49         if ('' == $this->getParam('cache_dir')) {
    50             // Use a sane default cache directory.
    51             $this->setParam(array('cache_dir' => '/tmp/' . md5(COMMON_BASE)));
    52         }
    53         if (!is_dir($this->getParam('cache_dir'))) {
    54             $app->logMsg(sprintf('Creating cache_dir: %s', $this->getParam('cache_dir')), LOG_INFO, __FILE__, __LINE__);               
    55             if (!mkdir($this->getParam('cache_dir'))) {
    56                 $app->logMsg(sprintf('Could not create cache_dir: %s', $this->getParam('cache_dir')), LOG_WARNING, __FILE__, __LINE__);               
     49        if ($this->getParam('cache_result')) {
     50            if ('' == $this->getParam('cache_dir')) {
     51                // Use a sane default cache directory.
     52                $this->setParam(array('cache_dir' => '/tmp/xcache_' . md5(COMMON_BASE)));
     53            }
     54            if (!is_dir($this->getParam('cache_dir'))) {
     55                $app->logMsg(sprintf('Creating cache_dir: %s', $this->getParam('cache_dir')), LOG_INFO, __FILE__, __LINE__);               
     56                if (!mkdir($this->getParam('cache_dir'))) {
     57                    $app->logMsg(sprintf('Could not create cache_dir: %s', $this->getParam('cache_dir')), LOG_WARNING, __FILE__, __LINE__);               
     58                }
    5759            }
    5860        }
     
    98100    *
    99101    * @access   public
    100     * @param   
    101     * @return   
     102    * @param    float   Base amount to convert from.
     103    * @param    string  3-letter currency code to convert from.
     104    * @param    string  3-letter currency code to convert to.
     105    * @return   mixed   Float exchange rate value, or false on error.
    102106    * @author   Quinn Comendant <quinn@strangecode.com>
    103107    * @version  1.0
     
    112116        if (!$this->getParam('cache_result') || !$cache_file_mtime || $cache_file_mtime < time() - $this->getParam('cache_age')) {
    113117            // Get fresh data and create cached file if missing or expired.
     118            $app->logMsg(sprintf('Getting fresh currency exchange rates: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);
    114119            $value = $this->_performAPICall('getValue', array(
    115120                'amount' => $amount,
     
    117122                'target' => $target
    118123            ));
    119             if (false !== $value) {
     124            if (false === $value || !is_numeric($value)) {
    120125                // Failed retreiving SOAP value. Use cached copy for now.
    121                 $app->logMsg(sprintf('Failed getting SOAP currency value: %s-to-%s', $base, $target), LOG_NOTICE, __FILE__, __LINE__);
     126                $app->logMsg(sprintf('Failed getting SOAP currency exchange rates: %s-to-%s, using cached copy', $base, $target), LOG_NOTICE, __FILE__, __LINE__);
    122127                if (!$value = file_get_contents($cache_file_path)) {
    123128                    $app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
     129                    return false;
    124130                }
    125131            } else if ($this->getParam('cache_result') && !file_put_contents($cache_file_path, $value, LOCK_EX)) {
    126132                $app->logMsg(sprintf('Failed writing to target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
     133                return false;
    127134            }
    128135        } else {
     136            $app->logMsg(sprintf('Getting cached currency exchange rates: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);
    129137            if (!$value = file_get_contents($cache_file_path)) {
    130138                $app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
     139                return false;
    131140            }
    132141        }
Note: See TracChangeset for help on using the changeset viewer.