1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Earnould\LaravelVeloApi; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Earnould\LaravelVeloApi\VeloClientInterface; |
10
|
|
|
use Earnould\LaravelVeloApi\Exceptions\VeloException; |
11
|
|
|
|
12
|
|
|
class VeloClient implements VeloClientInterface |
13
|
|
|
{ |
14
|
|
|
private $guzzleClient; |
15
|
|
|
private $client_id; |
16
|
|
|
private $client_secret; |
17
|
|
|
private $token_url; |
18
|
|
|
private $api_url; |
19
|
|
|
private $apiResponseFormat; |
20
|
|
|
|
21
|
8 |
|
public function __construct(Client $client = null) |
22
|
|
|
{ |
23
|
8 |
|
$this->guzzleClient = $client ?? new Client(); |
24
|
8 |
|
$this->client_id = config('laravel-velo-api.client_id'); |
25
|
8 |
|
$this->client_secret = config('laravel-velo-api.client_secret'); |
26
|
8 |
|
$this->token_url = 'https://antwerp.pub.api.smartbike.com/oauth/v2/token'; |
27
|
8 |
|
$this->api_url = 'https://antwerp.pub.api.smartbike.com/api/en/v3'; |
28
|
8 |
|
$this->apiResponseFormat = '.json'; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Request Velo API Access Token response. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
8 |
|
private function requestAccessToken() : string |
37
|
|
|
{ |
38
|
8 |
|
$response = $this->guzzleClient->post($this->token_url, [ |
39
|
|
|
'form_params' => [ |
40
|
8 |
|
'client_id' => $this->client_id, |
41
|
8 |
|
'client_secret' => $this->client_secret, |
42
|
8 |
|
'grant_type' => 'client_credentials', |
43
|
|
|
], |
44
|
|
|
]); |
45
|
|
|
|
46
|
8 |
|
$access_token = json_decode($response->getBody()->getContents())->access_token; |
47
|
|
|
|
48
|
8 |
|
if (! empty($access_token)) { |
49
|
5 |
|
return $access_token; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
throw new VeloException('An access token is required to perform requests tothe Velo API', 403); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get cached token or request a new one. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
8 |
|
public function getAccessToken() : string |
61
|
|
|
{ |
62
|
8 |
|
return Cache::remember('access_token', 55, Closure::fromCallable([$this, 'requestAccessToken'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Prepare the uri for a request. |
67
|
|
|
* |
68
|
|
|
* @param $resource |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
6 |
|
private function prepareUri($resource) : string |
72
|
|
|
{ |
73
|
6 |
|
$uri = $this->api_url.'/'.$resource.$this->apiResponseFormat; |
74
|
|
|
|
75
|
6 |
|
return $uri; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Fetch all Velo stations. |
80
|
|
|
* |
81
|
|
|
* @return Collection |
82
|
|
|
*/ |
83
|
3 |
|
public function fetchStations() : Collection |
84
|
|
|
{ |
85
|
3 |
|
$response = $this->guzzleClient->get($this->prepareUri('stations'), [ |
86
|
|
|
'headers' => [ |
87
|
3 |
|
'Authorization' => 'Bearer '.$this->getAccessToken(), |
88
|
|
|
], |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
try { |
92
|
2 |
|
$stations = json_decode($response->getBody()->getContents(), true)['stations']; |
93
|
1 |
|
} catch (\ErrorException $e) { |
94
|
1 |
|
throw new VeloException($e->getMessage(), $e->getCode()); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return collect($stations); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fetch all Velo stations statuses. |
102
|
|
|
* |
103
|
|
|
* @return Collection |
104
|
|
|
*/ |
105
|
3 |
|
public function fetchStationsStatuses() : Collection |
106
|
|
|
{ |
107
|
3 |
|
$response = $this->guzzleClient->get($this->prepareUri('stations/status'), [ |
108
|
|
|
'headers' => [ |
109
|
3 |
|
'Authorization' => 'Bearer '.$this->getAccessToken(), |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
try { |
114
|
2 |
|
$stationsStatuses = json_decode($response->getBody()->getContents(), true)['stationsStatus']; |
115
|
1 |
|
} catch (\ErrorException $e) { |
116
|
1 |
|
throw new VeloException($e->getMessage(), $e->getCode()); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return collect($stationsStatuses); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|