1
|
|
|
<?php namespace Arcanedev\Currencies\Services; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Contracts\Http\Client as ClientContract; |
4
|
|
|
use Arcanedev\Currencies\Contracts\Services\CurrencyService; |
5
|
|
|
use Arcanedev\Currencies\Entities\RateCollection; |
6
|
|
|
use Closure; |
7
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheContract; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractService |
12
|
|
|
* |
13
|
|
|
* @package Arcanedev\Currencies\Services |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractService implements CurrencyService |
17
|
|
|
{ |
18
|
|
|
/* ------------------------------------------------------------------------------------------------ |
19
|
|
|
| Properties |
20
|
|
|
| ------------------------------------------------------------------------------------------------ |
21
|
|
|
*/ |
22
|
|
|
/** |
23
|
|
|
* The base URL. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $baseUrl = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Arcanedev\Currencies\Contracts\Http\Client |
31
|
|
|
*/ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The cache repository. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
38
|
|
|
*/ |
39
|
|
|
protected $cache; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The enabled cache status. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $cacheEnabled; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The cache key name. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $cacheKey; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The cache duration. |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $cacheDuration; |
61
|
|
|
|
62
|
|
|
/* ------------------------------------------------------------------------------------------------ |
63
|
|
|
| Constructor |
64
|
|
|
| ------------------------------------------------------------------------------------------------ |
65
|
|
|
*/ |
66
|
|
|
/** |
67
|
|
|
* AbstractService constructor. |
68
|
|
|
* |
69
|
|
|
* @param \Arcanedev\Currencies\Contracts\Http\Client $client |
70
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
71
|
|
|
* @param array $configs |
72
|
|
|
*/ |
73
|
24 |
|
public function __construct(ClientContract $client, CacheContract $cache, array $configs = []) |
74
|
|
|
{ |
75
|
24 |
|
$this->client = $client; |
76
|
24 |
|
$this->cache = $cache; |
77
|
24 |
|
$this->setCacheConfigs($configs); |
78
|
24 |
|
$this->setProviderConfigs($configs); |
79
|
|
|
|
80
|
24 |
|
$this->client->setBaseUrl($this->baseUrl); |
81
|
24 |
|
} |
82
|
|
|
|
83
|
|
|
/* ------------------------------------------------------------------------------------------------ |
84
|
|
|
| Getters & Setters |
85
|
|
|
| ------------------------------------------------------------------------------------------------ |
86
|
|
|
*/ |
87
|
|
|
/** |
88
|
|
|
* Get the default currency. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
12 |
|
public function getDefaultCurrency() |
93
|
|
|
{ |
94
|
12 |
|
return config('currencies.default', 'USD'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the supported currencies. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
12 |
|
public function getSupportedCurrencies() |
103
|
|
|
{ |
104
|
12 |
|
return config('currencies.supported', []); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the `from` currency. |
109
|
|
|
* |
110
|
|
|
* @param string $from |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
12 |
|
protected function getFromCurrency($from) |
115
|
|
|
{ |
116
|
12 |
|
if (is_null($from)) { |
117
|
12 |
|
$from = $this->getDefaultCurrency(); |
118
|
9 |
|
} |
119
|
|
|
|
120
|
12 |
|
return $from; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the `to` currencies. |
125
|
|
|
* |
126
|
|
|
* @param array|string|null $to |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
12 |
|
protected function getToCurrencies($to) |
131
|
|
|
{ |
132
|
12 |
|
if (is_null($to)) { |
133
|
12 |
|
return array_diff( |
134
|
12 |
|
$this->getSupportedCurrencies(), |
135
|
12 |
|
[$this->getDefaultCurrency()] |
136
|
9 |
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $to; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the cache configs. |
144
|
|
|
* |
145
|
|
|
* @param array $configs |
146
|
|
|
*/ |
147
|
24 |
|
protected function setCacheConfigs(array $configs) |
148
|
|
|
{ |
149
|
24 |
|
$this->cacheEnabled = Arr::get($configs, 'cache.enabled', false); |
150
|
24 |
|
$this->cacheKey = Arr::get($configs, 'cache.key', 'currencies.rates'); |
151
|
24 |
|
$this->cacheDuration = Arr::get($configs, 'cache.duration', 0); |
152
|
24 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the configs. |
156
|
|
|
* |
157
|
|
|
* @param array $configs |
158
|
|
|
*/ |
159
|
|
|
abstract protected function setProviderConfigs(array $configs); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get cache key. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function getCacheKey() |
167
|
|
|
{ |
168
|
|
|
return $this->cacheKey; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check if cache is enabled. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
12 |
|
public function isCacheEnabled() |
177
|
|
|
{ |
178
|
12 |
|
return (bool) $this->cacheEnabled; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/* ------------------------------------------------------------------------------------------------ |
182
|
|
|
| Main Functions |
183
|
|
|
| ------------------------------------------------------------------------------------------------ |
184
|
|
|
*/ |
185
|
|
|
/** |
186
|
|
|
* Get currencies rates. |
187
|
|
|
* |
188
|
|
|
* @param string|null $from |
189
|
|
|
* @param array|string|null $to |
190
|
|
|
* |
191
|
|
|
* @return \Arcanedev\Currencies\Entities\RateCollection |
192
|
|
|
*/ |
193
|
12 |
|
public function rates($from = null, $to = null) |
194
|
|
|
{ |
195
|
12 |
|
$from = $this->getFromCurrency($from); |
196
|
12 |
|
$to = $this->getToCurrencies($to); |
197
|
|
|
|
198
|
12 |
|
$self = $this; |
199
|
12 |
|
$callback = function () use ($self, $from, $to) { |
200
|
12 |
|
return $self->request($from, $to); |
|
|
|
|
201
|
12 |
|
}; |
202
|
|
|
|
203
|
12 |
|
$rates = $this->isCacheEnabled() |
204
|
9 |
|
? $this->cacheResults($callback) |
205
|
12 |
|
: call_user_func($callback); |
206
|
|
|
|
207
|
12 |
|
return $this->prepareRates($from, $rates); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Make an API request. |
212
|
|
|
* |
213
|
|
|
* @param string $from |
214
|
|
|
* @param array $to |
215
|
|
|
* |
216
|
|
|
* @return \Arcanedev\Currencies\Entities\RateCollection |
217
|
|
|
*/ |
218
|
|
|
abstract protected function request($from, array $to); |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Cache results. |
222
|
|
|
* |
223
|
|
|
* @param Closure $callback |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
private function cacheResults(Closure $callback) |
228
|
|
|
{ |
229
|
|
|
return $this->cache->remember( |
230
|
|
|
$this->getCacheKey(), |
231
|
|
|
$this->cacheDuration, |
232
|
|
|
$callback |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Prepare rates collection. |
238
|
|
|
* |
239
|
|
|
* @param string $from |
240
|
|
|
* @param array $rates |
241
|
|
|
* |
242
|
|
|
* @return \Arcanedev\Currencies\Entities\RateCollection |
243
|
|
|
*/ |
244
|
12 |
|
protected function prepareRates($from, array $rates) |
245
|
|
|
{ |
246
|
12 |
|
return RateCollection::make()->load($from, $rates); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.