1 | <?php |
||
32 | class NationalBankOfSerbiaDomCrawlerSource implements SourceInterface |
||
33 | { |
||
34 | /** |
||
35 | * API - Source of rates from National bank of Serbia. |
||
36 | */ |
||
37 | const SOURCE = 'http://www.nbs.rs/kursnaListaModul/naZeljeniDan.faces'; |
||
38 | |||
39 | /** |
||
40 | * API - Name of source. |
||
41 | */ |
||
42 | const NAME = 'national_bank_of_serbia'; |
||
43 | |||
44 | use LoggerAwareTrait; |
||
45 | |||
46 | /** |
||
47 | * List of supported exchange rate types and currencies. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private static $supports = array( |
||
52 | 'default' => array('EUR', 'AUD', 'CAD', 'CNY', 'HRK', 'CZK', 'DKK', 'HUF', 'JPY', 'KWD', 'NOK', 'RUB', 'SEK', 'CHF', |
||
53 | 'GBP', 'USD', 'BAM', 'PLN', 'ATS', 'BEF', 'FIM', 'FRF', 'DEM', 'GRD', 'IEP', 'ITL', 'LUF', 'PTE', |
||
54 | 'ESP'), |
||
55 | 'foreign_cache_buying' => array('EUR', 'CHF', 'USD'), |
||
56 | 'foreign_cache_selling' => array('EUR', 'CHF', 'USD'), |
||
57 | 'foreign_exchange_buying' => array('EUR', 'AUD', 'CAD', 'CNY', 'DKK', 'JPY', 'NOK', 'RUB', 'SEK', 'CHF', 'GBP', 'USD'), |
||
58 | 'foreign_exchange_selling' => array('EUR', 'AUD', 'CAD', 'CNY', 'DKK', 'JPY', 'NOK', 'RUB', 'SEK', 'CHF', 'GBP', 'USD') |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $cache; |
||
65 | |||
66 | /** |
||
67 | * @var Client |
||
68 | */ |
||
69 | protected $guzzleClient; |
||
70 | |||
71 | /** |
||
72 | * @var CookieJar |
||
73 | */ |
||
74 | protected $guzzleCookieJar; |
||
75 | |||
76 | 6 | public function __construct() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 4 | public function getName() |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 6 | public function fetch($currencyCode, $rateType = 'default', \DateTime $date = null) |
|
93 | { |
||
94 | 6 | $currencyCode = CurrencyCodeUtil::clean($currencyCode); |
|
95 | |||
96 | 4 | $this->validate($currencyCode, $rateType); |
|
97 | |||
98 | 4 | if ($date === null) { |
|
99 | 2 | $date = new \DateTime('now'); |
|
100 | 1 | } |
|
101 | |||
102 | 4 | if (!array_key_exists($rateType, $this->cache)) { |
|
103 | |||
104 | try { |
||
105 | |||
106 | 4 | $this->load($date, $rateType); |
|
107 | |||
108 | 4 | } catch (\Exception $e) { |
|
109 | 4 | $message = sprintf('Unable to load data from "%s" for "%s" of rate type "%s".', $this->getName(), $currencyCode, $rateType); |
|
110 | |||
111 | 4 | $this->getLogger()->emergency($message);; |
|
112 | 4 | throw new SourceNotAvailableException($message, 0, $e); |
|
113 | } |
||
114 | } |
||
115 | |||
116 | if (array_key_exists($currencyCode, $this->cache[$rateType])) { |
||
117 | return $this->cache[$rateType][$currencyCode]; |
||
118 | } |
||
119 | |||
120 | $message = sprintf('API Changed: source "%s" does not provide currency code "%s" for rate type "%s".', $this->getName(), $currencyCode, $rateType); |
||
121 | $this->getLogger()->critical($message); |
||
122 | throw new \RuntimeException($message); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Check if currency code and rate type are supported by this source. |
||
127 | * |
||
128 | * @param string $currencyCode Currency code. |
||
129 | * @param string $rateType Rate type. |
||
130 | * @throws ConfigurationException If currency code is not supported by source and rate type. |
||
131 | * @throws UnknownRateTypeException If rate type is unknown. |
||
132 | */ |
||
133 | 4 | protected function validate($currencyCode, $rateType) |
|
143 | |||
144 | /** |
||
145 | * @param \DateTime $date |
||
146 | * @return RateInterface[] |
||
147 | * @throws SourceNotAvailableException |
||
148 | */ |
||
149 | 4 | protected function load(\DateTime $date, $rateType) |
|
150 | { |
||
151 | 4 | $this->cache[$rateType] = array(); |
|
152 | |||
153 | 4 | $xml = $this->executeHttpRequest(self::SOURCE, 'POST', array(), array( |
|
154 | 4 | 'index:brKursneListe:' => '', |
|
155 | 4 | 'index:year' => $date->format('Y'), |
|
156 | 4 | 'index:inputCalendar1' => $date->format('d/m/Y'), |
|
157 | 4 | 'index:vrsta' => call_user_func(function($rateType) { |
|
158 | switch ($rateType) { |
||
159 | 4 | case 'foreign_cache_buying': // FALL TROUGH |
|
160 | 2 | case 'foreign_cache_selling': |
|
161 | return 1; |
||
162 | break; |
||
|
|||
163 | 2 | case 'foreign_exchange_buying': // FALL TROUGH |
|
164 | 4 | case 'foreign_exchange_selling': |
|
165 | return 2; |
||
166 | break; |
||
167 | 2 | default: |
|
168 | 4 | return 3; |
|
169 | break; |
||
170 | 2 | } |
|
171 | 4 | }, $rateType), |
|
172 | 4 | 'index:prikaz' => 3, // XML |
|
173 | 4 | 'index:buttonShow' => 'Show', |
|
174 | 4 | 'index' => 'index', |
|
175 | 4 | 'com.sun.faces.VIEW' => $this->getFormCsrfToken() |
|
176 | )); |
||
177 | |||
178 | $rates = NationalBankOfSerbiaXmlSaxParser::parseXml($xml); |
||
179 | |||
180 | /** |
||
181 | * @var RateInterface $rate |
||
182 | */ |
||
183 | foreach ($rates as $rate) { |
||
184 | $this->cache[$rate->getRateType()][$rate->getCurrencyCode()] = $rate; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get NBS's form CSRF token. |
||
190 | * |
||
191 | * @return string CSRF token. |
||
192 | */ |
||
193 | 4 | protected function getFormCsrfToken() |
|
194 | { |
||
195 | 4 | $crawler = new Crawler($this->executeHttpRequest(self::SOURCE, 'GET')); |
|
196 | |||
197 | 2 | $hiddens = $crawler->filter('input[type="hidden"]'); |
|
198 | |||
199 | /** |
||
200 | * @var \DOMElement $hidden |
||
201 | */ |
||
202 | foreach ($hiddens as $hidden) { |
||
203 | |||
204 | if ($hidden->getAttribute('id') === 'com.sun.faces.VIEW') { |
||
205 | return $hidden->getAttribute('value'); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | $message = 'FATAL ERROR: National Bank of Serbia changed it\'s API, unable to extract token.'; |
||
210 | $this->getLogger()->emergency($message); |
||
211 | throw new \RuntimeException($message); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Execute HTTP request and get raw body response. |
||
216 | * |
||
217 | * @param string $url URL to fetch. |
||
218 | * @param string $method HTTP Method. |
||
219 | * @param array $params Params to send with request. |
||
220 | * @return string |
||
221 | */ |
||
222 | 4 | protected function executeHttpRequest($url, $method, array $query = array(), array $params = array()) |
|
223 | { |
||
224 | 4 | $client = $this->getGuzzleClient(); |
|
225 | |||
226 | 4 | $response = $client->request($method, $url, array( |
|
227 | 4 | 'cookies' => $this->getGuzzleCookieJar(), |
|
228 | 4 | 'form_params' => $params, |
|
229 | 2 | 'query' => $query |
|
230 | 2 | )); |
|
231 | |||
232 | 2 | return $response->getBody()->getContents(); |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * Get Guzzle Client. |
||
237 | * |
||
238 | * @return Client |
||
239 | */ |
||
240 | 4 | protected function getGuzzleClient() |
|
248 | |||
249 | /** |
||
250 | * Get Guzzle CookieJar. |
||
251 | * |
||
252 | * @return CookieJar |
||
253 | */ |
||
254 | 4 | protected function getGuzzleCookieJar() |
|
262 | } |
||
263 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.