1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zaporylie\Vipps; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpAsyncClient; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
use zaporylie\Vipps\Authentication\TokenMemoryCacheStorage; |
10
|
|
|
use zaporylie\Vipps\Authentication\TokenStorageInterface; |
11
|
|
|
use zaporylie\Vipps\Exceptions\Client\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
class Client implements ClientInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Http\Client\HttpClient|\Http\Client\HttpAsyncClient |
18
|
|
|
*/ |
19
|
|
|
protected $httpClient; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \zaporylie\Vipps\EndpointInterface |
23
|
|
|
*/ |
24
|
|
|
protected $endpoint; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Http\Message\MessageFactory |
28
|
|
|
*/ |
29
|
|
|
protected $messageFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $token; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $tokenType; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \zaporylie\Vipps\Authentication\TokenStorageInterface |
43
|
|
|
*/ |
44
|
|
|
protected $tokenStorage; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $clientId; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* VippsClient constructor. |
53
|
|
|
* |
54
|
|
|
* @param string $client_id |
55
|
|
|
* @param array $options |
56
|
|
|
*/ |
57
|
|
|
public function __construct($client_id, array $options = []) |
58
|
|
|
{ |
59
|
|
|
// Set Client ID. |
60
|
|
|
$this->setClientId($client_id); |
61
|
|
|
|
62
|
|
|
// Set or discover http client. |
63
|
|
|
$this->setHttpClient(isset($options['http_client']) ? $options['http_client'] : null); |
64
|
|
|
|
65
|
|
|
// Set endpoint or use default one. |
66
|
|
|
if (isset($options['endpoint'])) { |
67
|
|
|
$this->setEndpoint(call_user_func([ |
68
|
|
|
Endpoint::class, |
69
|
|
|
$options['endpoint'] |
70
|
|
|
])); |
71
|
|
|
} else { |
72
|
|
|
$this->setEndpoint(Endpoint::test()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Set custom token storage. If option is missing default in-memory |
76
|
|
|
// storage will be in use. |
77
|
|
|
$this->setTokenStorage( |
78
|
|
|
isset($options['token_storage']) |
79
|
|
|
? $options['token_storage'] |
80
|
|
|
: new TokenMemoryCacheStorage() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets token value. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getToken() |
90
|
|
|
{ |
91
|
|
|
if (!isset($this->token)) { |
92
|
|
|
throw new InvalidArgumentException('Missing Token'); |
93
|
|
|
} |
94
|
|
|
return $this->token; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets tokenStorage value. |
99
|
|
|
* |
100
|
|
|
* @return \zaporylie\Vipps\Authentication\TokenStorageInterface |
101
|
|
|
*/ |
102
|
|
|
public function getTokenStorage() |
103
|
|
|
{ |
104
|
|
|
return $this->tokenStorage; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets tokenStorage variable. |
109
|
|
|
* |
110
|
|
|
* @param \zaporylie\Vipps\Authentication\TokenStorageInterface $tokenStorage |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setTokenStorage(TokenStorageInterface $tokenStorage) |
115
|
|
|
{ |
116
|
|
|
$this->tokenStorage = $tokenStorage; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets clientId value. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getClientId() |
126
|
|
|
{ |
127
|
|
|
if (!isset($this->clientId)) { |
128
|
|
|
throw new InvalidArgumentException('Missing Client ID'); |
129
|
|
|
} |
130
|
|
|
return $this->clientId; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets clientId variable. |
135
|
|
|
* |
136
|
|
|
* @param string $clientId |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setClientId($clientId) |
141
|
|
|
{ |
142
|
|
|
$this->clientId = $clientId; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets connection value. |
148
|
|
|
* |
149
|
|
|
* @return \zaporylie\Vipps\EndpointInterface |
150
|
|
|
*/ |
151
|
|
|
public function getEndpoint() |
152
|
|
|
{ |
153
|
|
|
return $this->endpoint; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sets connection variable. |
158
|
|
|
* |
159
|
|
|
* @param \zaporylie\Vipps\EndpointInterface $endpoint |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function setEndpoint(EndpointInterface $endpoint) |
164
|
|
|
{ |
165
|
|
|
$this->endpoint = $endpoint; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets httpClient value. |
171
|
|
|
* |
172
|
|
|
* @return \Http\Client\HttpAsyncClient|\Http\Client\HttpClient |
173
|
|
|
*/ |
174
|
|
|
public function getHttpClient() |
175
|
|
|
{ |
176
|
|
|
return $this->httpClient; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets httpClient variable. |
181
|
|
|
* |
182
|
|
|
* @param \Http\Client\HttpAsyncClient|\Http\Client\HttpClient $httpClient |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setHttpClient($httpClient) |
187
|
|
|
{ |
188
|
|
|
$this->httpClient = self::httpClientDiscovery($httpClient); |
189
|
|
|
unset($this->messageFactory); |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Gets messageFactory value. |
195
|
|
|
* |
196
|
|
|
* @return \Http\Message\MessageFactory |
197
|
|
|
*/ |
198
|
|
|
public function getMessageFactory() |
199
|
|
|
{ |
200
|
|
|
if (!isset($this->messageFactory)) { |
201
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
202
|
|
|
} |
203
|
|
|
return $this->messageFactory; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Use this static method to get default HTTP Client. |
208
|
|
|
* |
209
|
|
|
* @param null|\Http\Client\HttpClient|\Http\Client\HttpAsyncClient $client |
210
|
|
|
* |
211
|
|
|
* @return \Http\Client\HttpClient|\Http\Client\HttpAsyncClient |
212
|
|
|
*/ |
213
|
|
|
protected function httpClientDiscovery($client = null) |
214
|
|
|
{ |
215
|
|
|
if (isset($client) && ($client instanceof HttpAsyncClient || $client instanceof HttpClient)) { |
216
|
|
|
return $client; |
217
|
|
|
} elseif (isset($client)) { |
218
|
|
|
throw new \LogicException(sprintf( |
219
|
|
|
'HttpClient must be instance of "%s" or "%s"', |
220
|
|
|
HttpClient::class, |
221
|
|
|
HttpAsyncClient::class |
222
|
|
|
)); |
223
|
|
|
} |
224
|
|
|
return HttpClientDiscovery::find(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|