RawResponse::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace LE_ACME2\Connector;
4
5
class RawResponse {
6
7
    /** @var string */
8
    public $request;
9
10
    /** @var array */
11
    public $header;
12
13
    /** @var array|string */
14
    public $body;
15
16
    public static function createFrom(string $method, string $url, string $response, int $headerSize) : self {
17
18
        $result = new self();
19
20
        $header = substr($response, 0, $headerSize);
21
        $body = substr($response, $headerSize);
22
23
        $body_json = json_decode($body, true);
24
25
        $result->request = $method . ' ' . $url;
26
27
        $result->header = array_map(function($line) {
28
            return trim($line);
29
        }, explode("\n", $header));
30
31
        $result->body = $body_json === null ? $body : $body_json;
32
33
        return $result;
34
    }
35
36
    public function toString() : string {
37
38
        return serialize([
39
            'request' => $this->request,
40
            'header' => $this->header,
41
            'body' => $this->body,
42
        ]);
43
    }
44
45
    public static function getFromString(string $string) : self {
46
47
        $array = unserialize($string);
48
49
        $rawResponse = new self();
50
51
        $rawResponse->request = $array['request'];
52
        $rawResponse->header = $array['header'];
53
        $rawResponse->body = $array['body'];
54
55
        return $rawResponse;
56
    }
57
}