Completed
Push — master ( 06d9f7...a7a165 )
by ARCANEDEV
9s
created

OpenExchangeRatesService::request()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
cc 2
eloc 7
nc 1
nop 2
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
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