1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ghc\Rosetta\Connectors; |
4
|
|
|
|
5
|
|
|
use Ghc\Rosetta\Messages\HttpResponse; |
6
|
|
|
use gomes81\GuzzleHttp\Subscriber\CookieAuth; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
10
|
|
|
use kamermans\OAuth2\OAuth2Middleware; |
11
|
|
|
|
12
|
|
|
class Http extends Connector |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Basic HTTP auth. |
16
|
|
|
* Config example: ['type' => self::AUTH_BASIC, 'username' => 'USER', 'password' => 'PASSWD']. |
17
|
|
|
*/ |
18
|
|
|
const AUTH_BASIC = 'basic'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Digest HTTP auth. |
22
|
|
|
* Config example: ['type' => self::AUTH_DIGEST, 'username' => 'USER', 'password' => 'PASSWD']. |
23
|
|
|
*/ |
24
|
|
|
const AUTH_DIGEST = 'digest'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Microsoft NTLM HTTP auth. |
28
|
|
|
* Config example: ['type' => self::AUTH_NTLM, 'username' => 'USER', 'password' => 'PASSWD']. |
29
|
|
|
*/ |
30
|
|
|
const AUTH_NTLM = 'ntlm'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Oauth1 auth. |
34
|
|
|
* Config example: ['type' => self::AUTH_OAUTH1, 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'token' => 'my_token', 'token_secret' => 'my_token_secret']. |
35
|
|
|
* |
36
|
|
|
* @link https://github.com/guzzle/oauth-subscriber |
37
|
|
|
*/ |
38
|
|
|
const AUTH_OAUTH1 = 'oauth1'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Oauth2 auth. |
42
|
|
|
* Config example: ['type' => self::AUTH_OAUTH2, "uri" => "access_token_uri", "grant_type" => "client_credentials", "client_id" => "my_client_id", "client_secret" => "my_client_secret",] |
43
|
|
|
* Grant types: authorization_code, client_credentials, password_credentials, refresh_token. |
44
|
|
|
* |
45
|
|
|
* @link https://github.com/kamermans/guzzle-oauth2-subscriber |
46
|
|
|
*/ |
47
|
|
|
const AUTH_OAUTH2 = 'oauth2'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Cookie auth. |
51
|
|
|
* Config example: ['type' => self::AUTH_COOKIE, 'uri' => 'form_uri', 'fields' => ['username' => 'USER', 'password' => 'PASSWD', 'foo' => 'bar'], 'method' => 'POST', 'cookies' => 'cookie_string_or_cookie_string_array_or_cookie_jar']. |
52
|
|
|
*/ |
53
|
|
|
const AUTH_COOKIE = 'cookie'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Custom handler stack auth. |
57
|
|
|
* Config example: ['type' => self::AUTH_CUSTOM, 'handler' => $handlerStack', 'auth' => 'authName']. |
58
|
|
|
*/ |
59
|
|
|
const AUTH_CUSTOM = 'custom'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* HTTP client. |
63
|
|
|
* |
64
|
|
|
* @var Client |
65
|
|
|
*/ |
66
|
|
|
protected $client; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Boot Connector. |
70
|
|
|
*/ |
71
|
|
|
protected function boot() |
72
|
|
|
{ |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Client $client |
77
|
|
|
* |
78
|
|
|
* @return Http |
79
|
|
|
*/ |
80
|
|
|
public function setClient($client) |
81
|
|
|
{ |
82
|
|
|
$this->client = $client; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Client |
89
|
|
|
*/ |
90
|
|
|
public function getClient() |
91
|
|
|
{ |
92
|
|
|
if (!$this->client) { |
93
|
|
|
$this->bootClient(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->client; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Client |
101
|
|
|
*/ |
102
|
|
|
public function bootClient() |
103
|
|
|
{ |
104
|
|
|
if ($authConfig = $this->config->get('auth_config')) { |
105
|
|
|
$this->config->set('auth_config', null); |
106
|
|
|
$this->setAuth($authConfig); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->setClient(new Client($this->config->all())); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function setAuth($config = []) |
113
|
|
|
{ |
114
|
|
|
$clientConfig = []; |
115
|
|
|
$type = $config['type']; |
116
|
|
|
unset($config['type']); |
117
|
|
|
|
118
|
|
|
switch ($type) { |
119
|
|
View Code Duplication |
case self::AUTH_BASIC: |
|
|
|
|
120
|
|
|
$clientConfig['auth'] = [ |
121
|
|
|
$config['username'], |
122
|
|
|
$config['password'], |
123
|
|
|
]; |
124
|
|
|
break; |
125
|
|
|
case self::AUTH_DIGEST: |
126
|
|
View Code Duplication |
case self::AUTH_NTLM: |
|
|
|
|
127
|
|
|
$clientConfig['auth'] = [ |
128
|
|
|
$config['username'], |
129
|
|
|
$config['password'], |
130
|
|
|
$type, |
131
|
|
|
]; |
132
|
|
|
break; |
133
|
|
|
case self::AUTH_OAUTH1: |
134
|
|
|
$stack = HandlerStack::create(); |
135
|
|
|
$middleware = new Oauth1($config); |
136
|
|
|
$stack->push($middleware); |
137
|
|
|
$clientConfig['handler'] = $stack; |
138
|
|
|
$clientConfig['auth'] = 'oauth'; |
139
|
|
|
break; |
140
|
|
|
case self::AUTH_OAUTH2: |
141
|
|
|
$authClient = new Client(['base_uri' => $config['uri']]); |
142
|
|
|
$grantType = '\\kamermans\\OAuth2\\GrantType\\'.studly_case($config['grant_type']); |
143
|
|
|
unset($config['uri']); |
144
|
|
|
unset($config['grant_type']); |
145
|
|
|
/** @var \kamermans\OAuth2\GrantType\GrantTypeInterface $grant_type */ |
146
|
|
|
$grant_type = new $grantType($authClient, $config); |
147
|
|
|
$oauth = new OAuth2Middleware($grant_type); |
148
|
|
|
$stack = HandlerStack::create(); |
149
|
|
|
$stack->push($oauth); |
150
|
|
|
$clientConfig['handler'] = $stack; |
151
|
|
|
$clientConfig['auth'] = 'oauth'; |
152
|
|
|
break; |
153
|
|
|
case self::AUTH_COOKIE: |
154
|
|
|
$stack = HandlerStack::create(); |
155
|
|
|
if (!isset($config['cookies'])) { |
156
|
|
|
$config['cookies'] = null; |
157
|
|
|
} |
158
|
|
|
if (!isset($config['method'])) { |
159
|
|
|
$config['method'] = 'POST'; |
160
|
|
|
} |
161
|
|
|
$middleware = new CookieAuth( |
162
|
|
|
$config['uri'], |
163
|
|
|
$config['fields'], |
164
|
|
|
$config['method'], |
165
|
|
|
$config['cookies'] |
166
|
|
|
); |
167
|
|
|
$stack->push($middleware); |
168
|
|
|
$clientConfig['handler'] = $stack; |
169
|
|
|
$clientConfig['auth'] = 'cookie'; |
170
|
|
|
break; |
171
|
|
|
case self::AUTH_CUSTOM: |
172
|
|
|
$clientConfig['handler'] = $config['handler']; |
173
|
|
|
$clientConfig['auth'] = $config['auth']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($clientConfig as $key => $value) { |
177
|
|
|
$this->setConfig($key, $value); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $uri |
183
|
|
|
* @param array $options |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
public function show($uri, $options = []) |
188
|
|
|
{ |
189
|
|
|
return new HttpResponse($this->getClient()->request('GET', $uri, $options)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $uri |
194
|
|
|
* @param null|mixed $data |
195
|
|
|
* @param array $options |
196
|
|
|
* |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function create($uri, $data = null, $options = []) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if (!is_null($data)) { |
202
|
|
|
$options['form_params'] = $data; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return new HttpResponse($this->getClient()->request('POST', $uri, $options)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $uri |
210
|
|
|
* @param null|mixed $data |
211
|
|
|
* @param array $options |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function update($uri, $data = null, $options = []) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
if (!is_null($data)) { |
218
|
|
|
$options['form_params'] = $data; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return new HttpResponse($this->getClient()->request('PATCH', $uri, $options)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $uri |
226
|
|
|
* @param null|mixed $data |
227
|
|
|
* @param array $options |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function replace($uri, $data = null, $options = []) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
if (!is_null($data)) { |
234
|
|
|
$options['form_params'] = $data; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return new HttpResponse($this->getClient()->request('PUT', $uri, $options)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string $uri |
242
|
|
|
* @param array $options |
243
|
|
|
* |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
public function delete($uri, $options = []) |
247
|
|
|
{ |
248
|
|
|
return new HttpResponse($this->getClient()->request('DELETE', $uri, $options)); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
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.