1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Resource base class. |
5
|
|
|
* |
6
|
|
|
* Abstract resource base class. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Vipps\Resource; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
12
|
|
|
use Http\Client\HttpAsyncClient; |
13
|
|
|
use Http\Client\HttpClient; |
14
|
|
|
use JMS\Serializer\SerializerBuilder; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Vipps\Exceptions\ViPPSErrorException; |
17
|
|
|
use Vipps\Exceptions\VippsException; |
18
|
|
|
use Vipps\VippsInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ResourceBase |
22
|
|
|
* @package Vipps\Resources |
23
|
|
|
*/ |
24
|
|
|
abstract class ResourceBase implements ResourceInterface |
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 \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 \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
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getBody() |
132
|
|
|
{ |
133
|
|
|
return $this->body; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $path |
138
|
|
|
* |
139
|
|
|
* @return \Psr\Http\Message\UriInterface |
140
|
|
|
*/ |
141
|
|
|
public function getUri($path) |
142
|
|
|
{ |
143
|
|
|
return $this->app->getClient()->getEndpoint()->getUri()->withPath($path); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
148
|
|
|
* |
149
|
|
|
* @throws \Vipps\Exceptions\VippsException |
150
|
|
|
*/ |
151
|
|
|
protected function makeCall() |
152
|
|
|
{ |
153
|
|
|
/** @var RequestInterface $request */ |
154
|
|
|
$request = $this->app->getClient()->getMessageFactory()->createRequest( |
155
|
|
|
$this->getMethod(), |
156
|
|
|
$this->getUri($this->getPath()), |
157
|
|
|
$this->getHeaders(), |
158
|
|
|
$this->getBody() |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
// Handle requests, sync precedence. |
162
|
|
|
$client = $response = $this->app->getClient()->getHttpClient(); |
163
|
|
|
if ($client instanceof HttpClient) { |
164
|
|
|
$response = $client->sendRequest($request); |
165
|
|
|
} elseif ($client instanceof HttpAsyncClient) { |
166
|
|
|
$response = $client->sendAsyncRequest($request)->wait(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// @todo: Handle error. |
170
|
|
|
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) { |
171
|
|
|
$error = $response->getBody()->getContents(); |
172
|
|
|
throw new VippsException($error, $response->getStatusCode()); |
173
|
|
|
} elseif ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { |
174
|
|
|
throw new VippsException($response->getReasonPhrase(), $response->getStatusCode()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $response; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
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.