|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use DigitalOceanV2\Exception\HttpException; |
|
15
|
|
|
use Guzzle\Http\Client; |
|
16
|
|
|
use Guzzle\Http\ClientInterface; |
|
17
|
|
|
use Guzzle\Http\Exception\RequestException; |
|
18
|
|
|
use Guzzle\Http\Message\Response; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Liverbool <[email protected]> |
|
22
|
|
|
* @author Graham Campbell <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class GuzzleAdapter implements AdapterInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ClientInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Response |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $response; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $token |
|
38
|
|
|
* @param ClientInterface|null $client |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($token, ClientInterface $client = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->client = $client ?: new Client(); |
|
43
|
|
|
|
|
44
|
|
|
$this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $token)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function get($url) |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$this->response = $this->client->get($url)->send(); |
|
54
|
|
|
} catch (RequestException $e) { |
|
55
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
56
|
|
|
$this->handleError(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->response->getBody(true); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function delete($url) |
|
66
|
|
|
{ |
|
67
|
|
|
try { |
|
68
|
|
|
$this->response = $this->client->delete($url)->send(); |
|
69
|
|
|
} catch (RequestException $e) { |
|
70
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
71
|
|
|
$this->handleError(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->response->getBody(true); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function put($url, $content = '') |
|
81
|
|
|
{ |
|
82
|
|
|
$request = $this->client->put($url); |
|
83
|
|
|
|
|
84
|
|
|
if (is_array($content)) { |
|
85
|
|
|
$request->setBody(json_encode($content), 'application/json'); |
|
86
|
|
|
} else { |
|
87
|
|
|
$request->setBody($content); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$this->response = $request->send(); |
|
92
|
|
|
} catch (RequestException $e) { |
|
93
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
94
|
|
|
$this->handleError(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->response->getBody(true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function post($url, $content = '') |
|
104
|
|
|
{ |
|
105
|
|
|
$request = $this->client->post($url); |
|
106
|
|
|
|
|
107
|
|
|
if (is_array($content)) { |
|
108
|
|
|
$request->setBody(json_encode($content), 'application/json'); |
|
109
|
|
|
} else { |
|
110
|
|
|
$request->setBody($content); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$this->response = $request->send(); |
|
115
|
|
|
} catch (RequestException $e) { |
|
116
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
117
|
|
|
$this->handleError(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->response->getBody(true); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getLatestResponseHeaders() |
|
127
|
|
|
{ |
|
128
|
|
|
if (null === $this->response) { |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return [ |
|
133
|
|
|
'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), |
|
134
|
|
|
'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), |
|
135
|
|
|
'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'), |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @throws HttpException |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function handleError() |
|
143
|
|
|
{ |
|
144
|
|
|
$body = (string) $this->response->getBody(true); |
|
145
|
|
|
$code = (int) $this->response->getStatusCode(); |
|
146
|
|
|
|
|
147
|
|
|
$content = json_decode($body); |
|
148
|
|
|
|
|
149
|
|
|
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: