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