Files
firefly-iii/app/Services/Currency/FixerIO.php

97 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* FixerIO.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
2017-09-29 15:32:35 +02:00
use Requests_Exception;
/**
2017-11-15 12:25:49 +01:00
* Class FixerIO.
*/
class FixerIO implements ExchangeRateInterface
{
2017-11-15 12:25:49 +01:00
/** @var User */
protected $user;
2017-12-17 14:30:53 +01:00
/**
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
* @return CurrencyExchangeRate
*/
public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate
{
2017-09-29 15:32:35 +02:00
$uri = sprintf('https://api.fixer.io/%s?base=%s&symbols=%s', $date->format('Y-m-d'), $fromCurrency->code, $toCurrency->code);
$statusCode = -1;
try {
$result = Requests::get($uri);
$statusCode = $result->status_code;
$body = $result->body;
} catch (Requests_Exception $e) {
// don't care about error
$body = sprintf('Requests_Exception: %s', $e->getMessage());
}
// Requests_Exception
$rate = 1.0;
$content = null;
2017-11-15 12:25:49 +01:00
if (200 !== $statusCode) {
2017-09-29 15:32:35 +02:00
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
}
// get rate from body:
2017-11-15 12:25:49 +01:00
if (200 === $statusCode) {
2017-09-29 15:32:35 +02:00
$content = json_decode($body, true);
}
2017-11-15 12:25:49 +01:00
if (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
2017-06-05 08:31:22 +02:00
*
* @return mixed|void
*/
public function setUser(User $user)
{
$this->user = $user;
}
2017-07-07 08:09:42 +02:00
}