Skip to content

Commit 4df924c

Browse files
committed
factory
1 parent 8048a3e commit 4df924c

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

src/Http/ClientFactory.php

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Http\Discovery\Psr17FactoryDiscovery;
2525
use Psr\Http\Message\RequestFactoryInterface;
2626
use Psr\Http\Message\RequestInterface;
27+
use Psr\Http\Message\StreamFactoryInterface;
28+
use Psr\Http\Message\UriFactoryInterface;
2729
use Psr\Log\LoggerInterface;
2830
use Psr\Log\NullLogger;
2931
use Shapecode\FUT\Client\Api\CoreInterface;
@@ -38,6 +40,12 @@ class ClientFactory implements ClientFactoryInterface
3840
/** @var RequestFactoryInterface */
3941
protected $requestFactory;
4042

43+
/** @var StreamFactoryInterface */
44+
protected $streamFactory;
45+
46+
/** @var UriFactoryInterface */
47+
protected $urlFactory;
48+
4149
/** @var ConfigInterface */
4250
protected $config;
4351

@@ -51,14 +59,65 @@ class ClientFactory implements ClientFactoryInterface
5159

5260
public function __construct(
5361
ConfigInterface $config,
54-
?RequestFactoryInterface $requestFactory = null,
5562
?CookieJarBuilderInterface $cookieJarBuilder = null,
56-
?LoggerInterface $logger = null
63+
?LoggerInterface $logger = null,
64+
?RequestFactoryInterface $requestFactory = null,
65+
?StreamFactoryInterface $streamFactory = null,
66+
?UriFactoryInterface $urlFactory = null
5767
) {
5868
$this->config = $config;
59-
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
6069
$this->cookieJarBuilder = $cookieJarBuilder ?: new CookieJarBuilder();
6170
$this->logger = $logger ?: new NullLogger();
71+
72+
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
73+
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
74+
$this->urlFactory = $urlFactory ?: Psr17FactoryDiscovery::findUrlFactory();
75+
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function request(
81+
AccountInterface $account,
82+
string $method,
83+
string $url,
84+
array $options = [],
85+
array $plugins = []
86+
) : ClientCall {
87+
$headers = [];
88+
89+
if (isset($options['headers'])) {
90+
/** @var mixed[] $headers */
91+
$headers = $options['headers'];
92+
unset($options['headers']);
93+
}
94+
95+
$call = new ClientCall();
96+
97+
$plugins[] = new HeaderSetPlugin(CoreInterface::REQUEST_HEADERS);
98+
$plugins[] = new HeaderSetPlugin([
99+
'User-Agent' => $this->getConfig()->getUserAgent(),
100+
]);
101+
102+
if (count($headers) > 0) {
103+
$plugins[] = new HeaderSetPlugin($headers);
104+
}
105+
106+
$plugins[] = new ContentLengthPlugin();
107+
$plugins[] = new LoggerPlugin($this->logger);
108+
$stopwatch = new Stopwatch();
109+
$plugins[] = new StopwatchPlugin($stopwatch);
110+
$plugins[] = new ClientCallPlugin($call);
111+
$plugins[] = new RedirectPlugin();
112+
113+
$guzzle = $this->createAccountClient($account, $options);
114+
$client = $this->createPluginClient($guzzle, $plugins);
115+
116+
$request = $this->createRequest($method, $url);
117+
118+
$client->sendRequest($request);
119+
120+
return $call;
62121
}
63122

64123
/**
@@ -105,10 +164,11 @@ protected function createRequest(
105164
?string $body = null,
106165
array $headers = []
107166
) : RequestInterface {
108-
$request = $this->requestFactory->createRequest($method, $uri);
167+
$url = $this->urlFactory->createUri($uri);
168+
$request = $this->requestFactory->createRequest($method, $url);
109169

110170
if ($body !== null) {
111-
$stream = Psr17FactoryDiscovery::findStreamFactory()->createStream($body);
171+
$stream = $this->streamFactory->createStream($body);
112172
$request = $request->withBody($stream);
113173
}
114174

@@ -121,52 +181,6 @@ protected function createRequest(
121181
return $request;
122182
}
123183

124-
/**
125-
* @inheritdoc
126-
*/
127-
public function request(
128-
AccountInterface $account,
129-
string $method,
130-
string $url,
131-
array $options = [],
132-
array $plugins = []
133-
) : ClientCall {
134-
$headers = [];
135-
136-
if (isset($options['headers'])) {
137-
/** @var mixed[] $headers */
138-
$headers = $options['headers'];
139-
unset($options['headers']);
140-
}
141-
142-
$call = new ClientCall();
143-
144-
$plugins[] = new HeaderSetPlugin(CoreInterface::REQUEST_HEADERS);
145-
$plugins[] = new HeaderSetPlugin([
146-
'User-Agent' => $this->getConfig()->getUserAgent(),
147-
]);
148-
149-
if (count($headers) > 0) {
150-
$plugins[] = new HeaderSetPlugin($headers);
151-
}
152-
153-
$plugins[] = new ContentLengthPlugin();
154-
$plugins[] = new LoggerPlugin($this->logger);
155-
$stopwatch = new Stopwatch();
156-
$plugins[] = new StopwatchPlugin($stopwatch);
157-
$plugins[] = new ClientCallPlugin($call);
158-
$plugins[] = new RedirectPlugin();
159-
160-
$guzzle = $this->createAccountClient($account, $options);
161-
$client = $this->createPluginClient($guzzle, $plugins);
162-
163-
$request = $this->createRequest($method, $url);
164-
165-
$client->sendRequest($request);
166-
167-
return $call;
168-
}
169-
170184
protected function getConfig() : ConfigInterface
171185
{
172186
return $this->config;

0 commit comments

Comments
 (0)