Completed
Push — master ( 65b0ea...5f2681 )
by Stojan
02:43
created

WebPageSource::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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) 2016 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\Contract\RateInterface;
15
use RunOpenCode\ExchangeRate\Contract\SourceInterface;
16
use RunOpenCode\ExchangeRate\Exception\SourceNotAvailableException;
17
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait;
18
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Api;
19
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Util\BancaIntesaBrowser;
20
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser\HtmlParser;
21
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil;
22
23
/**
24
 * Class WebPageSource
25
 *
26
 * Fetch rates from Banca Intesa website, as public user, without using their API service.
27
 *
28
 * @package RunOpenCode\ExchangeRate\BancaIntesaSerbia\Source
29
 */
30
final class WebPageSource implements SourceInterface
31
{
32
    use LoggerAwareTrait;
33
34
    /**
35
     * @var array
36
     */
37
    private $cache;
38
39
    /**
40
     * @var BancaIntesaBrowser
41
     */
42
    private $browser;
43
44 8
    public function __construct(BancaIntesaBrowser $browser = null)
45
    {
46 8
        $this->browser = ($browser !== null) ? $browser : new BancaIntesaBrowser();
47 8
        $this->cache = array();
48 8
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getName()
54
    {
55
        return Api::NAME;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 8
    public function fetch($currencyCode, $rateType = 'default', \DateTime $date = null)
62
    {
63 8
        $currencyCode = CurrencyCodeUtil::clean($currencyCode);
64
65 8
        if (!Api::supports($currencyCode, $rateType)) {
66 2
            throw new \RuntimeException(sprintf('Banca Intesa Serbia does not supports currency code "%s" for rate type "%s".', $currencyCode, $rateType));
67
        }
68
69 6
        if ($date === null) {
70 6
            $date = new \DateTime('now');
71 6
        }
72
73 6
        if (!array_key_exists($rateType, $this->cache)) {
74
75
            try {
76 6
                $this->load($date);
77
78 6
            } catch (\Exception $e) {
79
                $message = sprintf('Unable to load data from "%s" for "%s" of rate type "%s".', $this->getName(), $currencyCode, $rateType);
80
81
                $this->getLogger()->emergency($message);;
82
                throw new SourceNotAvailableException($message, 0, $e);
83
            }
84 6
        }
85
86 6
        if (array_key_exists($currencyCode, $this->cache[$rateType])) {
87 6
            return $this->cache[$rateType][$currencyCode];
88
        }
89
90
        $message = sprintf('API Changed: source "%s" does not provide currency code "%s" for rate type "%s".', $this->getName(), $currencyCode, $rateType);
91
        $this->getLogger()->critical($message);
92
        throw new \RuntimeException($message);
93
    }
94
95
    /**
96
     * Load rates from Banca Intesa Serbia website.
97
     *
98
     * @param \DateTime $date
99
     * @return RateInterface[]
100
     * @throws SourceNotAvailableException
101
     */
102 6
    private function load(\DateTime $date)
103
    {
104 6
        $parser = new HtmlParser($this->browser->getHtmlDocument($date), $date);
105
106 6
        foreach ($parser->getRates() as $rate) {
107
108 6
            if (!array_key_exists($rate->getRateType(), $this->cache)) {
109 6
                $this->cache[$rate->getRateType()] = array();
110 6
            }
111
112 6
            $this->cache[$rate->getRateType()][$rate->getCurrencyCode()] = $rate;
113 6
        }
114 6
    }
115
}
116