firefly-iii/app/Api/V1/Controllers/CurrencyExchangeRateController.php

113 lines
3.8 KiB
PHP
Raw Normal View History

2018-06-28 00:32:58 -05:00
<?php
/**
* CurrencyExchangeRateController.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\Api\V1\Controllers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Services\Currency\ExchangeRateInterface;
use FireflyIII\Transformers\CurrencyExchangeRateTransformer;
2018-07-04 23:10:35 -05:00
use FireflyIII\User;
2018-06-28 00:32:58 -05:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use League\Fractal\Resource\Item;
/**
* Class CurrencyExchangeRateController
2019-09-04 10:39:39 -05:00
*
* @codeCoverageIgnore
2018-06-28 00:32:58 -05:00
*/
class CurrencyExchangeRateController extends Controller
{
/** @var CurrencyRepositoryInterface The currency repository */
2018-06-28 00:32:58 -05:00
private $repository;
/**
* CurrencyExchangeRateController constructor.
*
2018-06-28 00:32:58 -05:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2018-07-04 23:10:35 -05:00
/** @var User $admin */
$admin = auth()->user();
2018-06-28 00:32:58 -05:00
$this->repository = app(CurrencyRepositoryInterface::class);
2018-07-04 23:10:35 -05:00
$this->repository->setUser($admin);
2018-06-28 00:32:58 -05:00
return $next($request);
}
);
}
/**
* Show an exchange rate.
*
2018-06-28 00:32:58 -05:00
* @param Request $request
*
* @return JsonResponse
* @throws FireflyException
*/
public function index(Request $request): JsonResponse
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-28 00:32:58 -05:00
$fromCurrency = $this->repository->findByCodeNull($request->get('from') ?? 'EUR');
$toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD');
if (null === $fromCurrency) {
throw new FireflyException('Unknown source currency.');
}
if (null === $toCurrency) {
throw new FireflyException('Unknown destination currency.');
}
/** @var Carbon $dateObj */
2018-07-05 11:02:02 -05:00
$dateObj = Carbon::createFromFormat('Y-m-d', $request->get('date') ?? date('Y-m-d'));
2018-06-28 00:32:58 -05:00
$this->parameters->set('from', $fromCurrency->code);
$this->parameters->set('to', $toCurrency->code);
$this->parameters->set('date', $dateObj->format('Y-m-d'));
2018-12-08 23:51:53 -06:00
$this->parameters->set('amount', $request->get('amount'));
2018-06-28 00:32:58 -05:00
$rate = $this->repository->getExchangeRate($fromCurrency, $toCurrency, $dateObj);
if (null === $rate) {
2018-07-04 23:10:35 -05:00
/** @var User $admin */
$admin = auth()->user();
2018-06-28 00:32:58 -05:00
// create service:
/** @var ExchangeRateInterface $service */
$service = app(ExchangeRateInterface::class);
2018-07-04 23:10:35 -05:00
$service->setUser($admin);
2018-06-28 00:32:58 -05:00
$rate = $service->getRate($fromCurrency, $toCurrency, $dateObj);
}
2018-12-15 15:03:05 -06:00
/** @var CurrencyExchangeRateTransformer $transformer */
$transformer = app(CurrencyExchangeRateTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($rate, $transformer, 'currency_exchange_rates');
2018-06-28 00:32:58 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
}