mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-25 21:16:47 +00:00
First basic code for currency exchange rate routines.
This commit is contained in:
38
app/Services/Currency/ExchangeRateInterface.php
Normal file
38
app/Services/Currency/ExchangeRateInterface.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* ExchangeRateInterface.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Currency;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\User;
|
||||
|
||||
interface ExchangeRateInterface
|
||||
{
|
||||
/**
|
||||
* @param TransactionCurrency $fromCurrency
|
||||
* @param TransactionCurrency $toCurrency
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return CurrencyExchangeRate
|
||||
*/
|
||||
public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function setUser(User $user);
|
||||
|
||||
}
|
||||
69
app/Services/Currency/FixerIO.php
Normal file
69
app/Services/Currency/FixerIO.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* FixerIO.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Currency;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
use Requests;
|
||||
|
||||
/**
|
||||
* Class FixerIO
|
||||
*
|
||||
* @package FireflyIII\Services\Currency
|
||||
*/
|
||||
class FixerIO implements ExchangeRateInterface
|
||||
{
|
||||
/** @var User */
|
||||
protected $user;
|
||||
|
||||
public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate
|
||||
{
|
||||
$uri = sprintf('https://api.fixer.io/%s?base=%s&symbols=%s', $date->format('Y-m-d'), $fromCurrency->code, $toCurrency->code);
|
||||
$result = Requests::get($uri);
|
||||
$rate = 1.0;
|
||||
$content = null;
|
||||
if ($result->status_code !== 200) {
|
||||
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $result->status_code, $result->body));
|
||||
}
|
||||
// get rate from body:
|
||||
if ($result->status_code === 200) {
|
||||
$content = json_decode($result->body, true);
|
||||
}
|
||||
if (!is_null($content)) {
|
||||
$code = $toCurrency->code;
|
||||
$rate = isset($content['rates'][$code]) ? $content['rates'][$code] : '1';
|
||||
}
|
||||
|
||||
// create new currency exchange rate object:
|
||||
$exchangeRate = new CurrencyExchangeRate;
|
||||
$exchangeRate->user()->associate($this->user);
|
||||
$exchangeRate->fromCurrency()->associate($fromCurrency);
|
||||
$exchangeRate->toCurrency()->associate($toCurrency);
|
||||
$exchangeRate->date = $date;
|
||||
$exchangeRate->rate = $rate;
|
||||
$exchangeRate->save();
|
||||
|
||||
return $exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user