1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq; |
4
|
|
|
|
5
|
|
|
use Bunq\Exception\BunqException; |
6
|
|
|
use Bunq\Exception\SessionWasExpiredException; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
|
10
|
|
|
final class Client implements BunqClient |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ClientInterface |
14
|
|
|
*/ |
15
|
|
|
private $httpClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param ClientInterface $httpClient |
19
|
|
|
*/ |
20
|
|
|
public function __construct(ClientInterface $httpClient) |
21
|
|
|
{ |
22
|
|
|
$this->httpClient = $httpClient; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $url |
27
|
|
|
* @param array $options |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function get($url, array $options = []) |
32
|
|
|
{ |
33
|
|
|
return $this->requestAPI('GET', (string)$url, $options); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $url |
38
|
|
|
* @param array $options |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function post($url, array $options = []) |
43
|
|
|
{ |
44
|
|
|
return $this->requestAPI('POST', (string)$url, $options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $url |
49
|
|
|
* @param array $options |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function put($url, array $options = []) |
54
|
|
|
{ |
55
|
|
|
return $this->requestAPI('PUT', $url, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Handles the API Calling. |
60
|
|
|
* |
61
|
|
|
* @param string $method |
62
|
|
|
* @param string $url |
63
|
|
|
* @param array $options |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
* @throws BunqException |
67
|
|
|
*/ |
68
|
|
|
private function requestAPI($method, $url, array $options = []) |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
return $this->sendRequest((string)$method, (string)$url, $options); |
72
|
|
|
} catch (SessionWasExpiredException $e) { |
73
|
|
|
return $this->sendRequest((string)$method, (string)$url, $options); |
74
|
|
|
} catch (ClientException $e) { |
75
|
|
|
throw new BunqException($e); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $method |
81
|
|
|
* @param string $url |
82
|
|
|
* @param array $options |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
private function sendRequest($method, $url, array $options = []) |
87
|
|
|
{ |
88
|
|
|
$response = $this->httpClient->request((string)$method, $url, $options); |
89
|
|
|
|
90
|
|
|
return json_decode($response->getBody(), true); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|