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 Buzz\Browser; |
15
|
|
|
use Buzz\Client\Curl; |
16
|
|
|
use Buzz\Listener\ListenerInterface; |
17
|
|
|
use Buzz\Message\Response; |
18
|
|
|
use DigitalOceanV2\Exception\HttpException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Antoine Corcy <[email protected]> |
22
|
|
|
* @author Graham Campbell <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class BuzzAdapter implements AdapterInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Browser |
28
|
|
|
*/ |
29
|
|
|
protected $browser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $token |
33
|
|
|
* @param Browser|null $browser |
34
|
|
|
* @param ListenerInterface|null $listener |
35
|
|
|
*/ |
36
|
|
|
public function __construct($token, Browser $browser = null, ListenerInterface $listener = null) |
37
|
|
|
{ |
38
|
|
|
$this->browser = $browser ?: new Browser(new Curl()); |
39
|
|
|
$this->browser->addListener($listener ?: new BuzzOAuthListener($token)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function get($url) |
46
|
|
|
{ |
47
|
|
|
$response = $this->browser->get($url); |
48
|
|
|
|
49
|
|
|
$this->handleResponse($response); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $response->getContent(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function delete($url) |
58
|
|
|
{ |
59
|
|
|
$response = $this->browser->delete($url); |
60
|
|
|
|
61
|
|
|
$this->handleResponse($response); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function put($url, $content = '') |
68
|
|
|
{ |
69
|
|
|
$headers = []; |
70
|
|
|
|
71
|
|
|
if (is_array($content)) { |
72
|
|
|
$content = json_encode($content); |
73
|
|
|
$headers[] = 'Content-Type: application/json'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$response = $this->browser->put($url, $headers, $content); |
77
|
|
|
|
78
|
|
|
$this->handleResponse($response); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return $response->getContent(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function post($url, $content = '') |
87
|
|
|
{ |
88
|
|
|
$headers = []; |
89
|
|
|
|
90
|
|
|
if (is_array($content)) { |
91
|
|
|
$content = json_encode($content); |
92
|
|
|
$headers[] = 'Content-Type: application/json'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$response = $this->browser->post($url, $headers, $content); |
96
|
|
|
|
97
|
|
|
$this->handleResponse($response); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $response->getContent(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getLatestResponseHeaders() |
106
|
|
|
{ |
107
|
|
|
if (null === $response = $this->browser->getLastResponse()) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return [ |
112
|
|
|
'reset' => (int) $response->getHeader('RateLimit-Reset'), |
113
|
|
|
'remaining' => (int) $response->getHeader('RateLimit-Remaining'), |
114
|
|
|
'limit' => (int) $response->getHeader('RateLimit-Limit'), |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Response $response |
120
|
|
|
* |
121
|
|
|
* @throws HttpException |
122
|
|
|
*/ |
123
|
|
|
protected function handleResponse(Response $response) |
124
|
|
|
{ |
125
|
|
|
if ($response->isSuccessful()) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->handleError($response); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Response $response |
134
|
|
|
* |
135
|
|
|
* @throws HttpException |
136
|
|
|
*/ |
137
|
|
|
protected function handleError(Response $response) |
138
|
|
|
{ |
139
|
|
|
$body = (string) $response->getContent(); |
|
|
|
|
140
|
|
|
$code = (int) $response->getStatusCode(); |
141
|
|
|
|
142
|
|
|
$content = json_decode($response->getContent()); |
143
|
|
|
|
144
|
|
|
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.