1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gnumoksha\FreeIpa\Infra\Rpc; |
6
|
|
|
|
7
|
|
|
use Gnumoksha\FreeIpa\Infra\Rpc\Response\BodyBuilder as ResponseBodyBuilder; |
8
|
|
|
use Gnumoksha\FreeIpa\Infra\Rpc\Response\CommonBodyBuilder; |
9
|
|
|
use Gnumoksha\FreeIpa\Options; |
10
|
|
|
use Http\Client\Common\Plugin; |
11
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
12
|
|
|
use Http\Client\Common\Plugin\CookiePlugin; |
13
|
|
|
use Http\Client\Common\Plugin\HeaderSetPlugin; |
14
|
|
|
use Http\Client\Common\PluginClient; |
15
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
16
|
|
|
use Http\Message\CookieJar; |
17
|
|
|
use Psr\Http\Client\ClientInterface; |
18
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Builds a HTTP RPC Client using HTTP-Plug plugins. |
23
|
|
|
*/ |
24
|
|
|
class PluginClientBuilder implements ClientBuilder |
25
|
|
|
{ |
26
|
|
|
/** @var \Gnumoksha\FreeIpa\Options */ |
27
|
|
|
private $options; |
28
|
|
|
/** @var \Psr\Http\Client\ClientInterface|null */ |
29
|
|
|
private $psrHttpClient; |
30
|
|
|
/** @var \Psr\Http\Message\UriFactoryInterface */ |
31
|
|
|
private $uriFactory; |
32
|
|
|
/** @var \Gnumoksha\FreeIpa\Infra\Rpc\Response\BodyBuilder */ |
33
|
|
|
private $responseBodyBuilder; |
34
|
|
|
/** @var \Http\Client\Common\Plugin[] */ |
35
|
|
|
private $httpClientPlugins; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
Options $options, |
39
|
|
|
ClientInterface $psrHttpClient = null, |
40
|
|
|
?UriFactoryInterface $uriFactory = null, |
41
|
|
|
?ResponseBodyBuilder $responseBodyBuilder = null |
42
|
|
|
) { |
43
|
|
|
$this->options = $options; |
44
|
|
|
$this->psrHttpClient = $psrHttpClient; |
45
|
|
|
$this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUrlFactory(); |
46
|
|
|
$this->responseBodyBuilder = $responseBodyBuilder ?? new CommonBodyBuilder(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function build(): Client |
50
|
|
|
{ |
51
|
|
|
return new Client( |
52
|
|
|
$this->options, |
53
|
|
|
$this->buildPluginClient(), |
54
|
|
|
Psr17FactoryDiscovery::findRequestFactory(), |
55
|
|
|
$this->responseBodyBuilder |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @see https://access.redhat.com/articles/2728021#end-points documentation |
61
|
|
|
*/ |
62
|
|
|
private function buildPluginClient(): PluginClient |
63
|
|
|
{ |
64
|
|
|
$cookieJar = new CookieJar(); |
65
|
|
|
$this->addHttpClientPlugin(new HeaderSetPlugin([ |
66
|
|
|
'Referer' => $this->options->getPrimaryUrl(), |
67
|
|
|
'User-Agent' => 'php-FreeIPA', |
68
|
|
|
])); |
69
|
|
|
$this->addHttpClientPlugin(new BaseUriPlugin($this->uriFactory->createUri($this->options->getPrimaryUrl()))); |
70
|
|
|
$this->addHttpClientPlugin(new CookiePlugin($cookieJar)); |
71
|
|
|
|
72
|
|
|
$psrHttpClient = $this->psrHttpClient; |
73
|
|
|
if ($psrHttpClient === null) { |
74
|
|
|
// #TODO setup docs.php-http.org/en/latest/clients/socket-client.html too |
75
|
|
|
if (!class_exists('\Http\Client\Curl\Client')) { |
76
|
|
|
throw new RuntimeException('Specify a HTTP client or install php-http/curl-client'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$psrHttpClient = new \Http\Client\Curl\Client( |
80
|
|
|
null, |
81
|
|
|
null, |
82
|
|
|
[ |
83
|
|
|
CURLOPT_CAINFO => $this->options->getCertificatePath(), |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return new PluginClient($psrHttpClient, $this->httpClientPlugins); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function addHttpClientPlugin(Plugin $plugin): self |
92
|
|
|
{ |
93
|
|
|
$this->httpClientPlugins[] = $plugin; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|