| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @category    Brownie/ExchangeRate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author      Brownie <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license     https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Brownie\ExchangeRate\Source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Brownie\ExchangeRate\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Brownie\ExchangeRate\SourceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Brownie\HttpClient\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Brownie\ExchangeRate\Model\ExchangeRate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Brownie\HttpClient\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Brownie\HttpClient\Exception\ValidateException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Brownie\ExchangeRate\Exception\InvalidExchangeRateException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class BankOfCanada extends Service implements SourceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Base currency. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $baseCode = 'CAD'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * The endpoint access to the list of exchange rate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $endpoint = 'https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/xml?recent=1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Returns the current exchange rate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return ExchangeRate[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws ClientException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws ValidateException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws InvalidExchangeRateException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 3 |  |     public function getExchangeRates() | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $response = $this | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 3 |  |             ->getHttpClient() | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 3 |  |             ->request( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 $this | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 3 |  |                     ->getHttpClient() | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 3 |  |                     ->createRequest() | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 3 |  |                     ->setMethod(Request::HTTP_METHOD_GET) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 3 |  |                     ->setUrl($this->endpoint) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 3 |  |         if ((200 != $response->getHttpCode()) || (empty($response->getBody()))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |             throw new InvalidExchangeRateException('Invalid response from currency exchange server.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |         libxml_use_internal_errors(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 2 |  |         $xml = simplexml_load_string($response->getBody()); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 2 |  |         if (false === $xml) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |             throw new InvalidExchangeRateException('Error parsing the response from the currency exchange server.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |         $exchangeRates = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         foreach ($xml->observations->o->v as $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |             $currencyCode = substr((string)$e->attributes()->s, 2, 3); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             $exchangeRates[$currencyCode] = new ExchangeRate( | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |                 $this->getBaseCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |                 $currencyCode, | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |                 1 / (float)$e | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |         $exchangeRates[$this->getBaseCode()] = new ExchangeRate( | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |             $this->getBaseCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |             $this->getBaseCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |             1.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         return $exchangeRates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |