1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\Pakkelabels; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
5
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
6
|
|
|
use GuzzleHttp\RequestOptions; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var GuzzleClientInterface |
14
|
|
|
*/ |
15
|
|
|
protected $httpClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This is the API username which you find/generate under Settings > API |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $username; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This is the API password which you find/generate under Settings > API |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $password; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This is the base url for the API |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $baseUrl = 'https://app.pakkelabels.dk/api/public/v3'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $defaultOptions; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ResponseInterface |
45
|
|
|
*/ |
46
|
|
|
private $lastResponse; |
47
|
|
|
|
48
|
15 |
|
public function __construct($username, $password) |
49
|
|
|
{ |
50
|
15 |
|
$this->username = $username; |
51
|
15 |
|
$this->password = $password; |
52
|
15 |
|
$this->defaultOptions = []; |
53
|
15 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $method |
57
|
|
|
* @param string $uri |
58
|
|
|
* @param array $options |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
3 |
|
public function doRequest($method, $uri, array $options = []) : array |
62
|
|
|
{ |
63
|
3 |
|
$optionsResolver = new OptionsResolver(); |
64
|
3 |
|
$this->configureOptions($optionsResolver); |
65
|
|
|
|
66
|
3 |
|
$url = $this->baseUrl . $uri; |
67
|
3 |
|
$options = $optionsResolver->resolve(array_replace($this->defaultOptions, $options)); |
68
|
3 |
|
$this->lastResponse = $this->getHttpClient()->request($method, $url, $options); |
69
|
|
|
try { |
70
|
3 |
|
$res = \GuzzleHttp\json_decode((string)$this->lastResponse->getBody(), true); |
71
|
3 |
|
} catch (\InvalidArgumentException $e) { |
72
|
3 |
|
$res = ['error' => '['.$this->lastResponse->getStatusCode().'] The response body was not correctly formatted JSON. Inspect the last response to figure out the reason for this.']; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $res; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/****************** |
79
|
|
|
* Helper methods * |
80
|
|
|
*****************/ |
81
|
9 |
|
/** |
82
|
|
|
* Returns the number of pages in the collection |
83
|
9 |
|
* Returns 0 if the header isn't set |
84
|
6 |
|
* |
85
|
|
|
* @return int |
86
|
9 |
|
*/ |
87
|
|
|
public function getPageCount() : int |
88
|
|
|
{ |
89
|
|
|
if($this->lastResponse && $this->lastResponse->getHeaderLine('X-Total-Pages')) { |
90
|
|
|
return (int)$this->lastResponse->getHeaderLine('X-Total-Pages'); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return 0; |
94
|
|
|
} |
95
|
3 |
|
|
96
|
3 |
|
/** |
97
|
|
|
* Returns the total item count in the collection |
98
|
|
|
* Returns 0 if the header isn't set |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
6 |
|
public function getItemCount() : int |
103
|
|
|
{ |
104
|
6 |
|
if($this->lastResponse && $this->lastResponse->getHeaderLine('X-Total-Count')) { |
105
|
|
|
return (int)$this->lastResponse->getHeaderLine('X-Total-Count'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
/** |
112
|
|
|
* @return GuzzleClientInterface |
113
|
3 |
|
*/ |
114
|
3 |
|
public function getHttpClient() : GuzzleClientInterface |
115
|
|
|
{ |
116
|
|
|
if (!$this->httpClient) { |
117
|
|
|
$this->httpClient = new GuzzleClient(); |
118
|
|
|
} |
119
|
|
|
return $this->httpClient; |
120
|
6 |
|
} |
121
|
|
|
|
122
|
6 |
|
/** |
123
|
|
|
* @param GuzzleClientInterface $httpClient |
124
|
|
|
* @return Client |
125
|
|
|
*/ |
126
|
|
|
public function setHttpClient(GuzzleClientInterface $httpClient) : self |
127
|
|
|
{ |
128
|
|
|
$this->httpClient = $httpClient; |
129
|
3 |
|
return $this; |
130
|
|
|
} |
131
|
3 |
|
|
132
|
3 |
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getUsername(): string |
136
|
|
|
{ |
137
|
|
|
return $this->username; |
138
|
3 |
|
} |
139
|
|
|
|
140
|
3 |
|
/** |
141
|
|
|
* @param string $username |
142
|
|
|
* @return Client |
143
|
|
|
*/ |
144
|
|
|
public function setUsername(string $username) : self |
145
|
|
|
{ |
146
|
|
|
$this->username = $username; |
147
|
3 |
|
return $this; |
148
|
|
|
} |
149
|
3 |
|
|
150
|
3 |
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getPassword(): string |
154
|
|
|
{ |
155
|
|
|
return $this->password; |
156
|
6 |
|
} |
157
|
|
|
|
158
|
6 |
|
/** |
159
|
|
|
* @param string $password |
160
|
|
|
* @return Client |
161
|
|
|
*/ |
162
|
|
|
public function setPassword(string $password) : self |
163
|
|
|
{ |
164
|
|
|
$this->password = $password; |
165
|
3 |
|
return $this; |
166
|
|
|
} |
167
|
3 |
|
|
168
|
3 |
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getBaseUrl() : string |
172
|
|
|
{ |
173
|
|
|
return $this->baseUrl; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $baseUrl |
178
|
|
|
* @return Client |
179
|
|
|
*/ |
180
|
|
|
public function setBaseUrl(string $baseUrl) : self |
181
|
|
|
{ |
182
|
|
|
$this->baseUrl = rtrim($baseUrl, '/'); |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
3 |
|
public function getDefaultOptions() : array |
190
|
|
|
{ |
191
|
|
|
return $this->defaultOptions; |
192
|
3 |
|
} |
193
|
3 |
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $options |
196
|
3 |
|
* @return Client |
197
|
3 |
|
*/ |
198
|
|
|
public function setDefaultOptions(array $options) : self |
199
|
3 |
|
{ |
200
|
|
|
$this->defaultOptions = $options; |
201
|
|
|
return $this; |
202
|
3 |
|
} |
203
|
3 |
|
|
204
|
|
|
/** |
205
|
|
|
* @return ResponseInterface |
206
|
3 |
|
*/ |
207
|
|
|
public function getLastResponse(): ResponseInterface |
208
|
|
|
{ |
209
|
|
|
return $this->lastResponse; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param ResponseInterface $lastResponse |
214
|
|
|
* @return Client |
215
|
|
|
*/ |
216
|
|
|
public function setLastResponse(ResponseInterface $lastResponse) : self |
217
|
|
|
{ |
218
|
|
|
$this->lastResponse = $lastResponse; |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function configureOptions(OptionsResolver $optionsResolver) |
223
|
|
|
{ |
224
|
|
|
// add request options from Guzzle |
225
|
|
|
$reflection = new \ReflectionClass(RequestOptions::class); |
226
|
|
|
$optionsResolver->setDefined($reflection->getConstants()); |
|
|
|
|
227
|
|
|
|
228
|
|
|
// set defaults |
229
|
|
|
$optionsResolver->setDefaults([ |
230
|
|
|
'allow_redirects' => false, |
231
|
|
|
'cookies' => false, |
232
|
|
|
'timeout' => 60, |
233
|
|
|
'http_errors' => false, |
234
|
|
|
'auth' => [ |
235
|
|
|
$this->username, |
236
|
|
|
$this->password |
237
|
|
|
] |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: