1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Link0\Bunq; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Middleware; |
9
|
|
|
use Link0\Bunq\Domain\Certificate; |
10
|
|
|
use Link0\Bunq\Domain\DeviceServer; |
11
|
|
|
use Link0\Bunq\Domain\Id; |
12
|
|
|
use Link0\Bunq\Domain\Keypair; |
13
|
|
|
use Link0\Bunq\Domain\Keypair\PublicKey; |
14
|
|
|
use Link0\Bunq\Domain\MonetaryAccountBank; |
15
|
|
|
use Link0\Bunq\Domain\Payment; |
16
|
|
|
use Link0\Bunq\Domain\Token; |
17
|
|
|
use Link0\Bunq\Domain\UserCompany; |
18
|
|
|
use Link0\Bunq\Domain\UserPerson; |
19
|
|
|
use Link0\Bunq\Middleware\DebugMiddleware; |
20
|
|
|
use Link0\Bunq\Middleware\RequestIdMiddleware; |
21
|
|
|
use Link0\Bunq\Middleware\RequestSignatureMiddleware; |
22
|
|
|
use Link0\Bunq\Middleware\ResponseSignatureMiddleware; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
|
25
|
|
|
final class Client |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var GuzzleClient |
29
|
|
|
*/ |
30
|
|
|
private $guzzle; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var HandlerStack |
34
|
|
|
*/ |
35
|
|
|
private $handlerStack; |
36
|
|
|
|
37
|
|
|
public function __construct(Environment $environment, Keypair $keypair, PublicKey $serverPublicKey = null, string $sessionToken = '', $proxy = null) |
38
|
|
|
{ |
39
|
|
|
Assertion::true(is_null($proxy) || is_string($proxy) || is_array($proxy), 'In case a proxy parameter is provided, it should be either a string or an array.'); |
40
|
|
|
|
41
|
|
|
$this->handlerStack = HandlerStack::create(); |
42
|
|
|
|
43
|
|
|
$this->addRequestIdMiddleware($sessionToken); |
44
|
|
|
$this->addRequestSignatureMiddleware($keypair); |
45
|
|
|
$this->addServerResponseMiddleware($serverPublicKey); |
46
|
|
|
$this->addDebugMiddleware($environment); |
47
|
|
|
|
48
|
|
|
$configuration = [ |
49
|
|
|
'base_uri' => $environment->endpoint(), |
50
|
|
|
'handler' => $this->handlerStack, |
51
|
|
|
'headers' => [ |
52
|
|
|
'User-Agent' => 'Link0 Bunq API Client' |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
if(is_string($proxy) || is_array($proxy)){ |
57
|
|
|
$configuration['proxy'] = $proxy; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->guzzle = new GuzzleClient($configuration); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function get(string $endpoint, array $headers = []): array |
64
|
|
|
{ |
65
|
|
|
return $this->processResponse( |
66
|
|
|
$this->guzzle->request('GET', $endpoint, [ |
67
|
|
|
'headers' => $headers, |
68
|
|
|
]) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function post(string $endpoint, array $body, array $headers = []): array |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return $this->processResponse( |
75
|
|
|
$this->guzzle->request('POST', $endpoint, [ |
76
|
|
|
'json' => $body, |
77
|
|
|
'headers' => $headers, |
78
|
|
|
]) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function put(string $endpoint, array $body, array $headers = []): array |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return $this->processResponse( |
85
|
|
|
$this->guzzle->request('PUT', $endpoint, [ |
86
|
|
|
'json' => $body, |
87
|
|
|
'headers' => $headers, |
88
|
|
|
]) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $endpoint |
94
|
|
|
* @param array $headers |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function delete(string $endpoint, array $headers = []) |
99
|
|
|
{ |
100
|
|
|
$this->guzzle->request('DELETE', $endpoint, [ |
101
|
|
|
'headers' => $headers, |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function processResponse(ResponseInterface $response): array |
106
|
|
|
{ |
107
|
|
|
$contents = (string)$response->getBody(); |
108
|
|
|
$json = json_decode($contents, true)['Response']; |
109
|
|
|
|
110
|
|
|
// Return empty responses |
111
|
|
|
if (count($json) === 0) { |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($json as $key => $value) { |
116
|
|
|
if (is_numeric($key)) { |
117
|
|
|
// Often only a single associative entry here |
118
|
|
|
foreach ($value as $type => $struct) { |
119
|
|
|
$json[$key] = $this->mapResponse($type, $struct); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $json; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function mapResponse(string $key, array $value) |
127
|
|
|
{ |
128
|
|
|
switch ($key) { |
129
|
|
|
case 'DeviceServer': |
130
|
|
|
return DeviceServer::fromArray($value); |
131
|
|
|
case 'MonetaryAccountBank': |
132
|
|
|
return MonetaryAccountBank::fromArray($value); |
133
|
|
|
case 'UserPerson': |
134
|
|
|
return UserPerson::fromArray($value); |
135
|
|
|
case 'UserCompany': |
136
|
|
|
return UserCompany::fromArray($value); |
137
|
|
|
case 'Id': |
138
|
|
|
return Id::fromInteger($value['id']); |
139
|
|
|
case 'CertificatePinned': |
140
|
|
|
return Certificate::fromArray($value); |
141
|
|
|
case 'Payment': |
142
|
|
|
return Payment::fromArray($value); |
143
|
|
|
case 'ServerPublicKey': |
144
|
|
|
return PublicKey::fromServerPublicKey($value); |
145
|
|
|
case 'Token': |
146
|
|
|
return Token::fromArray($value); |
147
|
|
|
default: |
148
|
|
|
throw new \Exception('Unknown struct type: ' . $key); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $sessionToken |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
private function addRequestIdMiddleware(string $sessionToken) |
158
|
|
|
{ |
159
|
|
|
$this->handlerStack->push( |
160
|
|
|
Middleware::mapRequest(new RequestIdMiddleware($sessionToken)), |
161
|
|
|
'bunq_request_id' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Keypair $keypair |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
private function addRequestSignatureMiddleware(Keypair $keypair) |
171
|
|
|
{ |
172
|
|
|
// TODO: Figure out if we can skip this middleware on POST /installation |
173
|
|
|
$this->handlerStack->push( |
174
|
|
|
Middleware::mapRequest(new RequestSignatureMiddleware($keypair->privateKey())), |
175
|
|
|
'bunq_request_signature' |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param PublicKey|null $serverPublicKey |
181
|
|
|
* |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
private function addServerResponseMiddleware(PublicKey $serverPublicKey = null) |
185
|
|
|
{ |
186
|
|
|
// If we have obtained the server's public key, we can verify responses |
187
|
|
|
if ($serverPublicKey instanceof PublicKey) { |
188
|
|
|
$this->handlerStack->push( |
189
|
|
|
Middleware::mapResponse(new ResponseSignatureMiddleware($serverPublicKey)) |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param Environment $environment |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
private function addDebugMiddleware(Environment $environment) |
200
|
|
|
{ |
201
|
|
|
if ($environment->inDebugMode()) { |
202
|
|
|
$this->handlerStack->push(DebugMiddleware::tap(), 'debug_tap'); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.