|
1
|
|
|
<?php namespace Arcanedev\Currencies\Converters; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Contracts\CurrencyConverter; |
|
4
|
|
|
use Arcanedev\Currencies\Contracts\Http\Client as ClientContract; |
|
5
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheContract; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AbstractConverter |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\Currencies\Converters |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractConverter implements CurrencyConverter |
|
15
|
|
|
{ |
|
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
| Properties |
|
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
19
|
|
|
*/ |
|
20
|
|
|
/** |
|
21
|
|
|
* The base URL. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $baseUrl = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Arcanedev\Currencies\Contracts\Http\Client |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $client; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The cache repository. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $cache; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The enabled cache status. |
|
41
|
|
|
* |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $cacheEnabled; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The cache key name. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $cacheKey; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The cache duration. |
|
55
|
|
|
* |
|
56
|
|
|
* @var int |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $cacheDuration; |
|
59
|
|
|
|
|
60
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
61
|
|
|
| Constructor |
|
62
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
63
|
|
|
*/ |
|
64
|
|
|
/** |
|
65
|
|
|
* AbstractConverter constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \Arcanedev\Currencies\Contracts\Http\Client $client |
|
68
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
|
69
|
|
|
* @param array $configs |
|
70
|
|
|
*/ |
|
71
|
24 |
|
public function __construct(ClientContract $client, CacheContract $cache, array $configs = []) |
|
72
|
|
|
{ |
|
73
|
24 |
|
$this->client = $client; |
|
74
|
24 |
|
$this->cache = $cache; |
|
75
|
24 |
|
$this->setCacheConfigs($configs); |
|
76
|
24 |
|
$this->setProviderConfigs($configs); |
|
77
|
24 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
80
|
|
|
| Getters & Setters |
|
81
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
82
|
|
|
*/ |
|
83
|
|
|
/** |
|
84
|
|
|
* Get the base URL. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
12 |
|
public function getBaseUrl() |
|
89
|
|
|
{ |
|
90
|
12 |
|
return $this->baseUrl; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the default currency. |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
12 |
|
public function getDefaultCurrency() |
|
99
|
|
|
{ |
|
100
|
12 |
|
return config('currencies.default', 'USD'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the supported currencies. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
12 |
|
public function getSupportedCurrencies() |
|
109
|
|
|
{ |
|
110
|
12 |
|
return config('currencies.supported', []); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set the cache configs. |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $configs |
|
117
|
|
|
*/ |
|
118
|
24 |
|
protected function setCacheConfigs(array $configs) |
|
119
|
|
|
{ |
|
120
|
24 |
|
$this->cacheEnabled = Arr::get($configs, 'cache.enabled', false); |
|
121
|
24 |
|
$this->cacheKey = Arr::get($configs, 'cache.key', 'currencies.rates'); |
|
122
|
24 |
|
$this->cacheDuration = Arr::get($configs, 'cache.duration', 0); |
|
123
|
24 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set the configs. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $configs |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
*/ |
|
132
|
|
|
abstract protected function setProviderConfigs(array $configs); |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Check if cache is enabled. |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
12 |
|
public function isCacheEnabled() |
|
140
|
|
|
{ |
|
141
|
12 |
|
return (bool) $this->cacheEnabled; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
145
|
|
|
| Main Functions |
|
146
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
147
|
|
|
*/ |
|
148
|
|
|
} |
|
149
|
|
|
|