1 | <?php |
||||
2 | |||||
3 | namespace Movavi\Service; |
||||
4 | |||||
5 | use Movavi\Builder\CbrRateBuilder; |
||||
6 | use Movavi\Entity\Rate; |
||||
7 | use Movavi\Entity\Currency; |
||||
8 | use Movavi\Http\ClientInterface; |
||||
9 | |||||
10 | /** |
||||
11 | * Class CbrService |
||||
12 | * |
||||
13 | * Service providing rate data from www.cbr.ru |
||||
14 | * |
||||
15 | * @package Movavi\Service |
||||
16 | */ |
||||
17 | class CbrService implements CurrencyServiceInterface |
||||
18 | { |
||||
19 | /** |
||||
20 | * Service name |
||||
21 | */ |
||||
22 | const NAME = 'CBR'; |
||||
23 | |||||
24 | /** |
||||
25 | * Service url pattern |
||||
26 | */ |
||||
27 | const URL = 'http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1=%s&date_req2=%s&VAL_NM_RQ=%s'; |
||||
28 | |||||
29 | /** |
||||
30 | * Service USD alias that substituting into url |
||||
31 | */ |
||||
32 | const URL_USD = 'R01235'; |
||||
33 | |||||
34 | /** |
||||
35 | * Service EUR alias that substituting into url |
||||
36 | */ |
||||
37 | const URL_EUR = 'R01239'; |
||||
38 | |||||
39 | /** |
||||
40 | * Service date format that substituting into url |
||||
41 | */ |
||||
42 | const URL_DATE_FORMAT = 'd/m/Y'; |
||||
43 | |||||
44 | /** |
||||
45 | * Builder for the Rate instances |
||||
46 | * |
||||
47 | * @var CbrRateBuilder|null |
||||
48 | */ |
||||
49 | public $builder = null; |
||||
50 | |||||
51 | /** |
||||
52 | * Http Client |
||||
53 | * |
||||
54 | * @var ClientInterface|null |
||||
55 | */ |
||||
56 | public $client = null; |
||||
57 | |||||
58 | /** |
||||
59 | * CbrService constructor. |
||||
60 | * |
||||
61 | * @param ClientInterface $client |
||||
62 | * @param CbrRateBuilder $builder |
||||
63 | */ |
||||
64 | public function __construct(ClientInterface $client, CbrRateBuilder $builder) |
||||
65 | { |
||||
66 | $this->client = $client; |
||||
67 | $this->builder = $builder; |
||||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * Returns an HTTP-response by URL given |
||||
72 | * |
||||
73 | * @param $url |
||||
74 | * |
||||
75 | * @return mixed |
||||
76 | */ |
||||
77 | public function sendHttpRequest(string $url): string |
||||
78 | { |
||||
79 | return $this->client->sendHttpRequest($url); |
||||
0 ignored issues
–
show
|
|||||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * Returns prepared URL-address |
||||
84 | * |
||||
85 | * @param $currencyFrom |
||||
86 | * @param \DateTime $date |
||||
87 | * |
||||
88 | * @return string |
||||
89 | */ |
||||
90 | protected function getUrl(string $currencyFrom, \DateTime $date): string |
||||
91 | { |
||||
92 | |||||
93 | $strDateTo = $date->format(static::URL_DATE_FORMAT); |
||||
94 | $strDateFrom = $date->sub(\DateInterval::createFromDateString('1 day'))->format(static::URL_DATE_FORMAT); |
||||
95 | |||||
96 | return sprintf( |
||||
97 | static::URL, |
||||
98 | $strDateFrom, |
||||
99 | $strDateTo, |
||||
100 | $currencyFrom |
||||
101 | ); |
||||
102 | } |
||||
103 | |||||
104 | /** |
||||
105 | * Returns dollar to ruble rate by specified date |
||||
106 | * Implements CurrencyServiceInterface::getUsdToRubRate |
||||
107 | * |
||||
108 | * @param \DateTime $date |
||||
109 | * |
||||
110 | * @return Rate |
||||
111 | * |
||||
112 | * @throws \Movavi\Exception\NoneRateException |
||||
113 | */ |
||||
114 | public function getUsdToRubRate(\DateTime $date): Rate |
||||
115 | { |
||||
116 | $url = $this->getUrl(static::URL_USD, $date); |
||||
117 | $content = $this->sendHttpRequest($url); |
||||
118 | |||||
119 | return $this->builder->fromXml(Currency::USD, Currency::RUB, $date, $content); |
||||
0 ignored issues
–
show
The method
fromXml() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Returns euro to ruble rate by specified date |
||||
124 | * Implements CurrencyServiceInterface::getUsdToRubRate |
||||
125 | * |
||||
126 | * @param \DateTime $date |
||||
127 | * |
||||
128 | * @return Rate |
||||
129 | * |
||||
130 | * @throws \Movavi\Exception\NoneRateException |
||||
131 | */ |
||||
132 | public function getEurToRubRate(\DateTime $date): Rate |
||||
133 | { |
||||
134 | $url = $this->getUrl(static::URL_EUR, $date); |
||||
135 | $content = $this->sendHttpRequest($url); |
||||
136 | |||||
137 | return $this->builder->fromXml(Currency::USD, Currency::RUB, $date, $content); |
||||
138 | } |
||||
139 | } |
||||
140 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.