Skip to content

Commit 8443ce1

Browse files
committed
tidy: code reuse
1 parent f01da1e commit 8443ce1

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

src/Response.php

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* @property-read UriInterface $url
2525
* @SuppressWarnings("UnusedPrivateMethod")
2626
* @SuppressWarnings("TooManyPublicMethods")
27+
* @SuppressWarnings("TooManyMethods")
28+
* @SuppressWarnings("ExcessiveClassComplexity")
2729
*/
2830
class Response implements ResponseInterface {
2931
use Message;
@@ -196,11 +198,7 @@ public function endDeferredResponse(?string $integrity = null):void {
196198
public function arrayBuffer():Promise {
197199
$promise = $this->getPromise();
198200
$promise->then(function(string $responseText) {
199-
$bytes = strlen($responseText);
200-
$arrayBuffer = new ArrayBuffer($bytes);
201-
for($i = 0; $i < $bytes; $i++) {
202-
$arrayBuffer->offsetSet($i, ord($responseText[$i]));
203-
}
201+
$arrayBuffer = $this->arrayBufferFromResponseText($responseText);
204202

205203
$this->deferred->resolve($arrayBuffer);
206204
});
@@ -210,12 +208,8 @@ public function arrayBuffer():Promise {
210208

211209
public function awaitArrayBuffer():ArrayBuffer {
212210
$arrayBuffer = null;
213-
if($bodyText = $this->getBody()->getContents()) {
214-
$bytes = strlen($bodyText);
215-
$arrayBuffer = new ArrayBuffer($bytes);
216-
for($i = 0; $i < $bytes; $i++) {
217-
$arrayBuffer->offsetSet($i, ord($bodyText[$i]));
218-
}
211+
if($responseText = $this->getBody()->getContents()) {
212+
$arrayBuffer = $this->arrayBufferFromResponseText($responseText);
219213
}
220214

221215
$this->arrayBuffer()->then(function(ArrayBuffer $resolved) use(&$arrayBuffer) {
@@ -225,6 +219,14 @@ public function awaitArrayBuffer():ArrayBuffer {
225219
return $arrayBuffer;
226220
}
227221

222+
private function arrayBufferFromResponseText(string $responseText):ArrayBuffer {
223+
$bytes = strlen($responseText);
224+
$arrayBuffer = new ArrayBuffer($bytes);
225+
for($i = 0; $i < $bytes; $i++) {
226+
$arrayBuffer->offsetSet($i, ord($responseText[$i]));
227+
}
228+
}
229+
228230
/**
229231
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
230232
* as a Gt\Http\Blob.
@@ -235,22 +237,17 @@ public function awaitArrayBuffer():ArrayBuffer {
235237
public function blob():Promise {
236238
$promise = $this->getPromise();
237239
$promise->then(function(string $responseText) {
238-
$blobOptions = [
239-
"type" => $this->getResponseHeaders()->get("content-type")?->getValues()[0],
240-
];
241-
$this->deferred->resolve(new Blob([$responseText], $blobOptions));
240+
$blob = $this->blobFromResponseText($responseText);
241+
$this->deferred->resolve($blob);
242242
});
243243

244244
return $promise;
245245
}
246246

247247
public function awaitBlob():Blob {
248248
$blob = null;
249-
if($bodyText = $this->getBody()->getContents()) {
250-
$blobOptions = [
251-
"type" => $this->getResponseHeaders()->get("content-type")?->getValues()[0],
252-
];
253-
$blob = new Blob([$bodyText], $blobOptions);
249+
if($responseText = $this->getBody()->getContents()) {
250+
$blob = $this->blobFromResponseText($responseText);
254251
}
255252

256253
$this->blob()->then(function(Blob $resolved) use(&$blob) {
@@ -260,6 +257,13 @@ public function awaitBlob():Blob {
260257
return $blob;
261258
}
262259

260+
private function blobFromResponseText(string $responseText):Blob {
261+
$blobOptions = [
262+
"type" => $this->getResponseHeaders()->get("content-type")?->getValues()[0],
263+
];
264+
return new Blob([$responseText], $blobOptions);
265+
}
266+
263267
/**
264268
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
265269
* as a Gt\Http\FormData.
@@ -272,16 +276,9 @@ public function formData():Promise {
272276
$newPromise = $newDeferred->getPromise();
273277

274278
$deferredPromise = $this->getPromise();
275-
$deferredPromise->then(function(string $resolvedValue)
279+
$deferredPromise->then(function(string $responseText)
276280
use($newDeferred) {
277-
parse_str($resolvedValue, $bodyData);
278-
$formData = new FormData();
279-
foreach($bodyData as $key => $value) {
280-
if(is_array($value)) {
281-
$value = implode(",", $value);
282-
}
283-
$formData->set((string)$key, (string)$value);
284-
}
281+
$formData = $this->formDataFromResponseText($responseText);
285282
$newDeferred->resolve($formData);
286283
});
287284

@@ -290,15 +287,8 @@ public function formData():Promise {
290287

291288
public function awaitFormData():FormData {
292289
$formData = null;
293-
if($bodyText = $this->getBody()->getContents()) {
294-
parse_str($bodyText, $bodyData);
295-
$formData = new FormData();
296-
foreach($bodyData as $key => $value) {
297-
if(is_array($value)) {
298-
$value = implode(",", $value);
299-
}
300-
$formData->set((string)$key, (string)$value);
301-
}
290+
if($responseText = $this->getBody()->getContents()) {
291+
$formData = $this->formDataFromResponseText($responseText);
302292
}
303293

304294
$this->blob()->then(function(FormData $resolved) use(&$formData) {
@@ -308,6 +298,18 @@ public function awaitFormData():FormData {
308298
return $formData;
309299
}
310300

301+
private function formDataFromResponseText(string $responseText):FormData {
302+
parse_str($responseText, $bodyData);
303+
$formData = new FormData();
304+
foreach($bodyData as $key => $value) {
305+
if(is_array($value)) {
306+
$value = implode(",", $value);
307+
}
308+
$formData->set((string)$key, (string)$value);
309+
}
310+
return $formData;
311+
}
312+
311313
/**
312314
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
313315
* as a Gt\Json\JsonObject.
@@ -320,8 +322,7 @@ public function awaitFormData():FormData {
320322
public function json(int $depth = 512, int $options = 0):Promise {
321323
$promise = $this->getPromise();
322324
$promise->then(function(string $responseText)use($depth, $options) {
323-
$builder = new JsonObjectBuilder($depth, $options);
324-
$json = $builder->fromJsonString($responseText);
325+
$json = $this->jsonFromResponseText($responseText, $depth, $options);
325326
$this->deferred->resolve($json);
326327
});
327328

@@ -331,9 +332,8 @@ public function json(int $depth = 512, int $options = 0):Promise {
331332
/** @param int<1, max> $depth */
332333
public function awaitJson(int $depth = 512, int $options = 0):JsonObject {
333334
$jsonObject = null;
334-
if($bodyText = $this->getBody()->getContents()) {
335-
$builder = new JsonObjectBuilder();
336-
$jsonObject = $builder->fromJsonString($bodyText);
335+
if($responseText = $this->getBody()->getContents()) {
336+
$jsonObject = $this->jsonFromResponseText($responseText);
337337
}
338338

339339
$this->json($depth, $options)->then(function(JsonObject $resolved) use(&$jsonObject) {
@@ -343,6 +343,11 @@ public function awaitJson(int $depth = 512, int $options = 0):JsonObject {
343343
return $jsonObject;
344344
}
345345

346+
private function jsonFromResponseText(string $responseText, int $depth = 512, int $options = 0):JsonObject {
347+
$builder = new JsonObjectBuilder($depth, $options);
348+
return $builder->fromJsonString($responseText);
349+
}
350+
346351
/**
347352
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
348353
* as a string.

0 commit comments

Comments
 (0)