1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Infinitypaul\NairaExchangeRates; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Infinitypaul\NairaExchangeRates\Exceptions\Exceptions; |
8
|
|
|
use Infinitypaul\NairaExchangeRates\Traits\CurrencyTraits; |
9
|
|
|
use Infinitypaul\NairaExchangeRates\Traits\DateTraits; |
10
|
|
|
use Infinitypaul\NairaExchangeRates\Traits\TypeTraits; |
11
|
|
|
|
12
|
|
|
class NairaExchangeRates |
13
|
|
|
{ |
14
|
|
|
use CurrencyTraits, TypeTraits, DateTraits; |
15
|
|
|
/** |
16
|
|
|
* Naira Exchange Rate API Base Url. |
17
|
|
|
*/ |
18
|
|
|
const baseURL = 'http://nairaexchangerate.herokuapp.com/api/v1/'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Access token. |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $accessToken; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Response from requests made to Naira Exchange Rate. |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $response; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Instance of Guzzle Client. |
34
|
|
|
* @var object |
35
|
|
|
*/ |
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
public function __construct($accessToken = null) |
39
|
|
|
{ |
40
|
|
|
if (is_null($accessToken)) { |
41
|
|
|
throw Exceptions::create('format.is_null'); |
42
|
|
|
} |
43
|
|
|
$this->accessToken = $accessToken; |
44
|
|
|
$this->prepareRequest(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function prepareRequest() |
48
|
|
|
{ |
49
|
|
|
$this->client = new Client(['base_uri' => self::baseURL, 'headers' => [ |
50
|
|
|
'Accept' => 'application/json', |
51
|
|
|
'Authorization' => 'Bearer '.$this->accessToken, |
52
|
|
|
]]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getResponse() |
56
|
|
|
{ |
57
|
|
|
$response = new Response($this->response); |
58
|
|
|
$json = $response->toJSON(); |
59
|
|
|
|
60
|
|
|
return json_decode($json); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function performGetRequest($relativeUrl, $params) |
64
|
|
|
{ |
65
|
|
|
$this->response = $this->client->request('GET', $relativeUrl, $params); |
66
|
|
|
|
67
|
|
|
return $this->getResponse(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function fetch() |
71
|
|
|
{ |
72
|
|
|
// Build the URL: |
73
|
|
|
$params = []; |
74
|
|
|
|
75
|
|
|
// Set the relevant endpoint: |
76
|
|
|
$endpoint = (is_null($this->dateFrom)) ? '/latest' : '/history'; |
77
|
|
|
|
78
|
|
|
// Add dateFrom if specified: |
79
|
|
|
if (! is_null($this->getDateFrom())) { |
80
|
|
|
$params['start_at'] = $this->getDateFrom(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Add a dateTo: |
84
|
|
|
if (! is_null($this->getDateTo())) { |
85
|
|
|
$params['end_at'] = $this->getDateTo(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Set the base currency: |
89
|
|
|
if (! is_null($this->getBaseCurrency())) { |
90
|
|
|
$params['currency'] = $this->getBaseCurrency(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
return $this->performGetRequest($this->getBaseType().$endpoint, ['query' => $params]); |
95
|
|
|
} catch (Exception $exception) { |
96
|
|
|
throw new Exception($exception->getMessage()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|