Passed
Push — master ( 529080...279f50 )
by Greg
02:25
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
        if ($result === false) return false;
17
        $json_result = json_decode($result);
18
        $conversion_rate = $json_result->{$currency_slug}->val;
19
20
        if (!is_null($conversion_rate) && $conversion_rate != '') {
21
22
            return $conversion_rate;
23
24
        } else {
25
26
            return false;
27
28
        }
29
    }
30
31
    public function getConvUrl($from_currency, $to_currency)
32
    {
33
        $currency_slug = $from_currency . '_' . $to_currency;
34
        return array($currency_slug, 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $currency_slug . '&compact=y');
35
    }
36
}
37