1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Remote\Psr; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use JsonException; |
7
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
8
|
|
|
use kalanis\UploadPerPartes\Responses; |
9
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
10
|
|
|
use kalanis\UploadPerPartes\UploadException; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use stdClass; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Response |
17
|
|
|
* @package kalanis\UploadPerPartes\Target\Remote\Psr |
18
|
|
|
* Process responses from PSR |
19
|
|
|
*/ |
20
|
|
|
class Response |
21
|
|
|
{ |
22
|
|
|
use TLang; |
23
|
|
|
|
24
|
|
|
protected Responses\Factory $responseFactory; |
25
|
|
|
|
26
|
28 |
|
public function __construct(Responses\Factory $responseFactory, ?Interfaces\IUppTranslations $lang = null) |
27
|
|
|
{ |
28
|
28 |
|
$this->responseFactory = $responseFactory; |
29
|
28 |
|
$this->setUppLang($lang); |
30
|
28 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ResponseInterface $response |
34
|
|
|
* @param string $clientData |
35
|
|
|
* @throws UploadException |
36
|
|
|
* @return Responses\BasicResponse |
37
|
|
|
*/ |
38
|
6 |
|
public function init(ResponseInterface $response, string $clientData): Responses\BasicResponse |
39
|
|
|
{ |
40
|
6 |
|
$parsed = $this->parseResponse($response); |
41
|
5 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
42
|
3 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_INIT); |
43
|
|
|
/** @var Responses\InitResponse $data */ |
44
|
3 |
|
return $data->setPassedInitData( |
45
|
3 |
|
strval($parsed->name ?? null), |
46
|
3 |
|
intval(max(0, $parsed->totalParts ?? 0)), |
47
|
3 |
|
intval(max(0,$parsed->lastKnownPart ?? 0)), |
48
|
3 |
|
intval(max(0, $parsed->partSize ?? 0)), |
49
|
3 |
|
strval($parsed->encoder ?? 'base64'), |
50
|
3 |
|
strval($parsed->check ?? 'md5') |
51
|
3 |
|
)->setBasics( |
52
|
3 |
|
strval($parsed->serverKey ?? ''), |
53
|
|
|
$clientData |
54
|
|
|
); |
55
|
|
|
} else { |
56
|
2 |
|
return $this->responseError( |
57
|
2 |
|
strval($parsed->message ?? ''), |
58
|
2 |
|
strval($parsed->serverKey ?? ''), |
59
|
|
|
$clientData |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ResponseInterface $response |
66
|
|
|
* @param string $clientData |
67
|
|
|
* @throws UploadException |
68
|
|
|
* @return Responses\BasicResponse |
69
|
|
|
*/ |
70
|
3 |
|
public function check(ResponseInterface $response, string $clientData): Responses\BasicResponse |
71
|
|
|
{ |
72
|
3 |
|
$parsed = $this->parseResponse($response); |
73
|
3 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
74
|
2 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_CHECK); |
75
|
|
|
/** @var Responses\CheckResponse $data */ |
76
|
2 |
|
return $data->setChecksum( |
77
|
2 |
|
strval($parsed->method ?? ''), |
78
|
2 |
|
strval($parsed->checksum ?? '') |
79
|
2 |
|
)->setBasics( |
80
|
2 |
|
strval($parsed->serverKey ?? ''), |
81
|
|
|
$clientData |
82
|
|
|
); |
83
|
|
|
} else { |
84
|
1 |
|
return $this->responseError( |
85
|
1 |
|
strval($parsed->message ?? ''), |
86
|
1 |
|
strval($parsed->serverKey ?? ''), |
87
|
|
|
$clientData |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ResponseInterface $response |
94
|
|
|
* @param string $clientData |
95
|
|
|
* @throws UploadException |
96
|
|
|
* @return Responses\BasicResponse |
97
|
|
|
*/ |
98
|
3 |
|
public function truncate(ResponseInterface $response, string $clientData): Responses\BasicResponse |
99
|
|
|
{ |
100
|
3 |
|
$parsed = $this->parseResponse($response); |
101
|
3 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
102
|
2 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_TRUNCATE); |
103
|
|
|
/** @var Responses\LastKnownResponse $data */ |
104
|
2 |
|
return $data->setLastKnown( |
105
|
2 |
|
intval(max(0, $parsed->lastKnown ?? 0)) |
106
|
2 |
|
)->setBasics( |
107
|
2 |
|
strval($parsed->serverKey ?? ''), |
108
|
|
|
$clientData |
109
|
|
|
); |
110
|
|
|
} else { |
111
|
1 |
|
return $this->responseError( |
112
|
1 |
|
strval($parsed->message ?? ''), |
113
|
1 |
|
strval($parsed->serverKey ?? ''), |
114
|
|
|
$clientData |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param ResponseInterface $response |
121
|
|
|
* @param string $clientData |
122
|
|
|
* @throws UploadException |
123
|
|
|
* @return Responses\BasicResponse |
124
|
|
|
*/ |
125
|
3 |
|
public function upload(ResponseInterface $response, string $clientData): Responses\BasicResponse |
126
|
|
|
{ |
127
|
3 |
|
$parsed = $this->parseResponse($response); |
128
|
3 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
129
|
2 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_UPLOAD); |
130
|
|
|
/** @var Responses\LastKnownResponse $data */ |
131
|
2 |
|
return $data->setLastKnown( |
132
|
2 |
|
intval(max(0, $parsed->lastKnown ?? 0)) |
133
|
2 |
|
)->setBasics( |
134
|
2 |
|
strval($parsed->serverKey ?? ''), |
135
|
|
|
$clientData |
136
|
|
|
); |
137
|
|
|
} else { |
138
|
1 |
|
return $this->responseError( |
139
|
1 |
|
strval($parsed->message ?? ''), |
140
|
1 |
|
strval($parsed->serverKey ?? ''), |
141
|
|
|
$clientData |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param ResponseInterface $response |
148
|
|
|
* @param string $clientData |
149
|
|
|
* @throws UploadException |
150
|
|
|
* @return Responses\BasicResponse |
151
|
|
|
*/ |
152
|
3 |
|
public function done(ResponseInterface $response, string $clientData): Responses\BasicResponse |
153
|
|
|
{ |
154
|
3 |
|
$parsed = $this->parseResponse($response); |
155
|
3 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
156
|
2 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_DONE); |
157
|
|
|
/** @var Responses\DoneResponse $data */ |
158
|
2 |
|
return $data->setFinalName( |
159
|
2 |
|
strval($parsed->name ?? '') |
160
|
2 |
|
)->setBasics( |
161
|
2 |
|
strval($parsed->serverKey ?? ''), |
162
|
|
|
$clientData |
163
|
|
|
); |
164
|
|
|
} else { |
165
|
1 |
|
return $this->responseError( |
166
|
1 |
|
strval($parsed->message ?? ''), |
167
|
1 |
|
strval($parsed->serverKey ?? ''), |
168
|
|
|
$clientData |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param ResponseInterface $response |
175
|
|
|
* @param string $clientData |
176
|
|
|
* @throws UploadException |
177
|
|
|
* @return Responses\BasicResponse |
178
|
|
|
*/ |
179
|
3 |
|
public function cancel(ResponseInterface $response, string $clientData): Responses\BasicResponse |
180
|
|
|
{ |
181
|
3 |
|
$parsed = $this->parseResponse($response); |
182
|
3 |
|
if (Responses\BasicResponse::STATUS_OK == $parsed->status) { |
183
|
2 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_CANCEL); |
184
|
2 |
|
return $data->setBasics( |
185
|
2 |
|
strval($parsed->serverKey ?? ''), |
186
|
|
|
$clientData |
187
|
|
|
); |
188
|
|
|
} else { |
189
|
1 |
|
return $this->responseError( |
190
|
1 |
|
strval($parsed->message ?? ''), |
191
|
1 |
|
strval($parsed->serverKey ?? ''), |
192
|
|
|
$clientData |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $message |
199
|
|
|
* @param string $serverKey |
200
|
|
|
* @param string $clientData |
201
|
|
|
* @throws UploadException |
202
|
|
|
* @return Responses\BasicResponse |
203
|
|
|
*/ |
204
|
7 |
|
protected function responseError(string $message, string $serverKey, string $clientData): Responses\BasicResponse |
205
|
|
|
{ |
206
|
7 |
|
$data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_ERROR); |
207
|
|
|
/** @var Responses\ErrorResponse $data */ |
208
|
7 |
|
return $data->setErrorMessage($message)->setBasics($serverKey, $clientData); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param ResponseInterface $response |
213
|
|
|
* @throws UploadException |
214
|
|
|
* @return stdClass |
215
|
|
|
*/ |
216
|
21 |
|
protected function parseResponse(ResponseInterface $response): stdClass |
217
|
|
|
{ |
218
|
|
|
try { |
219
|
21 |
|
$body = $response->getBody(); |
220
|
21 |
|
$body->rewind(); |
221
|
21 |
|
$parsed = json_decode($body->getContents(), false, 2, JSON_THROW_ON_ERROR); |
222
|
1 |
|
} catch (JsonException $ex) { |
223
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
224
|
|
|
} |
225
|
20 |
|
if (!$parsed instanceof stdClass) { |
226
|
|
|
// @codeCoverageIgnoreStart |
227
|
|
|
// this one is on phpstan |
228
|
|
|
throw new UploadException($this->getUppLang()->uppBadResponse('')); |
229
|
|
|
} |
230
|
|
|
// @codeCoverageIgnoreEnd |
231
|
20 |
|
return $parsed; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|