1
|
|
|
<?php namespace Arcanedev\Currencies\Services; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Exceptions\ApiException; |
4
|
|
|
use Illuminate\Support\Arr; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class OpenExchangeRatesService |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Currencies\Services |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class OpenExchangeRatesService extends AbstractApiService |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The base URL. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $baseUrl = 'http://openexchangerates.org/api'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The API ID. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $apiId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Pro Plan. |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $proPlan = false; |
38
|
|
|
|
39
|
|
|
/* ------------------------------------------------------------------------------------------------ |
40
|
|
|
| Getters & Setters |
41
|
|
|
| ------------------------------------------------------------------------------------------------ |
42
|
|
|
*/ |
43
|
|
|
/** |
44
|
|
|
* Set the configs. |
45
|
|
|
* |
46
|
|
|
* @param array $configs |
47
|
|
|
*/ |
48
|
60 |
|
protected function setConfigs(array $configs) |
49
|
|
|
{ |
50
|
60 |
|
$this->apiId = Arr::get($configs, 'api-id'); |
51
|
60 |
|
$this->proPlan = Arr::get($configs, 'pro-plan', false); |
52
|
60 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the API ID. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
* |
59
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\ApiException |
60
|
|
|
*/ |
61
|
24 |
|
private function getAppId() |
62
|
|
|
{ |
63
|
24 |
|
if ( ! $this->apiId) { |
64
|
|
|
throw new ApiException('OpenExchangeRates.org requires an app key.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
24 |
|
return $this->apiId; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get cache key. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getCacheKey() |
76
|
|
|
{ |
77
|
|
|
return parent::getCacheKey() . '.openexchangerates'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* ------------------------------------------------------------------------------------------------ |
81
|
|
|
| Main Functions |
82
|
|
|
| ------------------------------------------------------------------------------------------------ |
83
|
|
|
*/ |
84
|
|
|
/** |
85
|
|
|
* Make an API request. |
86
|
|
|
* |
87
|
|
|
* @param string $from |
88
|
|
|
* @param array $to |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
* |
92
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\ApiException |
93
|
|
|
*/ |
94
|
24 |
|
protected function request($from, array $to = []) |
95
|
|
|
{ |
96
|
24 |
|
$to = ( ! empty($to) && $this->proPlan) ? implode(',', $to) : null; |
97
|
|
|
|
98
|
24 |
|
$response = $this->client->get('/latest.json', [ |
99
|
24 |
|
'app_id' => $this->getAppId(), |
100
|
24 |
|
'base' => $from, |
101
|
24 |
|
'symbols' => $to, |
102
|
18 |
|
]); |
103
|
|
|
|
104
|
24 |
|
$data = json_decode($response, true); |
105
|
|
|
|
106
|
24 |
|
return $data['rates']; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|