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\Fortnox; |
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\UriFactoryDiscovery; |
17
|
|
|
use Http\Message\UriFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Configure an HTTP client. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @internal this class should not be used outside of the API Client, it is not part of the BC promise |
25
|
|
|
*/ |
26
|
|
|
final class HttpClientConfigurator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $endpoint = 'https://api.fortnox.se'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $clientId; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $clientSecret; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $accessToken; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var UriFactory |
50
|
|
|
*/ |
51
|
|
|
private $uriFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var HttpClient |
55
|
|
|
*/ |
56
|
|
|
private $httpClient; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Plugin[] |
60
|
|
|
*/ |
61
|
|
|
private $prependPlugins = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Plugin[] |
65
|
|
|
*/ |
66
|
|
|
private $appendPlugins = []; |
67
|
|
|
|
68
|
4 |
|
public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null) |
69
|
|
|
{ |
70
|
4 |
|
$this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
71
|
4 |
|
$this->uriFactory = $uriFactory ?? UriFactoryDiscovery::find(); |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
public function createConfiguredClient(): HttpClient |
75
|
|
|
{ |
76
|
|
|
$plugins = $this->prependPlugins; |
77
|
|
|
|
78
|
|
|
$plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->endpoint)); |
79
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin([ |
80
|
|
|
'User-Agent' => 'FriendsOfApi/fortnox (https://github.com/FriendsOfApi/fortnox)', |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$extraHeaders = []; |
84
|
|
|
if (null !== $this->clientId) { |
85
|
|
|
$extraHeaders['Client-Id'] = $this->clientId; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (null !== $this->clientSecret) { |
89
|
|
|
$extraHeaders['Client-Secret'] = $this->clientSecret; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (null !== $this->accessToken) { |
93
|
|
|
$extraHeaders['Access-Token'] = $this->accessToken; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!empty($extraHeaders)) { |
97
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin($extraHeaders); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new PluginClient($this->httpClient, \array_merge($plugins, $this->appendPlugins)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setEndpoint(string $endpoint): self |
104
|
|
|
{ |
105
|
|
|
$this->endpoint = $endpoint; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setClientId(string $clientId): self |
111
|
|
|
{ |
112
|
|
|
$this->clientId = $clientId; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setClientSecret(string $clientSecret): self |
118
|
|
|
{ |
119
|
|
|
$this->clientSecret = $clientSecret; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setAccessToken(string $accessToken): self |
125
|
|
|
{ |
126
|
|
|
$this->accessToken = $accessToken; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
public function appendPlugin(Plugin ...$plugin): self |
132
|
|
|
{ |
133
|
2 |
|
foreach ($plugin as $p) { |
134
|
2 |
|
$this->appendPlugins[] = $p; |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
public function prependPlugin(Plugin ...$plugin): self |
141
|
|
|
{ |
142
|
2 |
|
$plugin = \array_reverse($plugin); |
143
|
2 |
|
foreach ($plugin as $p) { |
144
|
2 |
|
\array_unshift($this->prependPlugins, $p); |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|