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

Currency   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConvRate() 0 26 6
A __construct() 0 3 1
A convertAmount() 0 3 1
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