1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Resource base class. |
5
|
|
|
* |
6
|
|
|
* Abstract resource base class. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace zaporylie\Vipps\Resource; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
12
|
|
|
use Http\Client\Exception\HttpException; |
13
|
|
|
use Http\Client\HttpAsyncClient; |
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use JMS\Serializer\SerializerBuilder; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use zaporylie\Vipps\Exceptions\VippsException; |
18
|
|
|
use zaporylie\Vipps\VippsInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ResourceBase |
22
|
|
|
* @package Vipps\Resources |
23
|
|
|
*/ |
24
|
|
|
abstract class ResourceBase implements ResourceInterface, SerializableInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var VippsInterface |
29
|
|
|
*/ |
30
|
|
|
protected $app; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $headers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $body = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $id; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $path; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \zaporylie\Vipps\Resource\HttpMethod |
54
|
|
|
*/ |
55
|
|
|
protected $method; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \JMS\Serializer\Serializer |
59
|
|
|
*/ |
60
|
|
|
protected $serializer; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* AbstractResource constructor. |
64
|
|
|
* |
65
|
|
|
* @param \zaporylie\Vipps\VippsInterface $vipps |
66
|
|
|
* @param string $subscription_key |
67
|
|
|
*/ |
68
|
|
|
public function __construct(VippsInterface $vipps, $subscription_key) |
69
|
|
|
{ |
70
|
|
|
$this->app = $vipps; |
71
|
|
|
|
72
|
|
|
$this->headers['Ocp-Apim-Subscription-Key'] = $subscription_key; |
73
|
|
|
|
74
|
|
|
// Initiate serializer. |
75
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
76
|
|
|
$this->serializer = SerializerBuilder::create() |
77
|
|
|
->build(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets serializer value. |
82
|
|
|
* |
83
|
|
|
* @return \JMS\Serializer\Serializer |
84
|
|
|
*/ |
85
|
|
|
public function getSerializer() |
86
|
|
|
{ |
87
|
|
|
return $this->serializer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getHeaders() |
94
|
|
|
{ |
95
|
|
|
return $this->headers; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getMethod() |
102
|
|
|
{ |
103
|
|
|
if (!isset($this->method)) { |
104
|
|
|
throw new \LogicException('Missing HTTP method'); |
105
|
|
|
} |
106
|
|
|
return $this->method; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* |
112
|
|
|
* All occurrences of {id} pattern will be replaced with $this->id |
113
|
|
|
*/ |
114
|
|
|
public function getPath() |
115
|
|
|
{ |
116
|
|
|
if (!isset($this->path)) { |
117
|
|
|
throw new \LogicException('Missing resource path'); |
118
|
|
|
} |
119
|
|
|
// Get local var. |
120
|
|
|
$path = $this->path; |
121
|
|
|
// If ID is set replace {id} pattern with model's ID. |
122
|
|
|
if (isset($this->id)) { |
123
|
|
|
$path = str_replace('{id}', $this->id, $path); |
124
|
|
|
} |
125
|
|
|
return $path; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets path variable. |
130
|
|
|
* |
131
|
|
|
* @param string $path |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function setPath($path) |
136
|
|
|
{ |
137
|
|
|
$this->path = $path; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getBody() |
145
|
|
|
{ |
146
|
|
|
return $this->body; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $path |
151
|
|
|
* |
152
|
|
|
* @return \Psr\Http\Message\UriInterface |
153
|
|
|
*/ |
154
|
|
|
public function getUri($path) |
155
|
|
|
{ |
156
|
|
|
return $this->app->getClient()->getEndpoint()->getUri()->withPath($path); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
161
|
|
|
* |
162
|
|
|
* @throws \zaporylie\Vipps\Exceptions\VippsException |
163
|
|
|
*/ |
164
|
|
|
protected function makeCall() |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
|
|
$request = $this->getRequest(); |
168
|
|
|
$response = $this->handleRequest($request); |
169
|
|
|
} catch (HttpException $e) { |
170
|
|
|
// Catch exceptions thrown by http client. |
171
|
|
|
// We must do that in order to normalize output. |
172
|
|
|
$response = $e->getResponse(); |
173
|
|
|
} catch (\Exception $e) { |
174
|
|
|
// Something went really bad here. |
175
|
|
|
throw new VippsException($e->getMessage(), $e->getCode(), $e); |
176
|
|
|
} |
177
|
|
|
return $this->handleResponse($response); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
182
|
|
|
* |
183
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
184
|
|
|
*/ |
185
|
|
|
protected function handleRequest(RequestInterface $request) |
186
|
|
|
{ |
187
|
|
|
// Get client. |
188
|
|
|
$client = $this->app->getClient()->getHttpClient(); |
189
|
|
|
|
190
|
|
|
// Handle requests, sync precedence. |
191
|
|
|
if ($client instanceof HttpClient) { |
192
|
|
|
// Send sync request. |
193
|
|
|
$response = $client->sendRequest($request); |
194
|
|
|
} elseif ($client instanceof HttpAsyncClient) { |
195
|
|
|
// Send async request. |
196
|
|
|
$response = $client->sendAsyncRequest($request)->wait(); |
197
|
|
|
} else { |
198
|
|
|
throw new \LogicException('Unknown HTTP Client type: '. implode(',', class_implements($client))); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $response; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return \Psr\Http\Message\RequestInterface |
206
|
|
|
*/ |
207
|
|
|
protected function getRequest() |
208
|
|
|
{ |
209
|
|
|
return $this->app->getClient()->getMessageFactory()->createRequest( |
210
|
|
|
$this->getMethod(), |
211
|
|
|
$this->getUri($this->getPath()), |
212
|
|
|
$this->getHeaders(), |
213
|
|
|
$this->getBody() |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
219
|
|
|
* |
220
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
221
|
|
|
* |
222
|
|
|
* @throws \zaporylie\Vipps\Exceptions\VippsException |
223
|
|
|
*/ |
224
|
|
|
protected function handleResponse($response) |
225
|
|
|
{ |
226
|
|
|
// Handle request errors. |
227
|
|
|
if ($response->getStatusCode() >= 400) { |
228
|
|
|
throw VippsException::createFromResponse($response, $this->getSerializer()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// Sometimes VIPPS returns 200 with error message :/ They promised |
232
|
|
|
// to fix it but as a temporary fix we are gonna check if body is |
233
|
|
|
// "invalid" and throw exception in such a case. |
234
|
|
|
$exception = VippsException::createFromResponse($response, $this->getSerializer(), false); |
235
|
|
|
if ($exception instanceof VippsException) { |
236
|
|
|
throw $exception; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $response; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
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.