1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\Guzzle5; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Message\RequestInterface as GuzzleRequest; |
8
|
|
|
use GuzzleHttp\Message\ResponseInterface as GuzzleResponse; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
use Http\Message\MessageFactory; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Http\Client\Exception as HttplugException; |
15
|
|
|
use GuzzleHttp\Exception as GuzzleExceptions; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author GeLo <[email protected]> |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Client implements HttpClient |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var MessageFactory |
30
|
|
|
*/ |
31
|
|
|
private $messageFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ClientInterface|null $client |
35
|
|
|
* @param MessageFactory|null $messageFactory |
36
|
|
|
*/ |
37
|
118 |
|
public function __construct(ClientInterface $client = null, MessageFactory $messageFactory = null) |
38
|
|
|
{ |
39
|
118 |
|
$this->client = $client ?: new GuzzleClient(); |
40
|
118 |
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
41
|
118 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
118 |
|
public function sendRequest(RequestInterface $request) |
47
|
|
|
{ |
48
|
118 |
|
$guzzleRequest = $this->createRequest($request); |
49
|
|
|
|
50
|
|
|
try { |
51
|
118 |
|
$response = $this->client->send($guzzleRequest); |
52
|
118 |
|
} catch (GuzzleExceptions\TransferException $e) { |
53
|
14 |
|
throw $this->handleException($e, $request); |
54
|
|
|
} |
55
|
|
|
|
56
|
104 |
|
return $this->createResponse($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Converts a PSR request into a Guzzle request. |
61
|
|
|
* |
62
|
|
|
* @param RequestInterface $request |
63
|
|
|
* |
64
|
|
|
* @return GuzzleRequest |
65
|
|
|
*/ |
66
|
118 |
|
private function createRequest(RequestInterface $request) |
67
|
|
|
{ |
68
|
|
|
$options = [ |
69
|
118 |
|
'exceptions' => false, |
70
|
118 |
|
'allow_redirects' => false, |
71
|
118 |
|
]; |
72
|
|
|
|
73
|
118 |
|
$options['version'] = $request->getProtocolVersion(); |
74
|
118 |
|
$options['headers'] = $request->getHeaders(); |
75
|
118 |
|
$options['body'] = (string) $request->getBody(); |
76
|
|
|
|
77
|
118 |
|
return $this->client->createRequest( |
78
|
118 |
|
$request->getMethod(), |
79
|
118 |
|
(string) $request->getUri(), |
80
|
|
|
$options |
81
|
118 |
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Converts a Guzzle response into a PSR response. |
86
|
|
|
* |
87
|
|
|
* @param GuzzleResponse $response |
88
|
|
|
* |
89
|
|
|
* @return ResponseInterface |
90
|
|
|
*/ |
91
|
108 |
|
private function createResponse(GuzzleResponse $response) |
92
|
|
|
{ |
93
|
108 |
|
$body = $response->getBody(); |
94
|
|
|
|
95
|
108 |
|
return $this->messageFactory->createResponse( |
96
|
108 |
|
$response->getStatusCode(), |
97
|
108 |
|
null, |
98
|
108 |
|
$response->getHeaders(), |
99
|
108 |
|
isset($body) ? $body->detach() : null, |
100
|
108 |
|
$response->getProtocolVersion() |
101
|
108 |
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Converts a Guzzle exception into an Httplug exception. |
106
|
|
|
* |
107
|
|
|
* @param GuzzleExceptions\TransferException $exception |
108
|
|
|
* @param RequestInterface $request |
109
|
|
|
* |
110
|
|
|
* @return HttplugException |
111
|
|
|
*/ |
112
|
14 |
|
private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request) |
113
|
|
|
{ |
114
|
14 |
|
if ($exception instanceof GuzzleExceptions\ConnectException) { |
115
|
3 |
|
return new HttplugException\NetworkException($exception->getMessage(), $request, $exception); |
116
|
|
|
} |
117
|
|
|
|
118
|
11 |
|
if ($exception instanceof GuzzleExceptions\RequestException) { |
119
|
|
|
// Make sure we have a response for the HttpException |
120
|
9 |
|
if ($exception->hasResponse()) { |
121
|
4 |
|
$psr7Response = $this->createResponse($exception->getResponse()); |
122
|
4 |
|
return new HttplugException\HttpException( |
123
|
4 |
|
$exception->getMessage(), |
124
|
4 |
|
$request, |
125
|
4 |
|
$psr7Response, |
126
|
|
|
$exception |
127
|
4 |
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
return new HttplugException\RequestException($exception->getMessage(), $request, $exception); |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return new HttplugException\TransferException($exception->getMessage(), 0, $exception); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|