Passed
Push — master ( 7ba92a...cc5ebf )
by Petr
08:15
created

StreamAdapter   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 98.46%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 146
ccs 64
cts 65
cp 0.9846
rs 10
c 1
b 0
f 0
wmc 27

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A isReadable() 0 3 1
A getLocalStream() 0 6 2
A getContents() 0 5 1
A isSeekable() 0 3 1
A seek() 0 4 1
A getSize() 0 4 2
A detach() 0 6 1
A tell() 0 3 1
A toStream() 0 10 2
A write() 0 3 1
A getMetadata() 0 11 4
A isWritable() 0 3 1
A close() 0 6 2
A eof() 0 3 1
A read() 0 5 1
A rewind() 0 3 1
A __toString() 0 6 2
1
<?php
2
3
namespace kalanis\RemoteRequestPsr\Adapters;
4
5
6
use kalanis\RemoteRequest\RequestException;
7
use kalanis\RemoteRequestPsr\Http\Answer;
8
use Psr\Http\Message\StreamInterface;
9
use RuntimeException;
10
11
12
/**
13
 * Class StreamAdapter
14
 * @package kalanis\RemoteRequestPsr\Adapters
15
 * Adapter of body of HTTP response
16
 * Returns workable stream under interface
17
 */
18
class StreamAdapter implements StreamInterface
19
{
20
    /** @var resource|null */
21
    protected $localStream = null;
22
    /** @var int */
23
    protected $offset = 0;
24
25
    /**
26
     * @param Answer $answer
27
     * @throws RequestException
28
     */
29 13
    public function __construct(Answer $answer)
30
    {
31 13
        $content = $answer->getBody();
32 13
        $this->localStream = is_resource($content) ? $content : $this->toStream($content);
33 13
    }
34
35
    /**
36
     * @param string|int|float|bool|null $content
37
     * @throws RequestException
38
     * @return resource
39
     */
40 1
    protected function toStream($content)
41
    {
42 1
        $stream = fopen('php://temp', 'rb+');
43 1
        if (false === $stream) {
44
            // @codeCoverageIgnoreStart
45
            throw new RequestException('Problem with php internals');
46
        }
47
        // @codeCoverageIgnoreEnd
48 1
        fwrite($stream, strval($content));
49 1
        return $stream;
50
    }
51
52 3
    public function __toString(): string
53
    {
54
        try {
55 3
            return strval(stream_get_contents($this->getLocalStream(), -1, 0));
56 1
        } catch (RuntimeException $ex) {
57 1
            return '';
58
        }
59
    }
60
61 4
    public function close(): void
62
    {
63
        try {
64 4
            fclose($this->getLocalStream());
65 4
            $this->localStream = null;
66 1
        } catch (RuntimeException $ex) {
67
            // nothing to do
68
        }
69 4
    }
70
71 1
    public function detach()
72
    {
73 1
        $stream = $this->localStream;
74 1
        $this->localStream = null;
75 1
        $this->offset = 0;
76 1
        return $stream;
77
    }
78
79 1
    public function getSize(): ?int
80
    {
81 1
        $data = fstat($this->getLocalStream());
82 1
        return (false !== $data) ? $data['size'] : null;
83
    }
84
85 1
    public function tell(): int
86
    {
87 1
        return intval(ftell($this->getLocalStream()));
88
    }
89
90 1
    public function eof(): bool
91
    {
92 1
        return feof($this->getLocalStream());
93
    }
94
95 1
    public function isSeekable(): bool
96
    {
97 1
        return !empty($this->localStream);
98
    }
99
100 1
    public function seek(int $offset, int $whence = SEEK_SET): void
101
    {
102 1
        fseek($this->getLocalStream(), $offset, $whence);
103 1
        $this->offset = $this->tell();
104 1
    }
105
106 1
    public function rewind(): void
107
    {
108 1
        $this->seek(0);
109 1
    }
110
111 1
    public function isWritable(): bool
112
    {
113 1
        return false;
114
    }
115
116 1
    public function write(string $string): int
117
    {
118 1
        throw new RuntimeException('Cannot write into output stream.');
119
    }
120
121 1
    public function isReadable(): bool
122
    {
123 1
        return !empty($this->localStream);
124
    }
125
126 2
    public function read(int $length): string
127
    {
128 2
        $data = strval(stream_get_contents($this->getLocalStream(), $length, $this->offset));
129 2
        $this->offset += strlen($data);
130 2
        return $data;
131
    }
132
133 3
    public function getContents(): string
134
    {
135 3
        $data = strval(stream_get_contents($this->getLocalStream(), -1, $this->offset));
136 3
        $this->offset += strlen($data);
137 3
        return $data;
138
    }
139
140 1
    public function getMetadata(?string $key = null)
141
    {
142
        try {
143 1
            $data = stream_get_meta_data($this->getLocalStream());
144 1
            if (!is_null($key)) {
145 1
                return (isset($data[$key])) ? $data[$key] : null;
146
            } else {
147 1
                return $data;
148
            }
149 1
        } catch (RuntimeException $ex) {
150 1
            return null;
151
        }
152
    }
153
154
    /**
155
     * @throws RuntimeException
156
     * @return resource
157
     */
158 9
    protected function getLocalStream()
159
    {
160 9
        if (empty($this->localStream)) {
161 3
            throw new RuntimeException('No stream available!');
162
        }
163 9
        return $this->localStream;
164
    }
165
}
166