1 | <?php namespace Arcanedev\Currencies\Services; |
||
18 | abstract class AbstractService implements CurrencyService |
||
19 | { |
||
20 | /* ------------------------------------------------------------------------------------------------ |
||
21 | | Properties |
||
22 | | ------------------------------------------------------------------------------------------------ |
||
23 | */ |
||
24 | /** |
||
25 | * The base URL. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $baseUrl = ''; |
||
30 | |||
31 | /** |
||
32 | * @var \Arcanedev\Currencies\Contracts\CurrencyManager |
||
33 | */ |
||
34 | protected $manager; |
||
35 | |||
36 | /** |
||
37 | * @var \Arcanedev\Currencies\Contracts\Http\Client |
||
38 | */ |
||
39 | protected $client; |
||
40 | |||
41 | /** |
||
42 | * The cache repository. |
||
43 | * |
||
44 | * @var \Illuminate\Contracts\Cache\Repository |
||
45 | */ |
||
46 | protected $cache; |
||
47 | |||
48 | /** |
||
49 | * The enabled cache status. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $cacheEnabled; |
||
54 | |||
55 | /** |
||
56 | * The cache key name. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $cacheKey; |
||
61 | |||
62 | /** |
||
63 | * The cache duration. |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $cacheDuration; |
||
68 | |||
69 | /* ------------------------------------------------------------------------------------------------ |
||
70 | | Constructor |
||
71 | | ------------------------------------------------------------------------------------------------ |
||
72 | */ |
||
73 | /** |
||
74 | * AbstractService constructor. |
||
75 | * |
||
76 | * @param \Arcanedev\Currencies\Contracts\CurrencyManager $manager |
||
77 | * @param \Arcanedev\Currencies\Contracts\Http\Client $client |
||
78 | * @param \Illuminate\Contracts\Cache\Repository $cache |
||
79 | * @param array $configs |
||
80 | */ |
||
81 | 60 | public function __construct( |
|
95 | |||
96 | /* ------------------------------------------------------------------------------------------------ |
||
97 | | Getters & Setters |
||
98 | | ------------------------------------------------------------------------------------------------ |
||
99 | */ |
||
100 | /** |
||
101 | * Get the default currency. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 24 | public function getDefault() |
|
109 | |||
110 | /** |
||
111 | * Get the supported currencies. |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 12 | public function getSupported() |
|
119 | |||
120 | /** |
||
121 | * Get the `from` currency. |
||
122 | * |
||
123 | * @param string|null $from |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 24 | protected function getFromCurrency($from = null) |
|
135 | |||
136 | /** |
||
137 | * Get the `to` currencies. |
||
138 | * |
||
139 | * @param array|string|null $to |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function getToCurrencies($to = null) |
||
144 | { |
||
145 | if (is_null($to)) { |
||
146 | return array_diff( |
||
147 | $this->getSupported(), |
||
148 | [$this->getDefault()] |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | return $to; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Set the cache configs. |
||
157 | * |
||
158 | * @param array $configs |
||
159 | */ |
||
160 | 60 | protected function setCacheConfigs(array $configs) |
|
166 | |||
167 | /** |
||
168 | * Set the configs. |
||
169 | * |
||
170 | * @param array $configs |
||
171 | */ |
||
172 | abstract protected function setProviderConfigs(array $configs); |
||
173 | |||
174 | /** |
||
175 | * Get cache key. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | protected function getCacheKey() |
||
183 | |||
184 | /** |
||
185 | * Check if cache is enabled. |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 24 | protected function isCacheEnabled() |
|
193 | |||
194 | /* ------------------------------------------------------------------------------------------------ |
||
195 | | Main Functions |
||
196 | | ------------------------------------------------------------------------------------------------ |
||
197 | */ |
||
198 | /** |
||
199 | * Get currencies rates. |
||
200 | * |
||
201 | * @return \Arcanedev\Currencies\Entities\RateCollection |
||
202 | */ |
||
203 | 24 | public function rates() |
|
217 | |||
218 | /** |
||
219 | * Get supported currencies rates. |
||
220 | * |
||
221 | * @return \Arcanedev\Currencies\Entities\RateCollection |
||
222 | */ |
||
223 | 12 | public function supportedRates() |
|
233 | |||
234 | /* ------------------------------------------------------------------------------------------------ |
||
235 | | Other Functions |
||
236 | | ------------------------------------------------------------------------------------------------ |
||
237 | */ |
||
238 | /** |
||
239 | * Make an API request. |
||
240 | * |
||
241 | * @param string $from |
||
242 | * @param array $to |
||
243 | * |
||
244 | * @return \Arcanedev\Currencies\Entities\RateCollection |
||
245 | */ |
||
246 | abstract protected function request($from, array $to = []); |
||
247 | |||
248 | /** |
||
249 | * Cache results. |
||
250 | * |
||
251 | * @param Closure $callback |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | private function cacheResults(Closure $callback) |
||
263 | |||
264 | /** |
||
265 | * Prepare rates collection. |
||
266 | * |
||
267 | * @param string $from |
||
268 | * @param array $rates |
||
269 | * |
||
270 | * @return \Arcanedev\Currencies\Entities\RateCollection |
||
271 | */ |
||
272 | 24 | protected function prepareRates($from, array $rates) |
|
276 | } |
||
277 |