| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * FixerIOv2.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Currency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | use FireflyIII\Models\CurrencyExchangeRate; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  | use GuzzleHttp\Client; | 
					
						
							|  |  |  | use GuzzleHttp\Exception\GuzzleException; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | use Log; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class FixerIOv2. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class FixerIOv2 implements ExchangeRateInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** @var User */ | 
					
						
							|  |  |  |     protected $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 08:41:03 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         if ('testing' === config('app.env')) { | 
					
						
							| 
									
										
										
										
											2019-06-07 18:20:15 +02:00
										 |  |  |             Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); | 
					
						
							| 
									
										
										
										
											2018-09-03 08:41:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param TransactionCurrency $fromCurrency | 
					
						
							|  |  |  |      * @param TransactionCurrency $toCurrency | 
					
						
							|  |  |  |      * @param Carbon              $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return CurrencyExchangeRate | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // create new exchange rate with default values.
 | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |         $rate         = 0; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         $exchangeRate = new CurrencyExchangeRate; | 
					
						
							|  |  |  |         $exchangeRate->user()->associate($this->user); | 
					
						
							|  |  |  |         $exchangeRate->fromCurrency()->associate($fromCurrency); | 
					
						
							|  |  |  |         $exchangeRate->toCurrency()->associate($toCurrency); | 
					
						
							| 
									
										
										
										
											2018-09-26 20:34:24 +02:00
										 |  |  |         $exchangeRate->date       = $date; | 
					
						
							|  |  |  |         $exchangeRate->rate       = $rate; | 
					
						
							|  |  |  |         $exchangeRate->updated_at = new Carbon; | 
					
						
							|  |  |  |         $exchangeRate->created_at = new Carbon; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get API key
 | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         $apiKey = config('firefly.fixer_api_key'); | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // if no API key, return unsaved exchange rate.
 | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |         if ('' === $apiKey) { | 
					
						
							|  |  |  |             Log::warning('No fixer.IO API key, will not do conversion.'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |             return $exchangeRate; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // build URI
 | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |         $uri = sprintf( | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |             'http://data.fixer.io/api/%s?access_key=%s&base=%s&symbols=%s', | 
					
						
							|  |  |  |             $date->format('Y-m-d'), $apiKey, $fromCurrency->code, $toCurrency->code | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri))); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |         $client = new Client; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |             $res        = $client->request('GET', $uri); | 
					
						
							|  |  |  |             $statusCode = $res->getStatusCode(); | 
					
						
							|  |  |  |             $body       = $res->getBody()->getContents(); | 
					
						
							|  |  |  |         } catch (GuzzleException|Exception $e) { | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |             // don't care about error
 | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |             $body       = sprintf('Guzzle exception: %s', $e->getMessage()); | 
					
						
							|  |  |  |             $statusCode = 500; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |         Log::debug(sprintf('Result status code is %d', $statusCode)); | 
					
						
							|  |  |  |         Log::debug(sprintf('Result body is: %s', $body)); | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $content = null; | 
					
						
							|  |  |  |         if (200 !== $statusCode) { | 
					
						
							|  |  |  |             Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |         $success = false; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         // get rate from body:
 | 
					
						
							|  |  |  |         if (200 === $statusCode) { | 
					
						
							|  |  |  |             $content = json_decode($body, true); | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |             $success = $content['success'] ?? false; | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |         if (null !== $content && true === $success) { | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |             $code = $toCurrency->code; | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |             $rate = (float)($content['rates'][$code] ?? 0); | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |             Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []); | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  |         $exchangeRate->rate = $rate; | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |         if (0.0 !== $rate) { | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |             Log::debug('Rate is not zero, save it!'); | 
					
						
							| 
									
										
										
										
											2018-03-30 06:33:04 +02:00
										 |  |  |             $exchangeRate->save(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 09:00:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $exchangeRate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed|void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |