|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DigitalOceanV2\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use DigitalOceanV2\Exception\ExceptionInterface; |
|
6
|
|
|
use GuzzleHttp\Psr7\Response; |
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
9
|
|
|
|
|
10
|
|
|
class Guzzle6Adapter extends AbstractAdapter implements AdapterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ClientInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ResponseInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $response; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ExceptionInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $exception; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $accessToken |
|
29
|
|
|
* @param ClientInterface $client (optional) |
|
30
|
|
|
* @param ExceptionInterface $exception (optional) |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($accessToken, ClientInterface $client = null, ExceptionInterface $exception = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->client = $client ?: new \GuzzleHttp\Client(['headers' => [ |
|
35
|
|
|
'Authorization' => sprintf('Bearer %s', $accessToken) |
|
36
|
|
|
]]); |
|
37
|
|
|
$this->exception = $exception; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get($url) |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
$this->response = $this->client->get($url); |
|
47
|
|
|
} catch (RequestException $e) { |
|
48
|
|
|
throw $this->handleResponse( $e->getResponse() ); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->response->getBody(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function delete($url, array $headers = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
$options = array('headers' => $headers); |
|
61
|
|
|
$this->response = $this->client->delete($url, $options); |
|
62
|
|
|
} catch (RequestException $e) { |
|
63
|
|
|
throw $this->handleResponse( $e->getResponse() ); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->response->getBody(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function put($url, array $headers = array(), $content = '') |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$options = ($json = json_decode($content, true)) ? |
|
76
|
|
|
array('headers' => $headers, 'json' => $json) : |
|
77
|
|
|
array('headers' => $headers, 'body' => $content); |
|
78
|
|
|
|
|
79
|
|
|
$this->response = $this->client->put($url, $options); |
|
80
|
|
|
} catch (RequestException $e) { |
|
81
|
|
|
throw $this->handleResponse( $e->getResponse() ); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->response->getBody(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function post($url, array $headers = array(), $content = '') |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
|
|
$options = ($json = json_decode($content, true)) ? |
|
94
|
|
|
array('headers' => $headers, 'json' => $json) : |
|
95
|
|
|
array('headers' => $headers, 'body' => $content); |
|
96
|
|
|
|
|
97
|
|
|
$this->response = $this->client->post($url, $options); |
|
98
|
|
|
} catch (RequestException $e) { |
|
99
|
|
|
throw $this->handleResponse( $e->getResponse() ); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->response->getBody(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getLatestResponseHeaders() |
|
109
|
|
|
{ |
|
110
|
|
|
if (null === $this->response) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return array( |
|
115
|
|
|
'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'), |
|
116
|
|
|
'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'), |
|
117
|
|
|
'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'), |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Response $response |
|
123
|
|
|
* @return ExceptionInterface|\RuntimeException |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function handleResponse(Response $response) |
|
126
|
|
|
{ |
|
127
|
|
|
$body = (string)$response->getBody(); |
|
128
|
|
|
$code = $response->getStatusCode(); |
|
129
|
|
|
|
|
130
|
|
|
if ($this->exception) { |
|
131
|
|
|
return $this->exception->create($body, $code); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$content = json_decode($body); |
|
135
|
|
|
|
|
136
|
|
|
return new \RuntimeException(sprintf('[%d] %s (%s)', $code, $content->message, $content->id), $code); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.