Test Setup Failed
Push — master ( d1af2b...414f7e )
by Andrey
03:32
created

ExchangeRatesProvider::getRatesExchangesDynamic()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 35
rs 6.7272
cc 7
eloc 19
nc 15
nop 3
1
<?php
2
/**
3
 * (c) itmedia.by <[email protected]>
4
 */
5
6
namespace Submarine\NbrbExchangeRatesBundle\Provider;
7
8
use Submarine\NbrbExchangeRatesBundle\Client\ApiClient;
9
use Submarine\NbrbExchangeRatesBundle\CurrencyRateDate;
10
use Submarine\NbrbExchangeRatesBundle\Exception\UndefinedCurrencyException;
11
use Submarine\NbrbExchangeRatesBundle\ExchangeRate;
12
13
/**
14
 * Class ExchangeRatesProvider
15
 * @package Submarine\NbrbParserBundle
16
 */
17
class ExchangeRatesProvider implements ExchangeRatesProviderInterface
18
{
19
20
    /**
21
     * Наименование валюты, содержащее номинал
22
     * @var bool
23
     */
24
    private $scaledName = true;
25
26
    /**
27
     * Выкидывать исключения?
28
     * @var bool
29
     */
30
    private $showExceptions = true;
31
32
33
    /**
34
     * @var ApiClient
35
     */
36
    private $apiClient;
37
38
    /**
39
     * ExchangeRatesProvider constructor.
40
     * @param ApiClient $apiClient
41
     * @param bool $showExceptions
42
     * @param bool $scaledName
43
     */
44
    public function __construct(ApiClient $apiClient, $showExceptions, $scaledName)
45
    {
46
        $this->apiClient = $apiClient;
47
        $this->showExceptions = $showExceptions;
48
        $this->scaledName = $scaledName;
49
    }
50
51
52
    /**
53
     * Все курсы валют за указанную дату
54
     *
55
     * @param \DateTime $date Если null - текущая дата
56
     *
57
     * @return ExchangeRate[]
58
     */
59
    public function getAllRatesExchanges(\DateTime $date = null)
60
    {
61
        if ($date === null) {
62
            $date = new \DateTime();
63
        }
64
65
        try {
66
            $body = $this->apiClient->getXmlExchangesRates($date, $this->scaledName);
67
            $xml = simplexml_load_string($body);
68
69
            $result = [];
70
71
            if (count($xml->Currency)) {
72
                foreach ($xml->Currency as $item) {
73
                    $exRate = new ExchangeRate($item, $date);
74
                    $result[$exRate->getCharCode()] = $exRate;
75
                }
76
            }
77
78
            return $result;
79
80
        } catch (\Exception $exc) {
81
            if ($this->showExceptions) {
82
                throw new $exc;
83
            }
84
85
            return [];
86
        }
87
    }
88
89
90
    /**
91
     * Курсы выбранных валют за указанную дату
92
     *
93
     * @param array $codes Коды валют в формате ISO (USD, UAH)
94
     * @param \DateTime $date Дата, по умолчанию текущая дата
95
     *
96
     * @throws UndefinedCurrencyException
97
     * @return ExchangeRate[]
98
     */
99
    public function getRatesExchanges(array $codes, \DateTime $date = null)
100
    {
101
        $rates = $this->getAllRatesExchanges($date);
102
        $result = [];
103
        foreach ($codes as $code) {
104
            if (isset($rates[$code])) {
105
                $result[$code] = $rates[$code];
106
            } elseif ($this->showExceptions) {
107
                throw new UndefinedCurrencyException(sprintf('Undefined currency code: %s', $code));
108
            }
109
        }
110
111
        return $result;
112
    }
113
114
115
    /**
116
     * Курс валюты за указанную дату
117
     *
118
     * @param string $code Код валюты в формате ISO (USD, UAH)
119
     * @param \DateTime $date Если null - текущая дата
120
     *
121
     * @throws UndefinedCurrencyException
122
     * @return ExchangeRate
123
     */
124
    public function getRateExchange($code, \DateTime $date = null)
125
    {
126
        $rates = $this->getAllRatesExchanges($date);
127
        if (isset($rates[$code])) {
128
            return $rates[$code];
129
        }
130
131
        if ($this->showExceptions) {
132
            throw new UndefinedCurrencyException(sprintf('Undefined currency code: %s', $code));
133
        }
134
135
        return new ExchangeRate();
136
    }
137
138
139
    /**
140
     * @param string $code
141
     * @param \DateTime $firstDate
142
     * @param \DateTime $lastDate
143
     *
144
     * @throws UndefinedCurrencyException
145
     * @return CurrencyRateDate[]
146
     */
147
    public function getRatesExchangesDynamic($code, \DateTime $firstDate, \DateTime $lastDate)
148
    {
149
        $currency = $this->getRateExchange($code);
150
151
        if (!$currency->getId()) {
152
153
            if ($this->showExceptions) {
154
                throw new UndefinedCurrencyException(sprintf('Undefined currency id: %s', $code));
155
            }
156
157
            return [];
158
        }
159
160
        try {
161
            $body = $this->apiClient->getXmlExchangesRatesDynamic($currency->getId(), $firstDate, $lastDate);
162
            $xml = simplexml_load_string($body);
163
164
            $result = [];
165
            if (count($xml->Record)) {
166
                foreach ($xml->Record as $item) {
167
                    $rate = new CurrencyRateDate($item);
168
                    $result[] = $rate;
169
                }
170
            }
171
172
            return $result;
173
174
        } catch (\Exception $exc) {
175
            if ($this->showExceptions) {
176
                throw new $exc;
177
            }
178
179
            return [];
180
        }
181
    }
182
183
}