1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Pnz\MattermostClient; |
||||
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\Psr17FactoryDiscovery; |
||||
12 | use Http\Message\Authentication; |
||||
13 | use Pnz\MattermostClient\Authentication\MattermostAuthentication; |
||||
14 | use Psr\Http\Message\RequestFactoryInterface; |
||||
15 | use Psr\Http\Message\StreamFactoryInterface; |
||||
16 | use Psr\Http\Message\UriFactoryInterface; |
||||
17 | use Psr\Http\Message\UriInterface; |
||||
18 | |||||
19 | /** |
||||
20 | * Configure an HTTP client. |
||||
21 | * |
||||
22 | * @internal this class should not be used outside of the API Client, it is not part of the BC promise |
||||
23 | */ |
||||
24 | final class HttpClientConfigurator |
||||
25 | { |
||||
26 | /** |
||||
27 | * @var string |
||||
28 | */ |
||||
29 | private $endpoint; |
||||
30 | |||||
31 | /** |
||||
32 | * @var string |
||||
33 | */ |
||||
34 | private $token; |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
35 | |||||
36 | /** |
||||
37 | * @var string |
||||
38 | */ |
||||
39 | private $loginId; |
||||
40 | |||||
41 | /** |
||||
42 | * @var string |
||||
43 | */ |
||||
44 | private $password; |
||||
45 | |||||
46 | /** |
||||
47 | * @var HttpClient |
||||
48 | */ |
||||
49 | private $httpClient; |
||||
50 | |||||
51 | /** |
||||
52 | * @var UriFactoryInterface |
||||
53 | */ |
||||
54 | private $uriFactory; |
||||
55 | |||||
56 | /** |
||||
57 | * @var RequestFactoryInterface |
||||
58 | */ |
||||
59 | private $requestFactory; |
||||
60 | |||||
61 | /** |
||||
62 | * @var StreamFactoryInterface |
||||
63 | */ |
||||
64 | private $streamFactory; |
||||
65 | |||||
66 | /** |
||||
67 | * @var Plugin[] |
||||
68 | */ |
||||
69 | private $prependPlugins = []; |
||||
70 | |||||
71 | /** |
||||
72 | * @var Plugin[] |
||||
73 | */ |
||||
74 | private $appendPlugins = []; |
||||
75 | |||||
76 | public function __construct( |
||||
77 | HttpClient $httpClient = null, |
||||
78 | UriFactoryInterface $uriFactory = null, |
||||
79 | RequestFactoryInterface $requestFactory = null, |
||||
80 | StreamFactoryInterface $streamFactory = null |
||||
81 | ) { |
||||
82 | $this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
||||
83 | $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUrlFactory(); |
||||
0 ignored issues
–
show
The function
Http\Discovery\Psr17Fact...overy::findUrlFactory() has been deprecated: This will be removed in 2.0. Consider using the findUriFactory() method.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.
Loading history...
|
|||||
84 | $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
||||
85 | $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); |
||||
86 | } |
||||
87 | |||||
88 | public function createConfiguredClient(): HttpClient |
||||
89 | { |
||||
90 | if (empty($this->endpoint)) { |
||||
91 | throw new \InvalidArgumentException('Unable to configure the client, no API Endpoint provided'); |
||||
92 | } |
||||
93 | |||||
94 | if (empty($this->loginId) || empty($this->password)) { |
||||
95 | throw new \InvalidArgumentException('Unable to configure the client, no LoginId or Password provided'); |
||||
96 | } |
||||
97 | |||||
98 | $baseUri = $this->uriFactory->createUri($this->endpoint); |
||||
99 | $plugins = $this->prependPlugins; |
||||
100 | |||||
101 | $plugins[] = new Plugin\BaseUriPlugin($baseUri); |
||||
102 | $plugins[] = new Plugin\AuthenticationPlugin($this->createAuthentication($baseUri)); |
||||
103 | |||||
104 | return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins)); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * @return HttpClientConfigurator |
||||
109 | */ |
||||
110 | public function setEndpoint(string $endpoint): self |
||||
111 | { |
||||
112 | $this->endpoint = $endpoint; |
||||
113 | |||||
114 | return $this; |
||||
115 | } |
||||
116 | |||||
117 | /** |
||||
118 | * Set the LoginId/password to be used during the authentication. |
||||
119 | * |
||||
120 | * @return HttpClientConfigurator |
||||
121 | */ |
||||
122 | public function setCredentials(string $loginId, string $password): self |
||||
123 | { |
||||
124 | if (empty($loginId) || empty($password)) { |
||||
125 | throw new \InvalidArgumentException('LoginId and Password cannot be empty'); |
||||
126 | } |
||||
127 | |||||
128 | $this->loginId = $loginId; |
||||
129 | $this->password = $password; |
||||
130 | |||||
131 | return $this; |
||||
132 | } |
||||
133 | |||||
134 | public function appendPlugin(Plugin ...$plugin): self |
||||
135 | { |
||||
136 | foreach ($plugin as $p) { |
||||
137 | $this->appendPlugins[] = $p; |
||||
138 | } |
||||
139 | |||||
140 | return $this; |
||||
141 | } |
||||
142 | |||||
143 | public function prependPlugin(Plugin ...$plugin): self |
||||
144 | { |
||||
145 | $plugin = array_reverse($plugin); |
||||
146 | foreach ($plugin as $p) { |
||||
147 | array_unshift($this->prependPlugins, $p); |
||||
148 | } |
||||
149 | |||||
150 | return $this; |
||||
151 | } |
||||
152 | |||||
153 | private function createAuthentication(UriInterface $baseUri): MattermostAuthentication |
||||
154 | { |
||||
155 | $authClient = new PluginClient($this->httpClient, array_merge($this->prependPlugins, [ |
||||
156 | new Plugin\BaseUriPlugin($baseUri), |
||||
157 | ])); |
||||
158 | |||||
159 | return new MattermostAuthentication($this->loginId, $this->password, $authClient, $this->requestFactory, $this->streamFactory); |
||||
160 | } |
||||
161 | } |
||||
162 |