Completed
Pull Request — master (#2)
by ARCANEDEV
02:44
created

OpenExchangeRatesConverter::getData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 178
Code Lines 174

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 178
rs 8.2857
cc 1
eloc 174
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Arcanedev\Currencies\Converters;
2
use Arcanedev\Currencies\Entities\RateCollection;
3
use Illuminate\Support\Arr;
4
5
/**
6
 * Class     OpenExchangeRatesConverter
7
 *
8
 * @package  Arcanedev\Currencies\Converters
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class OpenExchangeRatesConverter extends AbstractConverter
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * The base URL.
19
     *
20
     * @var string
21
     */
22
    protected $baseUrl = 'http://openexchangerates.org/api';
23
24
    /**
25
     * The API ID.
26
     *
27
     * @var string
28
     */
29
    protected $apiId;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $proPlan;
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Getters & Setters
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Set the configs.
42
     *
43
     * @param  array $configs
44
     *
45
     * @return mixed
46
     */
47
    protected function setProviderConfigs(array $configs)
48
    {
49
        $this->apiId   = Arr::get($configs, 'api-id');
50
        $this->proPlan = Arr::get($configs, 'pro-plan', false);
51
    }
52
53
    /**
54
     * Get the API ID.
55
     *
56
     * @return string
57
     *
58
     * @throws \Exception
59
     */
60
    private function getAppId()
61
    {
62
        if ( ! $this->apiId) {
63
            throw new \Exception('OpenExchangeRates.org requires an app key.');
64
        }
65
66
        return $this->apiId;
67
    }
68
69
    private function getFromCurrency($from)
70
    {
71
        if (is_null($from)) {
72
            $from = $this->getDefaultCurrency();
73
        }
74
75
        return $from;
76
    }
77
78
    private function getToCurrencies($to)
79
    {
80
        if (is_null($to)) {
81
            return array_diff(
82
                $this->getSupportedCurrencies(),
83
                [$this->getDefaultCurrency()]
84
            );
85
        }
86
87
        return $to;
88
    }
89
90
    /* ------------------------------------------------------------------------------------------------
91
     |  Main Functions
92
     | ------------------------------------------------------------------------------------------------
93
     */
94
    /**
95
     * Get currencies rates.
96
     *
97
     * @param  string|null        $from
98
     * @param  array|string|null  $to
99
     *
100
     * @return RateCollection
101
     */
102
    public function rates($from = null, $to = null)
103
    {
104
        $from = $this->getFromCurrency($from);
105
        $to   = $this->getToCurrencies($to);
106
107
        $self     = $this;
108
        $callback = function () use ($self, $from, $to) {
109
            return $self->request($from, $to);
110
        };
111
112
        return $this->isCacheEnabled()
113
            ? $this->cache->remember($this->cacheKey, $this->cacheDuration, $callback)
114
            : call_user_func($callback);
115
    }
116
117
    /**
118
     * Make an API request.
119
     *
120
     * @param  string  $from
121
     * @param  array   $to
122
     *
123
     * @return RateCollection
124
     *
125
     * @throws \Exception
126
     */
127
    protected function request($from, array $to)
128
    {
129
        $params = array_filter([
130
            'app_id'  => $this->getAppId(),
131
            'base'    => $from,
132
            // TODO: Add a config parameter to limit results to specific currencies (Pro Plan)
133
            'symbols' => $this->proPlan ? implode(',', $to) : null,
134
        ]);
135
136
        $response = $this->client->get($this->getBaseUrl() . "/latest.json?".http_build_query($params));
137
        $data     = json_decode($response, true);
138
139
        return $this->prepareRates($from, $data['rates']);
140
    }
141
142
    /**
143
     * Prepare rates collection.
144
     *
145
     * @param  string  $from
146
     * @param  array   $rates
147
     *
148
     * @return RateCollection
149
     */
150
    protected function prepareRates($from, array $rates)
151
    {
152
        return RateCollection::make()->load($from, $rates);
153
    }
154
155
    private function getData()
156
    {
157
        return [
158
            "rates" => [
159
                "AED" => 3.673028,
160
                "AFN" => 68.87,
161
                "ALL" => 121.7401,
162
                "AMD" => 476.265002,
163
                "ANG" => 1.7888,
164
                "AOA" => 165.893666,
165
                "ARS" => 14.17537,
166
                "AUD" => 1.324254,
167
                "AWG" => 1.793333,
168
                "AZN" => 1.538675,
169
                "BAM" => 1.728991,
170
                "BBD" => 2,
171
                "BDT" => 78.56374,
172
                "BGN" => 1.731065,
173
                "BHD" => 0.377138,
174
                "BIF" => 1675.622476,
175
                "BMD" => 1,
176
                "BND" => 1.345749,
177
                "BOB" => 6.912068,
178
                "BRL" => 3.354186,
179
                "BSD" => 1,
180
                "BTC" => 0.001591946027,
181
                "BTN" => 67.380467,
182
                "BWP" => 10.735563,
183
                "BYR" => 19820.15,
184
                "BZD" => 2.016625,
185
                "CAD" => 1.282715,
186
                "CDF" => 960.29325,
187
                "CHF" => 0.960247,
188
                "CLF" => 0.024602,
189
                "CLP" => 675.486304,
190
                "CNY" => 6.580977,
191
                "COP" => 2938.686696,
192
                "CRC" => 545.6316,
193
                "CUC" => 1,
194
                "CUP" => 24.728383,
195
                "CVE" => 97.620266,
196
                "CZK" => 23.96213,
197
                "DJF" => 177.545001,
198
                "DKK" => 6.584764,
199
                "DOP" => 46.017,
200
                "DZD" => 109.779599,
201
                "EEK" => 13.85685,
202
                "EGP" => 8.877957,
203
                "ERN" => 15.0015,
204
                "ETB" => 21.82536,
205
                "EUR" => 0.884403,
206
                "FJD" => 2.061383,
207
                "FKP" => 0.686404,
208
                "GBP" => 0.686404,
209
                "GEL" => 2.22886,
210
                "GGP" => 0.686404,
211
                "GHS" => 3.964002,
212
                "GIP" => 0.686404,
213
                "GMD" => 42.87454,
214
                "GNF" => 8446.124903,
215
                "GTQ" => 7.655101,
216
                "GYD" => 206.958836,
217
                "HKD" => 7.757641,
218
                "HNL" => 22.80163,
219
                "HRK" => 6.65668,
220
                "HTG" => 63.063975,
221
                "HUF" => 278.2943,
222
                "IDR" => 13232.75,
223
                "ILS" => 3.837856,
224
                "IMP" => 0.686404,
225
                "INR" => 67.33164,
226
                "IQD" => 1164.016292,
227
                "IRR" => 30362,
228
                "ISK" => 122.049,
229
                "JEP" => 0.686404,
230
                "JMD" => 125.881401,
231
                "JOD" => 0.708428,
232
                "JPY" => 105.027201,
233
                "KES" => 101.16445,
234
                "KGS" => 67.485249,
235
                "KHR" => 4089.4975,
236
                "KMF" => 431.33453,
237
                "KPW" => 900.09,
238
                "KRW" => 1151.471675,
239
                "KWD" => 0.300892,
240
                "KYD" => 0.82636,
241
                "KZT" => 336.662492,
242
                "LAK" => 8126.295049,
243
                "LBP" => 1512.273333,
244
                "LKR" => 147.028899,
245
                "LRD" => 90.50905,
246
                "LSL" => 14.58124,
247
                "LTL" => 3.029371,
248
                "LVL" => 0.621793,
249
                "LYD" => 1.366095,
250
                "MAD" => 9.675664,
251
                "MDL" => 19.73746,
252
                "MGA" => 3291.886667,
253
                "MKD" => 54.44427,
254
                "MMK" => 1190.839988,
255
                "MNT" => 1941.666667,
256
                "MOP" => 8.011656,
257
                "MRO" => 358.109166,
258
                "MTL" => 0.683738,
259
                "MUR" => 35.308163,
260
                "MVR" => 15.316667,
261
                "MWK" => 711.800202,
262
                "MXN" => 18.44083,
263
                "MYR" => 4.007186,
264
                "MZN" => 61.97,
265
                "NAD" => 14.58124,
266
                "NGN" => 283.8604,
267
                "NIO" => 28.64246,
268
                "NOK" => 8.249358,
269
                "NPR" => 108.0018,
270
                "NZD" => 1.386183,
271
                "OMR" => 0.385017,
272
                "PAB" => 1,
273
                "PEN" => 3.292465,
274
                "PGK" => 3.142375,
275
                "PHP" => 46.5031,
276
                "PKR" => 104.950899,
277
                "PLN" => 3.879402,
278
                "PYG" => 5669.505,
279
                "QAR" => 3.640597,
280
                "RON" => 3.992156,
281
                "RSD" => 109.626579,
282
                "RUB" => 63.98843,
283
                "RWF" => 796.445754,
284
                "SAR" => 3.750658,
285
                "SBD" => 7.79857,
286
                "SCR" => 13.1317,
287
                "SDG" => 6.10241,
288
                "SEK" => 8.249164,
289
                "SGD" => 1.342843,
290
                "SHP" => 0.686404,
291
                "SLL" => 3941.6058,
292
                "SOS" => 587.315494,
293
                "SRD" => 7.006,
294
                "STD" => 21712,
295
                "SVC" => 8.772953,
296
                "SYP" => 218.912332,
297
                "SZL" => 14.58414,
298
                "THB" => 35.23901,
299
                "TJS" => 7.8685,
300
                "TMT" => 3.5016,
301
                "TND" => 2.156606,
302
                "TOP" => 2.220539,
303
                "TRY" => 2.877363,
304
                "TTD" => 6.66551,
305
                "TWD" => 32.09229,
306
                "TZS" => 2196.06165,
307
                "UAH" => 24.91479,
308
                "UGX" => 3369.336667,
309
                "USD" => 1,
310
                "UYU" => 30.70814,
311
                "UZS" => 2949.354981,
312
                "VEF" => 9.9715,
313
                "VND" => 22346.633333,
314
                "VUV" => 109.350001,
315
                "WST" => 2.514619,
316
                "XAF" => 581.638957,
317
                "XAG" => 0.057532,
318
                "XAU" => 0.0007895,
319
                "XCD" => 2.70302,
320
                "XDR" => 0.704733,
321
                "XOF" => 581.941157,
322
                "XPD" => 0.00176,
323
                "XPF" => 105.49585,
324
                "XPT" => 0.001035,
325
                "YER" => 249.269001,
326
                "ZAR" => 14.59941,
327
                "ZMK" => 5253.075255,
328
                "ZMW" => 10.95365,
329
                "ZWL" => 322.387247,
330
            ],
331
        ];
332
    }
333
}
334