1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Katapoka\Ahgora\Adapters; |
4
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Katapoka\Ahgora\HttpResponse; |
8
|
|
|
use Katapoka\Ahgora\Contracts\IHttpClient; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Guzzle IHttpClient version |
12
|
|
|
*/ |
13
|
|
|
class GuzzleAdapter implements IHttpClient |
14
|
|
|
{ |
15
|
|
|
/** @var \GuzzleHttp\Client The guzzle client. */ |
16
|
|
|
private $client; |
17
|
|
|
|
18
|
|
|
/** @var int the timeout of the client or the request. */ |
19
|
|
|
private $timeout; |
20
|
|
|
|
21
|
|
|
/** @var array The client headers. */ |
22
|
|
|
private $headers = []; |
23
|
|
|
|
24
|
|
|
/** @var bool Set if the request sends an json */ |
25
|
|
|
private $isJson = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* GuzzleAdapter constructor. |
29
|
|
|
* |
30
|
|
|
* @param Client $client |
31
|
|
|
*/ |
32
|
11 |
|
public function __construct(Client $client) |
33
|
|
|
{ |
34
|
11 |
|
$this->client = $client; |
35
|
11 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Make an http request to some URL with the given http method |
39
|
|
|
* |
40
|
|
|
* @param string $method |
41
|
|
|
* @param string $url |
42
|
|
|
* @param array $data |
43
|
|
|
* @param array $config |
44
|
|
|
* |
45
|
|
|
* @return \Katapoka\Ahgora\Contracts\IHttpResponse |
46
|
|
|
*/ |
47
|
3 |
|
public function request($method, $url, $data = [], array $config = []) |
48
|
|
|
{ |
49
|
3 |
|
$config = array_merge($config, [ |
50
|
3 |
|
'form_params' => $data, |
51
|
|
|
'headers' => [ |
52
|
3 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
53
|
3 |
|
], |
54
|
3 |
|
]); |
55
|
|
|
|
56
|
3 |
|
$response = $this->client->request($method, $url, $config); |
57
|
|
|
|
58
|
3 |
|
$httpResponse = new HttpResponse([ |
59
|
3 |
|
'httpStatus' => $response->getStatusCode(), |
60
|
3 |
|
'body' => $response->getBody(), |
61
|
3 |
|
'headers' => $response->getHeaders(), |
62
|
3 |
|
]); |
63
|
|
|
|
64
|
3 |
|
return $httpResponse; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Make a get request to an URL. |
69
|
|
|
* |
70
|
|
|
* @param string $url |
71
|
|
|
* @param array $data |
72
|
|
|
* @param array $config |
73
|
|
|
* |
74
|
|
|
* @return \Katapoka\Ahgora\Contracts\IHttpResponse |
75
|
|
|
*/ |
76
|
1 |
|
public function get($url, $data = [], array $config = []) |
77
|
|
|
{ |
78
|
1 |
|
return $this->request(IHttpClient::HTTP_GET, $url, $data, $config); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Make a post request to an URL. |
83
|
|
|
* |
84
|
|
|
* @param string $url |
85
|
|
|
* @param array $data |
86
|
|
|
* @param array $config |
87
|
|
|
* |
88
|
|
|
* @return \Katapoka\Ahgora\Contracts\IHttpResponse |
89
|
|
|
*/ |
90
|
1 |
|
public function post($url, $data = [], array $config = []) |
91
|
|
|
{ |
92
|
1 |
|
return once(function () use ($url, $data, $config) { |
93
|
|
|
return $this->request(IHttpClient::HTTP_POST, $url, $data, $config); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set a header to the request. |
99
|
|
|
* |
100
|
|
|
* @param string $header |
101
|
|
|
* @param string $value |
102
|
|
|
* |
103
|
5 |
|
* @return \Katapoka\Ahgora\Contracts\IHttpClient the instance of the class for method chaining |
104
|
|
|
*/ |
105
|
5 |
|
public function setHeader($header, $value) |
106
|
1 |
|
{ |
107
|
|
|
if (!is_string($header)) { |
108
|
|
|
throw new InvalidArgumentException('Header should be a string'); |
109
|
5 |
|
} |
110
|
1 |
|
|
111
|
|
|
if (!is_string($value)) { |
112
|
|
|
throw new InvalidArgumentException('Value should be a string'); |
113
|
5 |
|
} |
114
|
|
|
|
115
|
5 |
|
$this->headers[$header] = $value; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Unset a header to the request. |
122
|
|
|
* |
123
|
|
|
* @param string $header |
124
|
|
|
* |
125
|
2 |
|
* @return \Katapoka\Ahgora\Contracts\IHttpClient the instance of the class for method chaining |
126
|
|
|
*/ |
127
|
2 |
|
public function unsetHeader($header) |
128
|
2 |
|
{ |
129
|
2 |
|
if ($this->headerExists($header)) { |
130
|
|
|
unset($this->headers[$header]); |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieves the value of a given header name. |
138
|
|
|
* |
139
|
|
|
* @param string $header |
140
|
|
|
* |
141
|
2 |
|
* @return string|null |
142
|
|
|
*/ |
143
|
2 |
|
public function getHeader($header) |
144
|
2 |
|
{ |
145
|
|
|
if ($this->headerExists($header)) { |
146
|
|
|
return $this->headers[$header]; |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
return null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get all headers from the http client. |
154
|
|
|
* |
155
|
3 |
|
* @return array |
156
|
|
|
*/ |
157
|
3 |
|
public function getHeaders() |
158
|
|
|
{ |
159
|
|
|
return $this->headers; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set a timeout to the connection. |
164
|
|
|
* |
165
|
|
|
* @param int $timeout |
166
|
|
|
* |
167
|
1 |
|
* @return \Katapoka\Ahgora\Contracts\IHttpClient |
168
|
|
|
*/ |
169
|
1 |
|
public function setTimeout($timeout) |
170
|
|
|
{ |
171
|
1 |
|
$this->timeout = $timeout; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set if the request will response a json instead of form data. |
178
|
|
|
* |
179
|
|
|
* @param bool $isJson |
180
|
|
|
* |
181
|
1 |
|
* @return \Katapoka\Ahgora\Contracts\IHttpClient |
182
|
|
|
*/ |
183
|
1 |
|
public function setIsJson($isJson = true) |
184
|
1 |
|
{ |
185
|
|
|
if (!is_bool($isJson)) { |
186
|
|
|
throw new InvalidArgumentException('IsJson should be a boolean'); |
187
|
1 |
|
} |
188
|
1 |
|
|
189
|
1 |
|
$this->isJson = $isJson; |
190
|
1 |
|
if ($isJson) { |
191
|
1 |
|
$this->setHeader('content-type', 'application/json'); |
192
|
|
|
} else { |
193
|
|
|
$this->unsetHeader('content-type'); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
3 |
|
} |
198
|
|
|
|
199
|
3 |
|
private function headerExists($header) |
200
|
|
|
{ |
201
|
|
|
return array_key_exists($header, $this->headers); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|