Skip to content

Commit 55e1193

Browse files
committed
MessageFactory wrapper to transform React Response
Mute a React Response to a Psr7 ResponseInterface instance
1 parent 945f774 commit 55e1193

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/ReactMessageFactory.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Http\Adapter;
4+
5+
use React\HttpClient\Response as ReactResponse;
6+
use Http\Message\MessageFactory;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\StreamInterface;
9+
10+
/**
11+
* Message Factory decorator to handle React response
12+
* @author Stéphane Hulard <[email protected]>
13+
*/
14+
class ReactMessageFactory
15+
{
16+
/**
17+
* @param MessageFactory $factory
18+
*/
19+
public function __construct(MessageFactory $factory)
20+
{
21+
$this->factory = $factory;
22+
}
23+
24+
/**
25+
* Transform a React Response to a valid PSR7 ResponseInterface instance
26+
* @param ReactResponse $response
27+
* @return ResponseInterface
28+
*/
29+
public function buildResponse(
30+
ReactResponse $response,
31+
StreamInterface $body
32+
) {
33+
$body->rewind();
34+
return $this->factory->createResponse(
35+
$response->getCode(),
36+
$response->getReasonPhrase(),
37+
$response->getHeaders(),
38+
$body,
39+
$response->getVersion()
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)