-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
237 lines (211 loc) · 6.38 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Lasso\Oauth2ClientBundle;
use Lasso\Oauth2ClientBundle;
use Buzz\Browser;
use Buzz\Message\RequestInterface;
use Buzz\Message\MessageInterface;
use Buzz\Message\Response;
use Lasso\Oauth2ClientBundle\Exceptions\ClientErrorException;
use Lasso\Oauth2ClientBundle\Exceptions\ServerErrorException;
/**
* This class essentially mirrors the public functions on Buzz\Browser,
* but injects an authorization token in the headers where appropriate.
* This way you still have access to all the functionality in Buzz\Browser,
* and don't have to manage oauth tokens manually. Also, if your IDE
* supports type hinting, you can hint instances of this class with
* Buzz\Browser to get auto-completion on all available methods.
*
* Class Client
*
* @package Lasso\Oauth2ClientBundle
*/
class Client
{
/**
* @var Token
*/
protected $token;
/**
* @var Browser
*/
protected $browser;
/**
* @param Token $token
* @param Browser $browser
*/
public function __construct(Token $token, Browser $browser)
{
$this->token = $token;
$this->browser = $browser;
}
/**
* Adds authorization token to headers using bearer method
*
* @param array $headers
*
* @return array
*/
protected function patchHeaders($headers)
{
$headers[] = 'Authorization: Bearer ' . $this->token->getToken();
return $headers;
}
/**
* @param Response $response
*
* @throws Exceptions\ClientErrorException
* @throws Exceptions\ServerErrorException
*/
protected function throwExceptionOnResponseError(Response $response)
{
switch (true) {
case (400 <= $response->getStatusCode() && $response->getStatusCode() <= 499):
throw new ClientErrorException($response);
break;
case (500 <= $response->getStatusCode() && $response->getStatusCode() <= 599):
throw new ServerErrorException($response);
break;
}
}
/**
* @param string $url
* @param array $headers
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function get($url, $headers = array())
{
return $this->call($url, RequestInterface::METHOD_GET, $headers);
}
/**
* @param string $url
* @param array $headers
* @param string $content
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function post($url, $headers = array(), $content = '')
{
return $this->call($url, RequestInterface::METHOD_POST, $headers, $content);
}
/**
* @param string $url
* @param array $headers
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function head($url, $headers = array())
{
return $this->call($url, RequestInterface::METHOD_HEAD, $headers);
}
/**
* @param string $url
* @param array $headers
* @param string $content
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function patch($url, $headers = array(), $content = '')
{
return $this->call($url, RequestInterface::METHOD_PATCH, $headers, $content);
}
/**
* @param string $url
* @param array $headers
* @param string $content
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function put($url, $headers = array(), $content = '')
{
return $this->call($url, RequestInterface::METHOD_PUT, $headers, $content);
}
/**
* @param string $url
* @param array $headers
* @param string $content
*
* @see Lasso\Oauth2ClientBundle\Client::call
*
* @return Response
*/
public function delete($url, $headers = array(), $content = '')
{
return $this->call($url, RequestInterface::METHOD_DELETE, $headers, $content);
}
/**
* Sends a http request with the given method. Is wrapped by shorthand methods that
* mirror the http methods.
*
* @param string $url The URL to call
* @param string $method The request method to use
* @param array $headers An array of request headers
* @param string $content The request content
*
* @throws Exceptions\ClientErrorException
* @throws Exceptions\ServerErrorException
* @return Response The response object
*/
public function call($url, $method, $headers = array(), $content = '')
{
$headers = $this->patchHeaders($headers);
$response = $this->browser->call($url, $method, $headers, $content);
$this->throwExceptionOnResponseError($response);
return $response;
}
/**
* Sends a form request.
*
* @param string $url The URL to submit to
* @param array $fields An array of fields
* @param string $method The request method to use
* @param array $headers An array of request headers
*
* @return Response The response object
*/
public function submit($url, array $fields, $method = RequestInterface::METHOD_POST, $headers = array())
{
$headers = $this->patchHeaders($headers);
$response = $this->browser->submit($url, $fields, $method, $headers);
$this->throwExceptionOnResponseError($response);
return $response;
}
/**
* Sends a request.
*
* @param RequestInterface $request A request object
* @param MessageInterface $response A response object
*
* @return MessageInterface The response
*/
public function send(RequestInterface $request, MessageInterface $response = null)
{
$request->addHeader('Authorization: Bearer ' . $this->token->getToken());
$returnResponse = $this->browser->send($request, $response);
$this->throwExceptionOnResponseError($returnResponse);
return $returnResponse;
}
/**
* Proxy all calls that don't require injecting an Authorization-header
* to the browser instance.
*
* @param string $name The called methods name
* @param array $arguments The arguments the method should be called with
*
* @return mixed
*/
public function __call($name, $arguments)
{
return call_user_func_array([$this->browser, $name], $arguments);
}
}