1 | <?php |
||
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)) { |
|
|
|||
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)) { |
|
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() |
|
185 | } |
||
186 |
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.