1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\Buzz; |
4
|
|
|
|
5
|
|
|
use Buzz\Client\ClientInterface; |
6
|
|
|
use Buzz\Client\FileGetContents; |
7
|
|
|
use Buzz\Exception as BuzzException; |
8
|
|
|
use Buzz\Message\Request as BuzzRequest; |
9
|
|
|
use Buzz\Message\Response as BuzzResponse; |
10
|
|
|
use Http\Client\HttpClient; |
11
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
12
|
|
|
use Http\Message\MessageFactory; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Http\Client\Exception as HttplugException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Client implements HttpClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ClientInterface |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MessageFactory |
29
|
|
|
*/ |
30
|
|
|
private $messageFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param FileGetContents|null $client |
34
|
|
|
* @param MessageFactory|null $messageFactory |
35
|
|
|
*/ |
36
|
53 |
|
public function __construct(FileGetContents $client = null, MessageFactory $messageFactory = null) |
37
|
|
|
{ |
38
|
53 |
|
$this->client = $client ?: new FileGetContents(); |
39
|
53 |
|
$this->client->setMaxRedirects(0); |
40
|
|
|
|
41
|
53 |
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
42
|
53 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
53 |
|
public function sendRequest(RequestInterface $request) |
48
|
|
|
{ |
49
|
53 |
|
$buzzRequest = $this->createRequest($request); |
50
|
|
|
|
51
|
|
|
try { |
52
|
53 |
|
$buzzResponse = new BuzzResponse(); |
53
|
53 |
|
$this->client->send($buzzRequest, $buzzResponse); |
54
|
53 |
|
} catch (BuzzException\ClientException $e) { |
55
|
1 |
|
throw new HttplugException\TransferException($e->getMessage(), 0, $e); |
56
|
|
|
} |
57
|
|
|
|
58
|
52 |
|
return $this->createResponse($buzzResponse); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Converts a PSR request into a BuzzRequest request. |
63
|
|
|
* |
64
|
|
|
* @param RequestInterface $request |
65
|
|
|
* |
66
|
|
|
* @return BuzzRequest |
67
|
|
|
*/ |
68
|
53 |
|
private function createRequest(RequestInterface $request) |
69
|
|
|
{ |
70
|
53 |
|
$buzzRequest = new BuzzRequest(); |
71
|
53 |
|
$buzzRequest->setMethod($request->getMethod()); |
72
|
53 |
|
$buzzRequest->fromUrl($request->getUri()->__toString()); |
73
|
53 |
|
$buzzRequest->setProtocolVersion($request->getProtocolVersion()); |
74
|
53 |
|
$buzzRequest->setContent((string) $request->getBody()); |
75
|
|
|
|
76
|
53 |
|
$this->addPsrHeadersToBuzzRequest($request, $buzzRequest); |
77
|
|
|
|
78
|
53 |
|
return $buzzRequest; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Converts a Buzz response into a PSR response. |
83
|
|
|
* |
84
|
|
|
* @param BuzzResponse $response |
85
|
|
|
* |
86
|
|
|
* @return ResponseInterface |
87
|
|
|
*/ |
88
|
52 |
|
private function createResponse(BuzzResponse $response) |
89
|
|
|
{ |
90
|
52 |
|
$body = $response->getContent(); |
91
|
|
|
|
92
|
52 |
|
return $this->messageFactory->createResponse( |
93
|
52 |
|
$response->getStatusCode(), |
94
|
52 |
|
null, |
95
|
52 |
|
$this->getBuzzHeaders($response), |
96
|
52 |
|
$body, |
97
|
52 |
|
number_format($response->getProtocolVersion(), 1) |
98
|
52 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Apply headers on a Buzz request. |
103
|
|
|
* |
104
|
|
|
* @param RequestInterface $request |
105
|
|
|
* @param BuzzRequest $buzzRequest |
106
|
|
|
*/ |
107
|
53 |
|
private function addPsrHeadersToBuzzRequest(RequestInterface $request, BuzzRequest $buzzRequest) |
108
|
1 |
|
{ |
109
|
53 |
|
$headers = $request->getHeaders(); |
110
|
53 |
|
foreach ($headers as $name => $values) { |
111
|
53 |
|
foreach ($values as $header) { |
112
|
53 |
|
$buzzRequest->addHeader($name.': '.$header); |
113
|
53 |
|
} |
114
|
53 |
|
} |
115
|
53 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get headers from a Buzz response. |
119
|
|
|
* |
120
|
|
|
* @param RequestInterface $request |
|
|
|
|
121
|
|
|
* @param BuzzRequest $buzzRequest |
|
|
|
|
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
52 |
|
private function getBuzzHeaders(BuzzResponse $response) |
126
|
|
|
{ |
127
|
52 |
|
$buzzHeaders = $response->getHeaders(); |
128
|
52 |
|
unset($buzzHeaders[0]); |
129
|
52 |
|
$headers = []; |
130
|
52 |
|
foreach ($buzzHeaders as $headerLine) { |
131
|
52 |
|
list($name, $value) = explode(':', $headerLine, 2); |
132
|
52 |
|
$headers[$name] = trim($value); |
133
|
52 |
|
} |
134
|
|
|
|
135
|
52 |
|
return $headers; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.