|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* Implementation of exchange rate crawler for Banca Intesa Serbia, http://www.bancaintesa.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\BancaIntesaSerbia\Source; |
|
13
|
|
|
|
|
14
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Enum\RateType; |
|
15
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Exception\RuntimeException; |
|
16
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Exception\SourceNotAvailableException; |
|
17
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
|
18
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourceInterface; |
|
19
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Api; |
|
20
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Util\BancaIntesaBrowser; |
|
21
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser\HtmlParser; |
|
22
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class WebPageSource |
|
26
|
|
|
* |
|
27
|
|
|
* Fetch rates from Banca Intesa website, as public user, without using their API service. |
|
28
|
|
|
* |
|
29
|
|
|
* @package RunOpenCode\ExchangeRate\BancaIntesaSerbia\Source |
|
30
|
|
|
*/ |
|
31
|
|
|
final class WebPageSource implements SourceInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $cache; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var BancaIntesaBrowser |
|
40
|
|
|
*/ |
|
41
|
|
|
private $browser; |
|
42
|
|
|
|
|
43
|
7 |
|
public function __construct(BancaIntesaBrowser $browser = null) |
|
44
|
|
|
{ |
|
45
|
7 |
|
$this->browser = ($browser !== null) ? $browser : new BancaIntesaBrowser(); |
|
46
|
7 |
|
$this->cache = array(); |
|
47
|
7 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function getName() |
|
53
|
|
|
{ |
|
54
|
3 |
|
return Api::NAME; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function fetch($currencyCode, $rateType = RateType::MEDIAN, \DateTime $date = null) |
|
61
|
|
|
{ |
|
62
|
6 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
|
63
|
|
|
|
|
64
|
6 |
|
if (!Api::supports($currencyCode, $rateType)) { |
|
65
|
1 |
|
throw new RuntimeException(sprintf('Banca Intesa Serbia does not supports currency code "%s" for rate type "%s".', $currencyCode, $rateType)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
if ($date === null) { |
|
69
|
5 |
|
$date = new \DateTime('now'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
if (!array_key_exists($rateType, $this->cache)) { |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
5 |
|
$this->load($date); |
|
76
|
|
|
|
|
77
|
1 |
|
} catch (\Exception $e) { |
|
78
|
1 |
|
throw new SourceNotAvailableException(sprintf('Unable to load data from "%s" for "%s" of rate type "%s".', $this->getName(), $currencyCode, $rateType), 0, $e); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
if (array_key_exists($currencyCode, $this->cache[$rateType])) { |
|
83
|
3 |
|
return $this->cache[$rateType][$currencyCode]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
throw new RuntimeException(sprintf('API Changed: source "%s" does not provide currency code "%s" for rate type "%s".', $this->getName(), $currencyCode, $rateType)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Load rates from Banca Intesa Serbia website. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \DateTime $date |
|
93
|
|
|
* @return RateInterface[] |
|
94
|
|
|
* @throws SourceNotAvailableException |
|
95
|
|
|
*/ |
|
96
|
5 |
|
private function load(\DateTime $date) |
|
97
|
|
|
{ |
|
98
|
5 |
|
$parser = new HtmlParser($this->browser->getHtmlDocument($date), $date); |
|
99
|
|
|
|
|
100
|
4 |
|
foreach ($parser->getRates() as $rate) { |
|
101
|
|
|
|
|
102
|
4 |
|
if (!array_key_exists($rate->getRateType(), $this->cache)) { |
|
103
|
4 |
|
$this->cache[$rate->getRateType()] = array(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
$this->cache[$rate->getRateType()][$rate->getCurrencyCode()] = $rate; |
|
107
|
|
|
} |
|
108
|
4 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|