Passed
Push — dev ( 529080...aaafdd )
by Greg
02:38
created

Money::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace GJClasses;
3
4
class Money
5
{
6
    public function convertAmount($amount, $from_currency, $to_currency)
7
    {
8
        return $this->getConvRate($from_currency, $to_currency) * $amount;
9
    }
10
11
    public function getConvRate($from_currency, $to_currency)
12
    {
13
        list($currency_slug, $full_url) = $this->getConvUrl($from_currency, $to_currency);
14
        $remote = new Remote();
15
        $result = $remote->getFileContents($full_url);
16
        $json_result = json_decode($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $json of json_decode() does only seem to accept string, 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 ignore-type  annotation

16
        $json_result = json_decode(/** @scrutinizer ignore-type */ $result);
Loading history...
17
        $conversion_rate = $json_result->{$currency_slug}->val;
18
19
        if (!is_null($conversion_rate) && $conversion_rate != '') {
20
21
            return $conversion_rate;
22
23
        } else {
24
25
            return false;
26
27
        }
28
    }
29
30
    public function getConvUrl($from_currency, $to_currency)
31
    {
32
        $currency_slug = $from_currency . '_' . $to_currency;
33
        return array($currency_slug, 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $currency_slug . '&compact=y');
34
    }
35
}
36