8
8
use Omnipay \Common \Http \Exception \RequestException ;
9
9
use Psr \Http \Message \RequestInterface ;
10
10
use Psr \Http \Message \ResponseInterface ;
11
+ use Psr \Http \Message \StreamFactoryInterface ;
11
12
use Psr \Http \Message \StreamInterface ;
12
- use Psr \Http \Client \ClientInterface as Psr18ClientInterface ;
13
- use Psr \Http \Message \RequestFactoryInterface as Psr17RequestFactoryInterface ;
13
+ use Psr \Http \Client \ClientInterface as HttpClientInterface ;
14
+ use Psr \Http \Message \RequestFactoryInterface as RequestFactoryInterface ;
15
+ use Psr \Http \Message \UriFactoryInterface ;
14
16
15
17
final class Client implements ClientInterface
16
18
{
17
19
/**
18
20
* The Http Client which implements `public function sendRequest(RequestInterface $request)`
19
21
*
20
- * @var Psr18ClientInterface
22
+ * @var HttpClientInterface
21
23
*/
22
24
private $ httpClient ;
23
25
24
26
/**
25
- * @var Psr17RequestFactoryInterface
27
+ * @var RequestFactoryInterface
26
28
*/
27
29
private $ requestFactory ;
28
30
31
+ /**
32
+ * @var StreamFactoryInterface
33
+ */
34
+ private $ streamFactory ;
35
+
29
36
public function __construct ($ httpClient = null , $ requestFactory = null )
30
37
{
31
- $ this ->httpClient = $ httpClient ?: Psr18ClientDiscovery::find ();
32
- $ this ->requestFactory = $ requestFactory ?: Psr17FactoryDiscovery::findRequestFactory ();
38
+ $ this ->httpClient = $ httpClient ;
39
+ $ this ->requestFactory = $ requestFactory ;
40
+ }
41
+
42
+ private function getHttpClient () : HttpClientInterface
43
+ {
44
+ if (!$ this ->httpClient ) {
45
+ $ this ->httpClient = Psr18ClientDiscovery::find ();
46
+ }
47
+
48
+ return $ this ->httpClient ;
49
+ }
50
+
51
+ private function getRequestFactory () : RequestFactoryInterface
52
+ {
53
+ if (!$ this ->requestFactory ) {
54
+ $ this ->requestFactory = Psr17FactoryDiscovery::findRequestFactory ();
55
+ }
56
+
57
+ return $ this ->requestFactory ;
58
+ }
59
+
60
+ private function getStreamFactory () : StreamFactoryInterface
61
+ {
62
+ if (!$ this ->streamFactory ) {
63
+ $ this ->streamFactory = Psr17FactoryDiscovery::findStreamFactory ();
64
+ }
65
+
66
+ return $ this ->streamFactory ;
33
67
}
34
68
35
69
/**
@@ -48,12 +82,13 @@ public function request(
48
82
$ body = null ,
49
83
$ protocolVersion = '1.1 '
50
84
) {
51
- $ request = $ this ->requestFactory
85
+ $ request = $ this ->getRequestFactory ()
52
86
->createRequest ($ method , $ uri )
53
87
->withProtocolVersion ($ protocolVersion );
54
88
55
89
if ($ body ) {
56
- $ request = $ request ->withBody ($ body );
90
+ $ stream = $ this ->getStreamFactory ()->createStream ($ body );
91
+ $ request = $ request ->withBody ($ stream );
57
92
}
58
93
59
94
foreach ($ headers as $ name => $ value ) {
@@ -71,7 +106,7 @@ public function request(
71
106
private function sendRequest (RequestInterface $ request )
72
107
{
73
108
try {
74
- return $ this ->httpClient ->sendRequest ($ request );
109
+ return $ this ->getHttpClient () ->sendRequest ($ request );
75
110
} catch (\Http \Client \Exception \NetworkException $ networkException ) {
76
111
throw new NetworkException ($ networkException ->getMessage (), $ request , $ networkException );
77
112
} catch (\Throwable $ exception ) {
0 commit comments