1 | <?php |
||||
2 | |||||
3 | namespace Movavi\Service; |
||||
4 | |||||
5 | use Movavi\Builder\RbcRateBuilder; |
||||
6 | use Movavi\Entity\Currency; |
||||
7 | use Movavi\Entity\Rate; |
||||
8 | use Movavi\Http\ClientInterface; |
||||
9 | |||||
10 | /** |
||||
11 | * Class RbcService |
||||
12 | * |
||||
13 | * Service providing rate data from cash.rbc.ru |
||||
14 | * |
||||
15 | * @package Movavi\Service |
||||
16 | */ |
||||
17 | class RbcService implements CurrencyServiceInterface |
||||
18 | { |
||||
19 | /** |
||||
20 | * Service name |
||||
21 | */ |
||||
22 | const NAME = 'RBC'; |
||||
23 | |||||
24 | /** |
||||
25 | * Service url pattern |
||||
26 | */ |
||||
27 | const URL = 'https://cash.rbc.ru/cash/json/converter_currency_rate/?currency_from=%s¤cy_to=%s&source=cbrf&sum=1&date=%s'; |
||||
28 | |||||
29 | /** |
||||
30 | * Service USD alias that substituting into url |
||||
31 | */ |
||||
32 | const URL_USD = 'USD'; |
||||
33 | |||||
34 | /** |
||||
35 | * Service RUB alias that substituting into url |
||||
36 | */ |
||||
37 | const URL_RUB = 'RUR'; |
||||
38 | |||||
39 | /** |
||||
40 | * Service EUR alias that substituting into url |
||||
41 | */ |
||||
42 | const URL_EUR = 'EUR'; |
||||
43 | |||||
44 | /** |
||||
45 | * Service date format that substituting into url |
||||
46 | */ |
||||
47 | const URL_DATE_FORMAT = 'Y-m-d'; |
||||
48 | |||||
49 | /** |
||||
50 | * Builder for the Rate instances |
||||
51 | * |
||||
52 | * @var RbcRateBuilder|null |
||||
53 | */ |
||||
54 | protected $builder = null; |
||||
55 | |||||
56 | /** |
||||
57 | * Http Client |
||||
58 | * |
||||
59 | * @var ClientInterface|null |
||||
60 | */ |
||||
61 | public $client = null; |
||||
62 | |||||
63 | /** |
||||
64 | * RbcService constructor. |
||||
65 | * |
||||
66 | * @param ClientInterface $client |
||||
67 | * @param RbcRateBuilder $builder |
||||
68 | */ |
||||
69 | public function __construct(ClientInterface $client, RbcRateBuilder $builder) |
||||
70 | { |
||||
71 | $this->client = $client; |
||||
72 | $this->builder = $builder; |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * Returns an HTTP-response by URL given |
||||
77 | * |
||||
78 | * @param $url |
||||
79 | * |
||||
80 | * @return mixed |
||||
81 | */ |
||||
82 | public function sendHttpRequest(string $url): string |
||||
83 | { |
||||
84 | return $this->client->sendHttpRequest($url); |
||||
0 ignored issues
–
show
|
|||||
85 | } |
||||
86 | |||||
87 | /** |
||||
88 | * Returns prepared URL-address |
||||
89 | * |
||||
90 | * @param $currencyFrom |
||||
91 | * @param $currencyTo |
||||
92 | * @param \DateTime $date |
||||
93 | * |
||||
94 | * @return string |
||||
95 | */ |
||||
96 | protected function getUrl(string $currencyFrom, string $currencyTo, \DateTime $date): string |
||||
97 | { |
||||
98 | return sprintf(static::URL, $currencyFrom, $currencyTo, $date->format(static::URL_DATE_FORMAT)); |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * Returns dollar to ruble rate by specified date |
||||
103 | * Implements CurrencyServiceInterface::getUsdToRubRate |
||||
104 | * |
||||
105 | * @param \DateTime $date |
||||
106 | * @return Rate |
||||
107 | * @throws \Movavi\Exception\NoneRateException |
||||
108 | */ |
||||
109 | public function getUsdToRubRate(\DateTime $date): Rate |
||||
110 | { |
||||
111 | |||||
112 | $content = $this->sendHttpRequest($this->getUrl(static::URL_USD, static::URL_RUB, $date)); |
||||
113 | |||||
114 | return $this->builder->fromJson(Currency::USD, Currency::RUB, $date, $content); |
||||
0 ignored issues
–
show
The method
fromJson() 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. ![]() |
|||||
115 | } |
||||
116 | |||||
117 | /** |
||||
118 | * Returns euro to ruble rate by specified date |
||||
119 | * Implements CurrencyServiceInterface::getUsdToRubRate |
||||
120 | * |
||||
121 | * @param \DateTime $date |
||||
122 | * @return Rate |
||||
123 | * @throws \Movavi\Exception\NoneRateException |
||||
124 | */ |
||||
125 | public function getEurToRubRate(\DateTime $date): Rate |
||||
126 | { |
||||
127 | $content = $this->sendHttpRequest($this->getUrl(static::URL_EUR, static::URL_RUB, $date)); |
||||
128 | |||||
129 | return $this->builder->fromJson(Currency::EUR, Currency::RUB, $date, $content); |
||||
130 | } |
||||
131 | } |
||||
132 |
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.