1 | <?php |
||||
2 | /** |
||||
3 | * CurrencyExchangeRateController.php |
||||
4 | * Copyright (c) 2018 [email protected] |
||||
5 | * |
||||
6 | * This file is part of Firefly III. |
||||
7 | * |
||||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||||
9 | * it under the terms of the GNU General Public License as published by |
||||
10 | * the Free Software Foundation, either version 3 of the License, or |
||||
11 | * (at your option) any later version. |
||||
12 | * |
||||
13 | * Firefly III is distributed in the hope that it will be useful, |
||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
16 | * GNU General Public License for more details. |
||||
17 | * |
||||
18 | * You should have received a copy of the GNU General Public License |
||||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||||
20 | */ |
||||
21 | |||||
22 | declare(strict_types=1); |
||||
23 | |||||
24 | namespace FireflyIII\Api\V1\Controllers; |
||||
25 | |||||
26 | use Carbon\Carbon; |
||||
27 | use FireflyIII\Exceptions\FireflyException; |
||||
28 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||||
29 | use FireflyIII\Services\Currency\ExchangeRateInterface; |
||||
30 | use FireflyIII\Transformers\CurrencyExchangeRateTransformer; |
||||
31 | use FireflyIII\User; |
||||
32 | use Illuminate\Http\JsonResponse; |
||||
33 | use Illuminate\Http\Request; |
||||
34 | use League\Fractal\Manager; |
||||
35 | use League\Fractal\Resource\Item; |
||||
36 | use League\Fractal\Serializer\JsonApiSerializer; |
||||
37 | |||||
38 | /** |
||||
39 | * Class CurrencyExchangeRateController |
||||
40 | */ |
||||
41 | class CurrencyExchangeRateController extends Controller |
||||
42 | { |
||||
43 | /** @var CurrencyRepositoryInterface The currency repository */ |
||||
44 | private $repository; |
||||
45 | |||||
46 | /** |
||||
47 | * CurrencyExchangeRateController constructor. |
||||
48 | */ |
||||
49 | public function __construct() |
||||
50 | { |
||||
51 | parent::__construct(); |
||||
52 | $this->middleware( |
||||
53 | function ($request, $next) { |
||||
54 | /** @var User $admin */ |
||||
55 | $admin = auth()->user(); |
||||
56 | |||||
57 | $this->repository = app(CurrencyRepositoryInterface::class); |
||||
58 | $this->repository->setUser($admin); |
||||
59 | |||||
60 | return $next($request); |
||||
61 | } |
||||
62 | ); |
||||
63 | |||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Show an exchange rate. |
||||
68 | * |
||||
69 | * @param Request $request |
||||
70 | * |
||||
71 | * @return JsonResponse |
||||
72 | * @throws FireflyException |
||||
73 | */ |
||||
74 | public function index(Request $request): JsonResponse |
||||
75 | { |
||||
76 | // create some objects: |
||||
77 | $manager = new Manager; |
||||
78 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||||
79 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||||
80 | |||||
81 | $fromCurrency = $this->repository->findByCodeNull($request->get('from') ?? 'EUR'); |
||||
82 | $toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD'); |
||||
83 | |||||
84 | if (null === $fromCurrency) { |
||||
85 | throw new FireflyException('Unknown source currency.'); |
||||
86 | } |
||||
87 | if (null === $toCurrency) { |
||||
88 | throw new FireflyException('Unknown destination currency.'); |
||||
89 | } |
||||
90 | |||||
91 | $dateObj = Carbon::createFromFormat('Y-m-d', $request->get('date') ?? date('Y-m-d')); |
||||
92 | $this->parameters->set('from', $fromCurrency->code); |
||||
93 | $this->parameters->set('to', $toCurrency->code); |
||||
94 | $this->parameters->set('date', $dateObj->format('Y-m-d')); |
||||
95 | $this->parameters->set('amount', $request->get('amount')); |
||||
96 | |||||
97 | $rate = $this->repository->getExchangeRate($fromCurrency, $toCurrency, $dateObj); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
98 | if (null === $rate) { |
||||
99 | /** @var User $admin */ |
||||
100 | $admin = auth()->user(); |
||||
101 | // create service: |
||||
102 | /** @var ExchangeRateInterface $service */ |
||||
103 | $service = app(ExchangeRateInterface::class); |
||||
104 | $service->setUser($admin); |
||||
105 | $rate = $service->getRate($fromCurrency, $toCurrency, $dateObj); |
||||
0 ignored issues
–
show
It seems like
$dateObj can also be of type false ; however, parameter $date of FireflyIII\Services\Curr...ateInterface::getRate() does only seem to accept Carbon\Carbon , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
106 | } |
||||
107 | /** @var CurrencyExchangeRateTransformer $transformer */ |
||||
108 | $transformer = app(CurrencyExchangeRateTransformer::class); |
||||
109 | $transformer->setParameters($this->parameters); |
||||
110 | $resource = new Item($rate, $transformer, 'currency_exchange_rates'); |
||||
111 | |||||
112 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||||
113 | } |
||||
114 | } |
||||
115 |