Completed
Pull Request — master (#7)
by Sandro
03:08
created

JsonStream::detach()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
namespace ArangoDb\Http;
11
12
use ArangoDb\Util\Json;
13
use Psr\Http\Message\StreamInterface;
14
15
final class JsonStream implements StreamInterface
16
{
17
    /**
18
     * Original data
19
     *
20
     * @var string|array
21
     */
22
    private $data;
23
24
    /**
25
     * Buffered json data if needed
26
     *
27
     * @var string|null
28
     */
29
    private $buffer;
30
31
    /**
32
     * @var int
33
     */
34
    private $size;
35
36
    /**
37
     * @param string|array $data
38
     */
39 42
    public function __construct($data)
40
    {
41 42
        $this->data = $data;
42 42
    }
43
44 25
    public function toJson(): string
45
    {
46 25
        if (! is_string($this->data)) {
47 1
            return Json::encode($this->data);
48
        }
49 24
        return $this->data;
50
    }
51
52 16
    public function toArray(): array
53
    {
54 16
        if (is_string($this->data)) {
55 7
            return Json::decode($this->data);
56
        }
57 9
        return $this->data;
58
    }
59
60 2
    public function __toString()
61
    {
62 2
        return $this->getContents();
63
    }
64
65
    public function close(): void
66
    {
67
        $this->data = '';
68
        $this->buffer = null;
69
    }
70
71
    public function detach(): void
72
    {
73
        $this->close();
74
    }
75
76 2
    public function getSize(): int
77
    {
78 2
        if ($this->size === null) {
79 2
            $this->size = strlen($this->getContents());
80
        }
81
82 2
        return $this->size;
83
    }
84
85
    public function tell(): int
86
    {
87
        throw new \RuntimeException('Cannot determine the position of a JsonStream');
88
    }
89
90
    public function eof(): bool
91
    {
92
        if ($this->buffer === null) {
93
            return false;
94
        }
95
        return $this->buffer === '';
96
    }
97
98
    public function isSeekable(): bool
99
    {
100
        return false;
101
    }
102
103
    /**
104
     * @param int $offset
105
     * @param int $whence
106
     */
107
    public function seek($offset, $whence = SEEK_SET): void
108
    {
109
        throw new \RuntimeException('Cannot seek a JsonStream');
110
    }
111
112 2
    public function rewind(): void
113
    {
114 2
        $this->buffer = null;
115 2
    }
116
117
    public function isWritable(): bool
118
    {
119
        return false;
120
    }
121
122
    /**
123
     * @param string $string
124
     */
125
    public function write($string): void
126
    {
127
        throw new \RuntimeException('Cannot write a JsonStream');
128
    }
129
130
    public function isReadable(): bool
131
    {
132
        return true;
133
    }
134
135
    /**
136
     * @param int $length
137
     * @return string
138
     */
139 2
    public function read($length): string
140
    {
141 2
        if ($this->buffer === null) {
142 2
            $this->buffer = $this->getContents();
143
        }
144 2
        $currentLength = strlen($this->buffer);
145
146 2
        if ($length >= $currentLength) {
147
            // No need to slice the data because we don't have enough data.
148 2
            $result = $this->buffer;
149 2
            $this->buffer = '';
150
        } else {
151
            // Slice up the result to provide a subset of the data.
152 2
            $result = substr($this->buffer, 0, $length);
153 2
            $this->buffer = substr($this->buffer, $length);
154
        }
155
156 2
        return $result;
157
    }
158
159 33
    public function getContents(): string
160
    {
161 33
        if (is_string($this->data)) {
162 15
            return $this->data;
163
        }
164
165 31
        if (! is_string($this->data)) {
0 ignored issues
show
introduced by
The condition is_string($this->data) is always false.
Loading history...
166 31
            $this->data = Json::encode($this->data);
167
        }
168 31
        return $this->data;
169
    }
170
171
    /**
172
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
173
     * @return array|mixed|null
174
     */
175
    public function getMetadata($key = null)
176
    {
177
        $metadata = [
178
            'wrapper_data' => ['string'],
179
            'wrapper_type' => 'string',
180
            'stream_type' => 'string',
181
            'mode' => 'r',
182
            'unread_bytes' => $this->getSize() - strlen($this->buffer ?? ''),
183
            'seekable' => $this->isSeekable(),
184
            'timeout' => false,
185
            'blocked' => false,
186
            'eof' => $this->eof()
187
        ];
188
189
        if ($key === null) {
0 ignored issues
show
introduced by
The condition $key === null is always true.
Loading history...
190
            return $metadata;
191
        }
192
        return $metadata[$key] ?? null;
193
    }
194
}
195