Currency   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 44
c 7
b 0
f 0
dl 0
loc 70
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convertAmount() 0 3 1
C getConvRate() 0 54 12
1
<?php
2
namespace GJClasses;
3
4
class Currency
5
{
6
    public $api_key;
7
    public $source;
8
9
    public function __construct($source, $api_key)
10
    {
11
        $this->source = $source;
12
        $this->api_key = $api_key;
13
    }
14
15
    public function convertAmount($amount, $from_currency, $to_currency)
16
    {
17
        return $this->getConvRate($from_currency, $to_currency) * $amount;
18
    }
19
20
    public function getConvRate($from_currency, $to_currency)
21
    {
22
        if ($from_currency == $to_currency) return 1.0;
23
24
        $conversion_rate = 0.0;
25
26
        if ($this->source === 'era') {
27
28
            $full_url = 'http://api.exchangeratesapi.io/v1/convert?access_key=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency;
29
            $remote = new Remote();
30
            $result = $remote->getFileContents($full_url);
31
            if ($result === false) return false;
32
            $json_result = json_decode($result, true);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; 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

32
            $json_result = json_decode(/** @scrutinizer ignore-type */ $result, true);
Loading history...
33
            $conversion_rate = $json_result['rates'][$to_currency];
34
35
        } elseif ($this->source === 'er-a') {
36
37
            $full_url = 'https://v6.exchangerate-api.com/v6/' . $this->api_key . '/pair/' . $from_currency . '/' . $to_currency;
38
            $remote = new Remote();
39
            $result = $remote->getFileContents($full_url);
40
            if ($result === false) return false;
41
            $json_result = json_decode($result, true);
42
            $conversion_rate = $json_result['conversion_rate'];
43
44
        } elseif ($this->source === 'fixer') {
45
46
            $full_url = 'http://data.fixer.io/api/convert?access_key=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency;
47
            $remote = new Remote();
48
            $result = $remote->getFileContents($full_url);
49
            if ($result === false) return false;
50
            $json_result = json_decode($result, true);
51
            $conversion_rate = $json_result['rates'][$to_currency];
52
53
        } elseif ($this->source === 'interzoid') {
54
55
            $full_url = 'https://api.interzoid.com/convertcurrency?license=' . $this->api_key . '&from=' . $from_currency . '&to=' . $to_currency . '&amount=1';
56
            $remote = new Remote();
57
            $result = $remote->getFileContents($full_url);
58
            if ($result === false) return false;
59
            $json_result = json_decode($result, true);
60
            $conversion_rate = $json_result['Converted'];
61
62
        } elseif ($this->source === 'erh') {
63
64
            $full_url = 'https://api.exchangerate.host/convert?from=' . $from_currency . '&to=' . $to_currency;
65
            $remote = new Remote();
66
            $result = $remote->getFileContents($full_url);
67
            if ($result === false) return false;
68
            $json_result = json_decode($result, true);
69
            $conversion_rate = $json_result['info']['rate'];
70
71
        }
72
73
        return $conversion_rate;
74
    }
75
}
76