ExchangeRateService::getExchangeRates()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
crap 3
1
<?php
2
3
namespace VojtaSvoboda\CnbRates\Services;
4
5
class ExchangeRateService extends BaseService
6
{
7
    protected $baseUrl = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt';
8
9
    protected $ident = 'exchange';
10
11
    /**
12
     * Get all exchange rates
13
     *
14
     * @param null $date Returns today rates by default
15
     *
16
     * @return array
17
     */
18 8
    public function getExchangeRates($date = null)
19
    {
20 8
        $keyIndex = 3;
21 8
        $rates = $this->getData($date, $keyIndex);
22 8
        $returnRates = [];
23 8
        $rounding = 3;
24
25 8
        foreach ($rates as $key => $rate)
26
        {
27 8
            if (sizeof($rate) >= 4)
28 8
            {
29 8
                $returnRates[$key] = [
30 8
                    'country' => $rate[0],
31 8
                    'currency' => $rate[1],
32 8
                    'base' => intval($rate[2]),
33 8
                    'symbol' => $rate[3],
34 8
                    'rate' => $this->priceStringToFloat($rate[4], $rounding),
35
                ];
36 8
            }
37 8
        }
38
39 8
        return $returnRates;
40
    }
41
42
    /**
43
     * Return exchange rate for specific currency and date
44
     *
45
     * @param string $currency
46
     * @param null $date Returns today rates by default
47
     *
48
     * @return float
49
     */
50 4
    public function getExchangeRate($currency = 'EUR', $date = null)
51
    {
52 4
        $data = $this->getExchangeRates($date);
53
54 4
        if (isset($data[$currency]['rate'])) {
55 2
            return $data[$currency]['rate'];
56
        }
57
58 2
        return null;
59
    }
60
}