1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\PhraseApp; |
11
|
|
|
|
12
|
|
|
use Http\Client\Common\Plugin; |
13
|
|
|
use Http\Client\Common\PluginClient; |
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Discovery\HttpClientDiscovery; |
16
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
17
|
|
|
use Http\Message\Authentication\BasicAuth; |
18
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configure an HTTP client. |
22
|
|
|
* |
23
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @internal This class should not be used outside of the API Client, it is not part of the BC promise. |
26
|
|
|
*/ |
27
|
|
|
final class HttpClientConfigurator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $endpoint = 'https://api.phraseapp.com'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var UriFactoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $uriFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var HttpClient |
41
|
|
|
*/ |
42
|
|
|
private $httpClient; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Plugin[] |
46
|
|
|
*/ |
47
|
|
|
private $prependPlugins = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Plugin[] |
51
|
|
|
*/ |
52
|
|
|
private $appendPlugins = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $token; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $token |
61
|
|
|
* @param HttpClient|null $httpClient |
62
|
|
|
* @param UriFactoryInterface|null $uriFactory |
63
|
|
|
*/ |
64
|
4 |
|
public function __construct(string $token, HttpClient $httpClient = null, UriFactoryInterface $uriFactory = null) |
65
|
|
|
{ |
66
|
4 |
|
$this->token = $token; |
67
|
4 |
|
$this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
68
|
4 |
|
$this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUrlFactory(); |
|
|
|
|
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return HttpClient |
73
|
|
|
*/ |
74
|
|
|
public function createConfiguredClient(): HttpClient |
75
|
|
|
{ |
76
|
|
|
$plugins = $this->prependPlugins; |
77
|
|
|
|
78
|
|
|
$plugins[] = new Plugin\AuthenticationPlugin(new BasicAuth($this->token, '')); |
79
|
|
|
$plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->endpoint)); |
80
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin([ |
81
|
|
|
'User-Agent' => 'FriendsOfApi/PhraseApp (https://github.com/FriendsOfApi/phraseapp)', |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $endpoint |
89
|
|
|
* |
90
|
|
|
* @return HttpClientConfigurator |
91
|
|
|
*/ |
92
|
|
|
public function setEndpoint(string $endpoint): HttpClientConfigurator |
93
|
|
|
{ |
94
|
|
|
$this->endpoint = $endpoint; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Plugin ...$plugin |
101
|
|
|
* |
102
|
|
|
* @return HttpClientConfigurator |
103
|
|
|
*/ |
104
|
2 |
|
public function appendPlugin(Plugin ...$plugin): HttpClientConfigurator |
105
|
|
|
{ |
106
|
2 |
|
foreach ($plugin as $p) { |
107
|
2 |
|
$this->appendPlugins[] = $p; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Plugin ...$plugin |
115
|
|
|
* |
116
|
|
|
* @return HttpClientConfigurator |
117
|
|
|
*/ |
118
|
2 |
|
public function prependPlugin(Plugin ...$plugin): HttpClientConfigurator |
119
|
|
|
{ |
120
|
2 |
|
$plugin = array_reverse($plugin); |
121
|
2 |
|
foreach ($plugin as $p) { |
122
|
2 |
|
array_unshift($this->prependPlugins, $p); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.