Completed
Push — master ( 6253d9...eb0c57 )
by Nikola
12:54
created

XmlParser   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 158
ccs 72
cts 72
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 4 1
A onDocumentStart() 0 8 1
A onElementStart() 0 8 2
B onElementData() 0 33 11
B onElementEnd() 0 40 5
A onDocumentEnd() 0 4 1
A onParseError() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * Implementation of exchange rate crawler for National Bank of Serbia, http://www.nbs.rs.
6
 *
7
 * (c) 2017 RunOpenCode
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace RunOpenCode\ExchangeRate\NationalBankOfSerbia\Parser;
13
14
use RunOpenCode\ExchangeRate\Contract\RateInterface;
15
use RunOpenCode\ExchangeRate\Model\Rate;
16
use RunOpenCode\ExchangeRate\NationalBankOfSerbia\Api;
17
use RunOpenCode\ExchangeRate\NationalBankOfSerbia\Enum\RateType;
18
use RunOpenCode\ExchangeRate\NationalBankOfSerbia\Exception\RuntimeException;
19
use RunOpenCode\Sax\Handler\AbstractSaxHandler;
20
21
/**
22
 * Class XmlParser
23
 *
24
 * Parse XML document with daily rates from National Bank of Serbia.
25
 *
26
 * @package RunOpenCode\ExchangeRate\NationalBankOfSerbia\Parser
27
 */
28
class XmlParser extends AbstractSaxHandler
29
{
30
    /**
31
     * @var RateInterface[]
32
     */
33
    private $rates;
34
35
    /**
36
     * @var \SplStack
37
     */
38
    private $stack;
39
40
    /**
41
     * @var array
42
     */
43
    private $currentRate;
44
45
    /**
46
     * @var \DateTime
47
     */
48
    private $date;
49
50
    /**
51
     * @var string
52
     */
53
    private $rateType;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 12
    protected function onDocumentStart($parser, $stream)
59
    {
60 12
        $this->rates = array();
61 12
        $this->stack = new \SplStack();
62 12
        $this->currentRate = array();
63 12
        $this->date = new \DateTime('now');
64 12
        $this->rateType = RateType::MEDIAN;
65 12
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 11
    protected function onElementStart($parser, $name, $attributes)
71
    {
72 11
        $this->stack->push($name);
73
74 11
        if ($name === 'ITEM') {
75 10
            $this->currentRate = array();
76
        }
77 11
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 10
    protected function onElementData($parser, $data)
83
    {
84 10
        if (!empty($data)) {
85 10
            switch ($this->stack->top()) {
86 10
                case 'DATE':
87 9
                    $this->date = \DateTime::createFromFormat('d.m.Y', $data);
88 9
                    break;
89 10
                case 'TYPE':
90 9
                    $data = trim($data);
91 9
                    if ($data === 'FOREIGN EXCHANGE') {
92 3
                        $this->rateType = 'foreign_exchange';
93 6
                    } elseif ($data === 'FOREIGN CASH') {
94 3
                        $this->rateType = 'foreign_cash';
95
                    }
96 9
                    break;
97 10
                case 'CURRENCY':
98 10
                    $this->currentRate['currencyCode'] = trim($data);
99 10
                    break;
100 10
                case 'UNIT':
101 10
                    $this->currentRate['unit'] = (int) trim($data);
102 10
                    break;
103 10
                case 'BUYING_RATE':
104 6
                    $this->currentRate['buyingRate'] = (float) trim($data);
105 6
                    break;
106 10
                case 'SELLING_RATE':
107 6
                    $this->currentRate['sellingRate'] = (float) trim($data);
108 6
                    break;
109 10
                case 'MIDDLE_RATE':
110 4
                    $this->currentRate['middleRate'] = (float) trim($data);
111 4
                    break;
112
            }
113
        }
114 10
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 10
    protected function onElementEnd($parser, $name)
120
    {
121 10
        $this->stack->pop();
122
123 10
        $buildRate = function ($value, $currencyCode, $rateType, $date) {
124 10
            return new Rate(Api::NAME, $value, $currencyCode, $rateType, $date, 'RSD', new \DateTime('now'), new \DateTime('now'));
125
        };
126
127 10
        if ($name === 'ITEM') {
128
129 10
            if (array_key_exists('buyingRate', $this->currentRate)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130 6
                $this->rates[] = $buildRate(
131 6
                    $this->currentRate['buyingRate'] / $this->currentRate['unit'],
132 6
                    $this->currentRate['currencyCode'],
133 6
                    $this->rateType.'_buying',
134 6
                    $this->date
135
                );
136
            }
137
138 10
            if (array_key_exists('sellingRate', $this->currentRate)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139 6
                $this->rates[] = $buildRate(
140 6
                    $this->currentRate['sellingRate'] / $this->currentRate['unit'],
141 6
                    $this->currentRate['currencyCode'],
142 6
                    $this->rateType.'_selling',
143 6
                    $this->date
144
                );
145
            }
146
147 10
            if (array_key_exists('middleRate', $this->currentRate)) {
148 4
                $this->rates[] = $buildRate(
149 4
                    $this->currentRate['middleRate'] / $this->currentRate['unit'],
150 4
                    $this->currentRate['currencyCode'],
151 4
                    RateType::MEDIAN,
152 4
                    $this->date
153
                );
154
            }
155
156 10
            $this->currentRate = array();
157
        }
158 10
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 11
    protected function onDocumentEnd($parser, $stream)
164
    {
165
        // noop
166 11
    }
167
168
    /**
169
     * {@inheritdoc}
170
     *
171
     * @throws \RuntimeException
172
     */
173 1
    protected function onParseError($message, $code, $lineno)
174
    {
175 1
        throw new RuntimeException(sprintf('Unable to parse XML source from National Bank of Serbia, reason: "%s", lineno: "%s".', $message, $lineno), $code);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181 11
    protected function getResult()
182
    {
183 11
        return $this->rates;
184
    }
185
}
186