|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AdamPaterson\ApiClient\Foundation\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Http\Client\Common\Plugin; |
|
8
|
|
|
use Http\Client\Common\PluginClient; |
|
9
|
|
|
use Http\Client\HttpClient; |
|
10
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
11
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
12
|
|
|
use Http\Message\UriFactory; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ClientBuilder |
|
16
|
|
|
* |
|
17
|
|
|
* @package AdamPaterson\ApiClient\Foundation\Http |
|
18
|
|
|
*/ |
|
19
|
|
|
class ClientBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Http\Client\HttpClient |
|
23
|
|
|
*/ |
|
24
|
|
|
private $httpClient; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Http\Message\UriFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $uriFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $prependPlugins = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $appendPlugins = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* ClientBuilder constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Http\Client\HttpClient|null $httpClient |
|
45
|
|
|
* @param \Http\Message\UriFactory|null $uriFactory |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null) |
|
48
|
|
|
{ |
|
49
|
9 |
|
$this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
|
50
|
9 |
|
$this->uriFactory = $uriFactory ?? UriFactoryDiscovery::find(); |
|
51
|
9 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return \Http\Client\HttpClient |
|
55
|
|
|
*/ |
|
56
|
6 |
|
public function createConfiguredClient(): HttpClient |
|
57
|
|
|
{ |
|
58
|
6 |
|
$plugins = $this->prependPlugins; |
|
59
|
|
|
|
|
60
|
6 |
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param \Http\Client\Common\Plugin[] ...$plugin |
|
65
|
|
|
* |
|
66
|
|
|
* @return \AdamPaterson\ApiClient\Foundation\Http\ClientBuilder |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function appendPlugin(Plugin ...$plugin): ClientBuilder |
|
69
|
|
|
{ |
|
70
|
3 |
|
foreach ($plugin as $p) { |
|
71
|
3 |
|
$this->appendPlugins[] = $p; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \Http\Client\Common\Plugin[] ...$plugin |
|
79
|
|
|
* |
|
80
|
|
|
* @return \AdamPaterson\ApiClient\Foundation\Http\ClientBuilder |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function prependPlugin(Plugin ...$plugin): ClientBuilder |
|
83
|
|
|
{ |
|
84
|
3 |
|
$plugin = array_reverse($plugin); |
|
85
|
3 |
|
foreach ($plugin as $p) { |
|
86
|
3 |
|
array_unshift($this->prependPlugins, $p); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|