1 | <?php |
||
16 | class Client implements HttpClient |
||
17 | { |
||
18 | /** @var ZendClient */ |
||
19 | private $client; |
||
20 | |||
21 | /** @var ResponseFactory */ |
||
22 | private $responseFactory; |
||
23 | |||
24 | 82 | public function __construct(ZendClient $client = null, ResponseFactory $responseFactory = null) |
|
25 | { |
||
26 | 82 | $this->client = $client ?: new ZendClient(); |
|
27 | 82 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
28 | 82 | } |
|
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 82 | public function sendRequest(RequestInterface $request) |
|
34 | { |
||
35 | 82 | $request = $this->sanitizeRequest($request); |
|
36 | 82 | $headers = new Headers(); |
|
37 | |||
38 | 82 | foreach ($request->getHeaders() as $key => $value) { |
|
|
|||
39 | 82 | $headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key))); |
|
40 | 82 | } |
|
41 | |||
42 | 82 | $zendRequest = new Request(); |
|
43 | 82 | $zendRequest->setMethod($request->getMethod()); |
|
44 | 82 | $zendRequest->setUri((string) $request->getUri()); |
|
45 | 82 | $zendRequest->setHeaders($headers); |
|
46 | 82 | $zendRequest->setContent($request->getBody()->getContents()); |
|
47 | |||
48 | $options = [ |
||
49 | 82 | 'httpversion' => $request->getProtocolVersion(), |
|
50 | 82 | ]; |
|
51 | |||
52 | 82 | if (extension_loaded('curl')) { |
|
53 | 82 | $options['curloptions'] = [ |
|
54 | 82 | CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion()), |
|
55 | ]; |
||
56 | 82 | } |
|
57 | |||
58 | 82 | $this->client->setOptions($options); |
|
59 | |||
60 | 82 | if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) { |
|
61 | 31 | $request = $request->withHeader('Content-Length', '0'); |
|
62 | 31 | } |
|
63 | |||
64 | try { |
||
65 | 82 | $zendResponse = $this->client->send($zendRequest); |
|
66 | 82 | } catch (RuntimeException $exception) { |
|
67 | 2 | throw new NetworkException($exception->getMessage(), $request, $exception); |
|
68 | } |
||
69 | |||
70 | 80 | return $this->responseFactory->createResponse( |
|
71 | 80 | $zendResponse->getStatusCode(), |
|
72 | 80 | $zendResponse->getReasonPhrase(), |
|
73 | 80 | $zendResponse->getHeaders()->toArray(), |
|
74 | 80 | $zendResponse->getContent(), |
|
75 | 80 | $zendResponse->getVersion() |
|
76 | 80 | ); |
|
77 | } |
||
78 | |||
79 | 82 | private function sanitizeRequest(RequestInterface $request) |
|
86 | |||
87 | /** |
||
88 | * Zend request remove the body if it's a trace but does not rewrite the content length header, |
||
89 | * This can lead to error from the server has it can expect a specific content length, but don't have |
||
90 | * a body, so we set content-length to 0 to avoid bad reading from the server. |
||
91 | * |
||
92 | * @param RequestInterface $request |
||
93 | * |
||
94 | * @return RequestInterface|static |
||
95 | */ |
||
96 | 82 | private function sanitizeWithTrace(RequestInterface $request) |
|
104 | |||
105 | /** |
||
106 | * On cUrl Adapter, zend does not include the body if it's not a POST, PUT or PATCH request but does not |
||
107 | * rewrite the content length header. |
||
108 | * This can lead to error from the server has it can expect a specific content length, but don't have |
||
109 | * a body, so we set content-length to 0 to avoid bad reading from the server. |
||
110 | * |
||
111 | * @param RequestInterface $request |
||
112 | * |
||
113 | * @return RequestInterface|static |
||
114 | */ |
||
115 | 82 | private function sanitizeWithCurl(RequestInterface $request) |
|
127 | |||
128 | /** |
||
129 | * Return cURL constant for specified HTTP version. |
||
130 | * |
||
131 | * @param string $requestVersion |
||
132 | * |
||
133 | * @throws \UnexpectedValueException if unsupported version requested |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | 82 | private function getProtocolVersion($requestVersion) |
|
154 | } |
||
155 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: