source: trunk/lib/Currency.inc.php @ 328

Last change on this file since 328 was 328, checked in by quinn, 16 years ago

Created v1.0 of Currency class, using xurrency.com.

File size: 5.2 KB
Line 
1<?php
2/**
3 * Currency.inc.php
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
5 *
6 * Class to convert currency values.
7 *
8 * @author  Quinn Comendant <quinn@strangecode.com>
9 * @version 1.0
10 *
11 * Example of use:
12---------------------------------------------------------------------
13$currency = new Currency();
14echo $currency->getValue(1, 'eur', 'usd');
15---------------------------------------------------------------------
16 */
17
18require_once 'SOAP/Client.php';
19 
20class Currency {
21
22    // Configuration parameters for this object.
23    var $_params = array(
24        'cache_result' => false,
25        'cache_dir' => '',
26        'cache_age' => 28800, // 8 hours.
27        'soap_api_url' => 'http://xurrency.com/servidor_soap.php',
28    );
29   
30    // Object to hold SOAP_Client instance.
31    var $_soap_client;
32
33    /**
34     * Cart constructor.
35     */
36    function Currency($params=array())
37    {
38        $app =& App::getInstance();
39
40        // Initialize.
41        if (!isset($_SESSION['_currency'])) {
42            $_SESSION['_currency'] = array();
43        }
44
45        // Set custom parameters.
46        $this->setParam($params);
47       
48        // 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__);               
57            }
58        }
59       
60        // Setup SOAP object.
61        $this->_soap_client = new SOAP_Client($this->getParam('soap_api_url'));
62    }
63
64    /**
65     * Set the params of this object.
66     *
67     * @param  array $params   Array of param keys and values to set.
68     */
69    function setParam($params=null)
70    {
71        if (isset($params) && is_array($params)) {
72            // Merge new parameters with old overriding only those passed.
73            $this->_params = array_merge($this->_params, $params);
74        }
75    }
76
77    /**
78     * Return the value of a parameter, if it exists.
79     *
80     * @access public
81     * @param string $param        Which parameter to return.
82     * @return mixed               Configured parameter value.
83     */
84    function getParam($param)
85    {
86        $app =& App::getInstance();
87   
88        if (isset($this->_params[$param])) {
89            return $this->_params[$param];
90        } else {
91            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
92            return null;
93        }
94    }
95   
96    /*
97    * Return the exchange value between the two given currencies.
98    *
99    * @access   public
100    * @param   
101    * @return   
102    * @author   Quinn Comendant <quinn@strangecode.com>
103    * @version  1.0
104    * @since    05 May 2008 23:50:59
105    */
106    function getValue($amount, $base, $target)
107    {
108        $app =& App::getInstance();
109       
110        $cache_file_path = sprintf('%s/%s-to-%s', $this->getParam('cache_dir'), $base, $target);
111        $cache_file_mtime = @filemtime($cache_file_path);
112        if (!$this->getParam('cache_result') || !$cache_file_mtime || $cache_file_mtime < time() - $this->getParam('cache_age')) {
113            // Get fresh data and create cached file if missing or expired.
114            $value = $this->_performAPICall('getValue', array(
115                'amount' => $amount,
116                'base' => $base,
117                'target' => $target
118            ));
119            if (false !== $value) {
120                // 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__);
122                if (!$value = file_get_contents($cache_file_path)) {
123                    $app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
124                }
125            } else if ($this->getParam('cache_result') && !file_put_contents($cache_file_path, $value, LOCK_EX)) {
126                $app->logMsg(sprintf('Failed writing to target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
127            }
128        } else {
129            if (!$value = file_get_contents($cache_file_path)) {
130                $app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
131            }
132        }
133        return trim($value);
134    }
135   
136    /**
137     * @param  string
138     * @param  array
139     * @return mixed
140     * @access private
141     */
142    function _performAPICall($api_call, $parameters=null)
143    {
144        $app =& App::getInstance();
145       
146        $result = $this->_soap_client->call($api_call, $parameters);
147       
148        if (PEAR::isError($result)) {
149            $app->logMsg(sprintf('SOAP Pear error: %s', $result->getMessage()), LOG_WARNING, __FILE__, __LINE__);
150            return false;
151        }
152
153        return $result;
154    }
155}
156
157
158?>
Note: See TracBrowser for help on using the repository browser.