Passed
Push — master ( 8f4bcc...5aeb2c )
by Greg
02:04
created

Currency::getConvRate()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 5
nop 2
dl 0
loc 26
rs 9.0777
c 0
b 0
f 0
1
<?php
2
namespace GJClasses;
3
4
class Currency
5
{
6
    public $source;
7
8
    public function __construct($source)
9
    {
10
        $this->source = $source;
11
    }
12
13
    public function convertAmount($amount, $from_currency, $to_currency)
14
    {
15
        return $this->getConvRate($from_currency, $to_currency) * $amount;
16
    }
17
18
    public function getConvRate($from_currency, $to_currency)
19
    {
20
        $conversion_rate = 0.0;
21
22
        if ($this->source === 'era') {
23
24
            $full_url = 'https://api.exchangeratesapi.io/latest?base=' . $from_currency . '&symbols=' . $to_currency;
25
            $remote = new Remote();
26
            $result = $remote->getFileContents($full_url);
27
            if ($result === false) return false;
28
            $json_result = json_decode($result, true);
29
            $conversion_rate = $json_result['rates'][$to_currency];
30
31
        } elseif ($this->source === 'fcca') {
32
33
            $currency_slug = $from_currency . '_' . $to_currency;
34
            $full_url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $currency_slug . '&compact=y';
35
            $remote = new Remote();
36
            $result = $remote->getFileContents($full_url);
37
            if ($result === false || $result === '{}') return false;
38
            $json_result = json_decode($result);
39
            $conversion_rate = $json_result->{$currency_slug}->val;
40
41
        }
42
43
        return $conversion_rate;
44
    }
45
}
46