-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientBuilder.php
More file actions
55 lines (45 loc) · 1.76 KB
/
Copy pathClientBuilder.php
File metadata and controls
55 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace Featurit\Client\HttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClientFactory;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
class ClientBuilder
{
private ClientInterface $httpClient;
private RequestFactoryInterface $requestFactoryInterface;
private StreamFactoryInterface $streamFactoryInterface;
private array $plugins = [];
/**
* @param ClientInterface|null $httpClient
* @param RequestFactoryInterface|null $requestFactoryInterface
* @param StreamFactoryInterface|null $streamFactoryInterface
*/
public function __construct(
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactoryInterface = null,
StreamFactoryInterface $streamFactoryInterface = null
) {
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->requestFactoryInterface = $requestFactoryInterface ?: Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactoryInterface = $streamFactoryInterface ?: Psr17FactoryDiscovery::findStreamFactory();
}
public function addPlugin(Plugin $plugin): void
{
$this->plugins[] = $plugin;
}
public function getHttpClient(): HttpMethodsClientInterface
{
$pluginClient = (new PluginClientFactory())->createClient($this->httpClient, $this->plugins);
return new HttpMethodsClient(
$pluginClient,
$this->requestFactoryInterface,
$this->streamFactoryInterface
);
}
}