Passed
Push — main ( 4a5f0c...02f3c9 )
by MyFatoorah
06:23 queued 02:44
created

MyFatoorahList::getCurrencyRate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace MyFatoorah\Library\API;
4
5
use MyFatoorah\Library\MyFatoorah;
6
use Exception;
7
8
/**
9
 * MyFatoorahList handles the list process of MyFatoorah API endpoints
10
 *
11
 * @author    MyFatoorah <[email protected]>
12
 * @copyright 2021 MyFatoorah, All rights reserved
13
 * @license   GNU General Public License v3.0
14
 */
15
class MyFatoorahList extends MyFatoorah
16
{
17
    //-----------------------------------------------------------------------------------------------------------------------------------------
18
19
    /**
20
     * Gets the rate of a given currency according to the default currency of the MyFatoorah portal account.
21
     *
22
     * @param string $currency The currency that will be converted into the currency of MyFatoorah portal account.
23
     *
24
     * @return number       The conversion rate converts a given currency to the MyFatoorah account default currency.
25
     *
26
     * @throws Exception    Throw exception if the input currency is not support by MyFatoorah portal account.
27
     */
28
    public function getCurrencyRate($currency)
29
    {
30
31
        $json = $this->getCurrencyRates();
32
        foreach ($json as $value) {
33
            if ($value->Text == $currency) {
34
                return $value->Value;
35
            }
36
        }
37
        throw new Exception('The selected currency is not supported by MyFatoorah');
38
    }
39
40
    //-----------------------------------------------------------------------------------------------------------------------------------------
41
42
    /**
43
     * Get list of MyFatoorah currency rates
44
     *
45
     * @return array
46
     */
47
    public function getCurrencyRates()
48
    {
49
50
        $url = "$this->apiURL/v2/GetCurrenciesExchangeList";
51
        return (array) $this->callAPI($url, null, null, 'Get Currencies Exchange List');
52
    }
53
54
    //-----------------------------------------------------------------------------------------------------------------------------------------
55
}
56