88use Omnipay \Common \Http \Exception \RequestException ;
99use Psr \Http \Message \RequestInterface ;
1010use Psr \Http \Message \ResponseInterface ;
11+ use Psr \Http \Message \StreamFactoryInterface ;
1112use 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 ;
1416
1517final class Client implements ClientInterface
1618{
1719 /**
1820 * The Http Client which implements `public function sendRequest(RequestInterface $request)`
1921 *
20- * @var Psr18ClientInterface
22+ * @var HttpClientInterface
2123 */
2224 private $ httpClient ;
2325
2426 /**
25- * @var Psr17RequestFactoryInterface
27+ * @var RequestFactoryInterface
2628 */
2729 private $ requestFactory ;
2830
31+ /**
32+ * @var StreamFactoryInterface
33+ */
34+ private $ streamFactory ;
35+
2936 public function __construct ($ httpClient = null , $ requestFactory = null )
3037 {
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 ;
3367 }
3468
3569 /**
@@ -48,12 +82,13 @@ public function request(
4882 $ body = null ,
4983 $ protocolVersion = '1.1 '
5084 ) {
51- $ request = $ this ->requestFactory
85+ $ request = $ this ->getRequestFactory ()
5286 ->createRequest ($ method , $ uri )
5387 ->withProtocolVersion ($ protocolVersion );
5488
5589 if ($ body ) {
56- $ request = $ request ->withBody ($ body );
90+ $ stream = $ this ->getStreamFactory ()->createStream ($ body );
91+ $ request = $ request ->withBody ($ stream );
5792 }
5893
5994 foreach ($ headers as $ name => $ value ) {
@@ -71,7 +106,7 @@ public function request(
71106 private function sendRequest (RequestInterface $ request )
72107 {
73108 try {
74- return $ this ->httpClient ->sendRequest ($ request );
109+ return $ this ->getHttpClient () ->sendRequest ($ request );
75110 } catch (\Http \Client \Exception \NetworkException $ networkException ) {
76111 throw new NetworkException ($ networkException ->getMessage (), $ request , $ networkException );
77112 } catch (\Throwable $ exception ) {
0 commit comments