Passed
Pull Request — master (#7)
by Sandro
02:16
created

HttpHelper::parseMessage()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 9.3554
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 5
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2019 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb;
13
14
use ArangoDb\Http\JsonStream;
15
use ArangoDb\Util\Json;
16
use Psr\Http\Message\ResponseInterface;
17
18
final class HttpHelper
19
{
20
    /**
21
     * Separator between header and body
22
     */
23
    private const BODY_SEPARATOR = "\r\n\r\n";
24
25
    /**
26
     * Splits the message in HTTP status code, headers and body.
27
     *
28
     * @param string $message
29
     * @return array Values are HTTP status code, PSR-7 headers and body
30
     */
31 39
    public static function parseMessage(string $message): array
32
    {
33 39
        $startLine = null;
34 39
        $headers = [];
35 39
        [$headerLines, $body] = explode(self::BODY_SEPARATOR, $message, 2);
36 39
        $headerLines = explode("\n", $headerLines);
37
38 39
        foreach ($headerLines as $header) {
39
            // Parse message headers
40 39
            if ($startLine === null) {
41 39
                $startLine = explode(' ', $header, 3);
42 39
                continue;
43
            }
44 39
            $parts = explode(':', $header, 2);
45 39
            $key = trim($parts[0]);
46 39
            $value = isset($parts[1]) ? trim($parts[1]) : '';
47
48 39
            if (! isset($headers[$key])) {
49 39
                $headers[$key] = [];
50
            }
51 39
            $headers[$key][] = $value;
52
        }
53
54
        return [
55 39
            (int) ($startLine[1] ?? 0),
56 39
            $headers,
57 39
            $body,
58
        ];
59
    }
60
61
    /**
62
     * @param ResponseInterface $response
63
     * @param string|null $key
64
     * @return string|bool|int|float|array|null
65
     */
66 8
    public static function responseContentAsJson(ResponseInterface $response, string $key = null)
67
    {
68 8
        $body = $response->getBody();
69
70 8
        if ($body instanceof JsonStream) {
71 4
            if ($key === null) {
72 1
                return $body->toJson();
73
            }
74 3
            $value = $body->toArray()[$key] ?? null;
75
76 3
            if (null === $value
77 2
                || is_bool($value)
78 3
                || is_int($value)
79
            ) {
80 2
                return $value;
81
            }
82 1
            return Json::encode($value);
83
        }
84 4
        if ($key === null) {
85 1
            return $body->getContents();
86
        }
87 3
        $value = Json::decode($body->getContents())[$key] ?? null;
88
89 3
        if ($value !== null && ! \is_scalar($value)) {
90 1
            $value = Json::encode($value);
91
        }
92
93 3
        return $value;
94
    }
95
96
    /**
97
     * @param ResponseInterface $response
98
     * @param string|null $key
99
     * @return string|bool|int|float|array|null
100
     */
101 11
    public static function responseContentAsArray(ResponseInterface $response, string $key = null)
102
    {
103 11
        $body = $response->getBody();
104
105 11
        if ($body instanceof JsonStream) {
106 6
            if ($key === null) {
107 2
                return $body->toArray();
108
            }
109 4
            return $body->toArray()[$key] ?? null;
110
        }
111 5
        if ($key === null) {
112 1
            return Json::decode($body->getContents());
113
        }
114
115 4
        return Json::decode($body->getContents())[$key] ?? null;
116
    }
117
}
118