GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Services/Currency/FixerIOv2.php (1 issue)

1
<?php
2
/**
3
 * FixerIOv2.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
declare(strict_types=1);
22
23
namespace FireflyIII\Services\Currency;
24
25
use Carbon\Carbon;
26
use Exception;
27
use FireflyIII\Models\CurrencyExchangeRate;
28
use FireflyIII\Models\TransactionCurrency;
29
use FireflyIII\User;
30
use Log;
31
use Requests;
32
33
/**
34
 * Class FixerIOv2.
35
 */
36
class FixerIOv2 implements ExchangeRateInterface
37
{
38
    /** @var User */
39
    protected $user;
40
41
    /**
42
     * @param TransactionCurrency $fromCurrency
43
     * @param TransactionCurrency $toCurrency
44
     * @param Carbon              $date
45
     *
46
     * @return CurrencyExchangeRate
47
     */
48
    public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate
49
    {
50
        // create new exchange rate with default values.
51
        // create new currency exchange rate object:
52
        $exchangeRate = new CurrencyExchangeRate;
53
        $exchangeRate->user()->associate($this->user);
54
        $exchangeRate->fromCurrency()->associate($fromCurrency);
55
        $exchangeRate->toCurrency()->associate($toCurrency);
56
        $exchangeRate->date = $date;
57
        $exchangeRate->rate = 0;
58
59
        // get API key
60
        $apiKey = env('FIXER_API_KEY', '');
61
62
        // if no API key, return unsaved exchange rate.
63
        if (strlen($apiKey) === 0) {
64
            return $exchangeRate;
65
        }
66
67
        // build URI
68
        $uri        = sprintf(
69
            'http://data.fixer.io/api/%s?access_key=%s&base=%s&symbols=%s',
70
            $date->format('Y-m-d'), $apiKey, $fromCurrency->code, $toCurrency->code
71
        );
72
        $statusCode = -1;
73
        Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
74
        try {
75
            $result     = Requests::get($uri);
76
            $statusCode = $result->status_code;
77
            $body       = $result->body;
78
            Log::debug(sprintf('Result status code is %d', $statusCode));
79
        } catch (Exception $e) {
80
            // don't care about error
81
            $body = sprintf('Requests_Exception: %s', $e->getMessage());
82
        }
83
84
        // Requests_Exception
85
        $content = null;
86
        if (200 !== $statusCode) {
87
            Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
88
        }
89
        // get rate from body:
90
        if (200 === $statusCode) {
91
            $content = json_decode($body, true);
92
        }
93
        if (null !== $content) {
94
            $code = $toCurrency->code;
95
            $rate = (float)($content['rates'][$code] ?? 0);
96
        }
97
        Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []);
98
        $exchangeRate->rate = $rate;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rate does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
        if ($rate !== 0.0) {
100
            $exchangeRate->save();
101
        }
102
103
        return $exchangeRate;
104
    }
105
106
    /**
107
     * @param User $user
108
     *
109
     * @return mixed|void
110
     */
111
    public function setUser(User $user)
112
    {
113
        $this->user = $user;
114
    }
115
}
116