PriborService::getPriborRate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace VojtaSvoboda\CnbRates\Services;
4
5
class PriborService extends BaseService
6
{
7
    protected $baseUrl = 'https://www.cnb.cz/cs/financni_trhy/penezni_trh/pribor/denni.txt';
8
9
    protected $ident = 'pribor';
10
11
    /**
12
     * Returns all PRIBOR rates for specific date
13
     *
14
     * @param null $date Returns today rates by default
15
     *
16
     * @return array
17
     */
18 7
    public function getPriborRates($date = null)
19
    {
20 7
        $data = $this->getData($date);
21 7
        $intervalKeys = $this->getIntervalKeys();
22 7
        $returnRates = [];
23
24 7
        foreach ($data as $key => $rate) {
25 3
            if (isset($intervalKeys[$key])) {
26 3
                $returnRates[$intervalKeys[$key]] = $this->priceStringToFloat($rate[2], 2);
27 3
            }
28 7
        }
29
30 7
        return $returnRates;
31
    }
32
33
    /**
34
     * Returns PRIBOR rates for specific date and interval
35
     *
36
     * @param null $date Returns today rates by default
37
     * @param string $interval Year interval by default
38
     *
39
     * @return float
40
     */
41 3
    public function getPriborRate($date = null, $interval = 'year')
42
    {
43 3
        $data = $this->getPriborRates($date);
44
45 3
        if (isset($data[$interval])) {
46 1
            return $data[$interval];
47
        }
48
49 2
        return null;
50
    }
51
52
    /**
53
     * Get pairing keys between plugins API and ČNB format
54
     *
55
     * @return array
56
     */
57 7
    private function getIntervalKeys()
58
    {
59
        return [
60 7
            '1 rok' => 'year',
61 7
            '9 měsíců' => '9months',
62 7
            '6 měsíců' => '6months',
63 7
            '3 měsíce' => '3months',
64 7
            '2 měsíce' => '2months',
65 7
            '1 měsíc' => 'month',
66 7
            '14 dní' => '2weeks',
67 7
            '7 dní' => 'week',
68 7
            '1 den' => 'day',
69 7
        ];
70
    }
71
}