Completed
Push — master ( 18c0ae...d28c83 )
by Nikola
03:43
created

NationalBankOfSerbiaXmlSaxParser::onEnd()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 53
Code Lines 32

Duplication

Lines 18
Ratio 33.96 %

Code Coverage

Tests 28
CRAP Score 5.9256

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 18
loc 53
ccs 28
cts 42
cp 0.6667
rs 8.7155
cc 5
eloc 32
nc 9
nop 2
crap 5.9256

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate\Source\Api;
11
use RunOpenCode\ExchangeRate\Contract\RateInterface;
12
use RunOpenCode\ExchangeRate\Model\Rate;
13
use RunOpenCode\ExchangeRate\Source\NationalBankOfSerbiaDomCrawlerSource;
14
15
/**
16
 * Class NationalBankOfSerbiaXmlSaxParser
17
 *
18
 * Parses XML files from National bank of Serbia.
19
 *
20
 * @package RunOpenCode\ExchangeRate\Source\Api
21
 * @internal
22
 */
23
class NationalBankOfSerbiaXmlSaxParser
24
{
25
    /**
26
     * @var RateInterface[]
27
     */
28
    private $rates;
29
30
    /**
31
     * @var \SplStack
32
     */
33
    private $stack;
34
35
    /**
36
     * @var array
37
     */
38
    private $currentRate;
39
40
    /**
41
     * @var \DateTime
42
     */
43
    private $date;
44
45
    /**
46
     * @var string
47
     */
48
    private $rateType;
49
50
    /**
51
     * Parse XML from National bank of Serbia.
52
     *
53
     * @param string $xml
54
     * @return RateInterface[]
55
     */
56 2
    public function parse($xml)
57
    {
58 2
        $this->rates = array();
59 2
        $this->stack = new \SplStack();
60 2
        $this->currentRate = array();
61 2
        $this->date = new \DateTime('now');
62 2
        $this->rateType = 'default';
63
64 2
        $parser = xml_parser_create();
65
66 2
        xml_set_element_handler(
67 2
            $parser,
68
            \Closure::bind(function($parser, $name, $attributes) {
69 2
                $this->onStart($parser, $name, $attributes);
70 2
            }, $this),
71
            \Closure::bind(function($parser, $name) {
72 2
                $this->onEnd($parser, $name);
73 2
            }, $this)
74 2
        );
75
76 2
        xml_set_character_data_handler(
77 2
            $parser,
78
            \Closure::bind(function($parser, $data) {
79 2
                $this->onData($parser, $data);
80 2
            }, $this));
81
82 2
        xml_parse($parser, $xml);
83 2
        xml_parser_free($parser);
84
85 2
        return $this->rates;
86
    }
87
88
    /**
89
     * SAX on tag start callback.
90
     *
91
     * @param $parser
92
     * @param $name
93
     * @param $attributes
94
     */
95 2
    private function onStart($parser, $name, $attributes)
96
    {
97 2
        $this->stack->push($name);
98
99 2
        if ($name === 'ITEM') {
100 2
            $this->currentRate = array();
101 2
        }
102 2
    }
103
104
    /**
105
     * SAX on tag end callback.
106
     *
107
     * @param $parser
108
     * @param $name
109
     */
110 2
    private function onEnd($parser, $name)
111
    {
112 2
        $this->stack->pop();
113
114 2
        $buildRate = function($value, $currencyCode, $rateType, $date) {
115
116 2
            return new Rate(
117 2
                NationalBankOfSerbiaDomCrawlerSource::NAME,
118 2
                $value,
119 2
                $currencyCode,
120 2
                $rateType,
121 2
                $date,
122 2
                'RSD',
123 2
                new \DateTime('now'),
124 2
                new \DateTime('now')
125 2
            );
126 2
        };
127
128 2
        if ($name === 'ITEM') {
129
130 2 View Code Duplication
            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...
131
132
                $this->rates[] = $buildRate(
133
                    $this->currentRate['buyingRate'] / $this->currentRate['unit'],
134
                    $this->currentRate['currencyCode'],
135
                    $this->rateType . '_buying',
136
                    $this->date
137
                );
138
            }
139
140 2 View Code Duplication
            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...
141
142
                $this->rates[] = $buildRate(
143
                    $this->currentRate['sellingRate'] / $this->currentRate['unit'],
144
                    $this->currentRate['currencyCode'],
145
                    $this->rateType . '_selling',
146
                    $this->date
147
                );
148
            }
149
150 2
            if (array_key_exists('middleRate', $this->currentRate)) {
151
152 2
                $this->rates[] = $buildRate(
153 2
                    $this->currentRate['middleRate'] / $this->currentRate['unit'],
154 2
                    $this->currentRate['currencyCode'],
155 2
                    'default',
156 2
                    $this->date
157 2
                );
158 2
            }
159
160 2
            $this->currentRate = array();
161 2
        }
162 2
    }
163
164
    /**
165
     * SAX on data callback.
166
     *
167
     * @param $parser
168
     * @param $data
169
     */
170 2
    private function onData($parser, $data)
171
    {
172 2
        if (!empty($data)) {
173
174 2
            switch ($this->stack->top()) {
175 2
                case 'DATE':
176 2
                    $this->date = \DateTime::createFromFormat('d.m.Y', $data);
177 2
                    break;
178 2
                case 'TYPE':
179 2
                    $data = trim($data);
180 2
                    if ($data === 'FOREIGN EXCHANGE') {
181
                        $this->rateType = 'foreign_exchange';
182 2
                    } elseif ($data === 'FOREIGN CASH') {
183
                        $this->rateType = 'foreign_cache';
184
                    }
185 2
                    break;
186 2
                case 'CURRENCY':
187 2
                    $this->currentRate['currencyCode'] = trim($data);
188 2
                    break;
189 2
                case 'UNIT':
190 2
                    $this->currentRate['unit'] = (int) trim($data);
191 2
                    break;
192 2
                case 'BUYING_RATE':
193
                    $this->currentRate['buyingRate'] = (float) trim($data);
194
                    break;
195 2
                case 'SELLING_RATE':
196
                    $this->currentRate['sellingRate'] = (float) trim($data);
197
                    break;
198 2
                case 'MIDDLE_RATE':
199 2
                    $this->currentRate['middleRate'] = (float) trim($data);
200 2
                    break;
201 2
            }
202 2
        }
203 2
    }
204
205
    /**
206
     * Parse XML from National bank of Serbia.
207
     *
208
     * @param string $xml
209
     * @return RateInterface[]
210
     */
211 2
    public static function parseXml($xml)
212
    {
213 2
        $parser = new static();
214 2
        return $parser->parse($xml);
215
    }
216
}
217