Skip to content

Commit 788697e

Browse files
Fix broken HttpMethodsClient with PSR RequestFactory (#202)
1 parent d70de2f commit 788697e

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
"extra": {
6363
"branch-alias": {
64-
"dev-master": "2.2.x-dev"
64+
"dev-master": "2.3.x-dev"
6565
}
6666
}
6767
}

src/HttpMethodsClient.php

+63-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Psr\Http\Message\RequestFactoryInterface;
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\StreamFactoryInterface;
13+
use Psr\Http\Message\StreamInterface;
14+
use Psr\Http\Message\UriInterface;
1215

1316
final class HttpMethodsClient implements HttpMethodsClientInterface
1417
{
@@ -22,19 +25,29 @@ final class HttpMethodsClient implements HttpMethodsClientInterface
2225
*/
2326
private $requestFactory;
2427

28+
/**
29+
* @var StreamFactoryInterface|null
30+
*/
31+
private $streamFactory;
32+
2533
/**
2634
* @param RequestFactory|RequestFactoryInterface
2735
*/
28-
public function __construct(ClientInterface $httpClient, $requestFactory)
36+
public function __construct(ClientInterface $httpClient, $requestFactory, StreamFactoryInterface $streamFactory = null)
2937
{
3038
if (!$requestFactory instanceof RequestFactory && !$requestFactory instanceof RequestFactoryInterface) {
3139
throw new \TypeError(
3240
sprintf('%s::__construct(): Argument #2 ($requestFactory) must be of type %s|%s, %s given', self::class, RequestFactory::class, RequestFactoryInterface::class, get_debug_type($requestFactory))
3341
);
3442
}
3543

44+
if (!$requestFactory instanceof RequestFactory && null === $streamFactory) {
45+
@trigger_error(sprintf('Passing a %s without a %s to %s::__construct() is deprecated as of version 2.3 and will be disallowed in version 3.0. A stream factory is required to create a request with a non-empty string body.', RequestFactoryInterface::class, StreamFactoryInterface::class, self::class));
46+
}
47+
3648
$this->httpClient = $httpClient;
3749
$this->requestFactory = $requestFactory;
50+
$this->streamFactory = $streamFactory;
3851
}
3952

4053
public function get($uri, array $headers = []): ResponseInterface
@@ -79,12 +92,55 @@ public function options($uri, array $headers = [], $body = null): ResponseInterf
7992

8093
public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface
8194
{
82-
return $this->sendRequest($this->requestFactory->createRequest(
83-
$method,
84-
$uri,
85-
$headers,
86-
$body
87-
));
95+
if (!is_string($uri) && !$uri instanceof UriInterface) {
96+
throw new \TypeError(
97+
sprintf('%s::send(): Argument #2 ($uri) must be of type string|%s, %s given', self::class, UriInterface::class, get_debug_type($uri))
98+
);
99+
}
100+
101+
if (!is_string($body) && !$body instanceof StreamInterface && null !== $body) {
102+
throw new \TypeError(
103+
sprintf('%s::send(): Argument #4 ($body) must be of type string|%s|null, %s given', self::class, StreamInterface::class, get_debug_type($body))
104+
);
105+
}
106+
107+
return $this->sendRequest(
108+
self::createRequest($method, $uri, $headers, $body)
109+
);
110+
}
111+
112+
/**
113+
* @param string|UriInterface $uri
114+
* @param string|StreamInterface|null $body
115+
*/
116+
private function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface
117+
{
118+
if ($this->requestFactory instanceof RequestFactory) {
119+
return $this->requestFactory->createRequest(
120+
$method,
121+
$uri,
122+
$headers,
123+
$body
124+
);
125+
}
126+
127+
if (is_string($body) && '' !== $body && null === $this->streamFactory) {
128+
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.');
129+
}
130+
131+
$request = $this->requestFactory->createRequest($method, $uri);
132+
133+
foreach ($headers as $key => $value) {
134+
$request = $request->withHeader($key, $value);
135+
}
136+
137+
if (null !== $body && '' !== $body) {
138+
$request = $request->withBody(
139+
is_string($body) ? $this->streamFactory->createStream($body) : $body
140+
);
141+
}
142+
143+
return $request;
88144
}
89145

90146
public function sendRequest(RequestInterface $request): ResponseInterface

0 commit comments

Comments
 (0)