ExRatesTable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parseXml() 0 28 5
1
<?php
2
3
namespace Ksdev\NBPCurrencyConverter;
4
5
class ExRatesTable
6
{
7
    /** @var string */
8
    public $rawContent;
9
10
    /** @var array */
11
    public $parsedContent;
12
13
    /**
14
     * @param string $rawContent Raw xml content
15
     *
16
     * @throws \Exception
17
     */
18
    public function __construct($rawContent)
19
    {
20
        $this->rawContent = $rawContent;
21
        $this->parsedContent = $this->parseXml($rawContent);
22
    }
23
24
    /**
25
     * Transform the raw xml content into an array
26
     *
27
     * @param string $rawContent
28
     *
29
     * @return array
30
     *
31
     * @throws \Exception
32
     */
33
    private function parseXml($rawContent)
34
    {
35
        $xml = new \SimpleXMLElement($rawContent);
36
        if (empty($xml->numer_tabeli) || empty($xml->data_publikacji) || empty($xml->pozycja)) {
37
            throw new \Exception('Invalid xml response content');
38
        }
39
        $rates = [
40
            'numer_tabeli'    => (string)$xml->numer_tabeli,
41
            'data_publikacji' => (string)$xml->data_publikacji,
42
            'waluty'          => [
43
                'PLN' => [
44
                    'nazwa_waluty' => 'złoty polski',
45
                    'przelicznik'  => '1',
46
                    'kurs_sredni'  => '1'
47
                ]
48
            ]
49
        ];
50
        foreach ($xml->pozycja as $pozycja) {
51
            $rates['waluty'] += [
52
                (string)$pozycja->kod_waluty => [
53
                    'nazwa_waluty' => (string)$pozycja->nazwa_waluty,
54
                    'przelicznik'  => (string)$pozycja->przelicznik,
55
                    'kurs_sredni'  => (string)$pozycja->kurs_sredni
56
                ]
57
            ];
58
        }
59
        return $rates;
60
    }
61
}
62