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 AbstractService |
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
|
24 |
|
protected function setProviderConfigs(array $configs) |
49
|
|
|
{ |
50
|
24 |
|
$this->apiId = Arr::get($configs, 'api-id'); |
51
|
24 |
|
$this->proPlan = Arr::get($configs, 'pro-plan', false); |
52
|
24 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the API ID. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
* |
59
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\ApiException |
60
|
|
|
*/ |
61
|
12 |
|
private function getAppId() |
62
|
|
|
{ |
63
|
12 |
|
if ( ! $this->apiId) { |
64
|
|
|
throw new ApiException('OpenExchangeRates.org requires an app key.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
return $this->apiId; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/* ------------------------------------------------------------------------------------------------ |
71
|
|
|
| Main Functions |
72
|
|
|
| ------------------------------------------------------------------------------------------------ |
73
|
|
|
*/ |
74
|
|
|
/** |
75
|
|
|
* Make an API request. |
76
|
|
|
* |
77
|
|
|
* @param string $from |
78
|
|
|
* @param array $to |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* |
82
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\ApiException |
83
|
|
|
*/ |
84
|
12 |
|
protected function request($from, array $to) |
85
|
|
|
{ |
86
|
12 |
|
$response = $this->client->get('/latest.json', [ |
87
|
12 |
|
'app_id' => $this->getAppId(), |
88
|
12 |
|
'base' => $from, |
89
|
12 |
|
'symbols' => $this->proPlan ? implode(',', $to) : null, |
90
|
9 |
|
]); |
91
|
|
|
|
92
|
12 |
|
$data = json_decode($response, true); |
93
|
|
|
|
94
|
12 |
|
return $data['rates']; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|