ExchangeRateService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExchangeRates() 0 23 3
A getExchangeRate() 0 10 2
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
}