Completed
Pull Request — master (#64)
by Frederik
03:38 queued 01:09
created

ResourceStream::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\StreamInterface;
7
8
final class ResourceStream implements StreamInterface
9
{
10
    /**
11
     * @var resource
12
     */
13
    private $resource;
14
15
    /**
16
     * @param resource $resource
17
     */
18 83
    public function __construct($resource)
19
    {
20 83
        if (!\is_resource($resource)) {
21
            throw new \InvalidArgumentException('Argument 0 must be a resource');
22
        }
23
24 83
        \rewind($resource);
25 83
        $this->resource = $resource;
26 83
    }
27
28
    /**
29
     * @return string
30
     */
31 32
    public function __toString(): string
32
    {
33 32
        \rewind($this->resource);
34 32
        return (string)\stream_get_contents($this->resource);
35
    }
36
    
37
    public function close(): void
38
    {
39
        \fclose($this->resource);
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45 77
    public function detach()
46
    {
47 77
        return $this->resource;
48
    }
49
50
    /**
51
     * @return int|null
52
     */
53 7
    public function getSize(): ?int
54
    {
55 7
        $stat = \fstat($this->resource);
56 7
        if ($stat === false) {
57
            throw new \UnexpectedValueException('Cannot get stat from resource');
58
        }
59
60 7
        return $stat['size'];
61
    }
62
63
    /**
64
     * @return int
65
     * @throws \RuntimeException
66
     */
67 3
    public function tell(): int
68
    {
69 3
        $tell = \ftell($this->resource);
70 3
        if ($tell === false) {
71
            throw new \UnexpectedValueException('Cannot get tell from resource');
72
        }
73
74 3
        return $tell;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 7
    public function eof(): bool
81
    {
82 7
        return \feof($this->resource);
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isSeekable(): bool
89
    {
90
        $metaData = \stream_get_meta_data($this->resource);
91
        if (!isset($metaData['seekable'])) {
92
            return false;
93
        }
94
95
        return $metaData['seekable'];
96
    }
97
98
    /**
99
     * @param int $offset
100
     * @param int $whence
101
     * @return int
102
     */
103 1
    public function seek(int $offset, int $whence = SEEK_SET): int
104
    {
105 1
        return \fseek($this->resource, $offset, $whence);
106
    }
107
108
    /**
109
     * @return bool
110
     */
111 33
    public function rewind(): bool
112
    {
113 33
        return \rewind($this->resource);
114
    }
115
116
    /**
117
     * @return bool
118
     */
119 1
    public function isWritable(): bool
120
    {
121 1
        $metaData = \stream_get_meta_data($this->resource);
122 1
        if (!isset($metaData['uri'])) {
123
            return false;
124
        }
125
126 1
        return \is_writable($metaData['uri']) || $metaData['uri'] === 'php://memory';
127
    }
128
129
    /**
130
     * @param string $string
131
     * @return int
132
     */
133 1
    public function write($string): int
134
    {
135 1
        $written = \fwrite($this->resource, $string);
136 1
        if ($written === false) {
137
            throw new \UnexpectedValueException('Cannot write data to resource');
138
        }
139
140 1
        return $written;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146
    public function isReadable(): bool
147
    {
148
        return true;
149
    }
150
151
    /**
152
     * @param int $length
153
     * @return string
154
     */
155 11
    public function read(int $length): string
156
    {
157 11
        $bytes = \fread($this->resource, $length);
158 11
        if ($bytes === false) {
159
            throw new \UnexpectedValueException('Cannot read from resource');
160
        }
161
162 11
        return $bytes;
163
    }
164
165
    /**
166
     * @return string
167
     */
168 8
    public function getContents(): string
169
    {
170 8
        $contents = \stream_get_contents($this->resource);
171 8
        if ($contents === false) {
172
            throw new \UnexpectedValueException('Cannot read contents from resource');
173
        }
174
175 8
        return $contents;
176
    }
177
178
    /**
179
     * @param array<int, string> $keys
180
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
181
     */
182 32
    public function getMetadata(array $keys = []): array
183
    {
184 32
        $metaData = \stream_get_meta_data($this->resource);
185
186 32
        $keys = \array_map('strtolower', $keys);
187
188 32
        return \array_filter(
189 32
            $metaData,
190
            function ($key) use ($keys) {
191 32
                return \in_array(\strtolower($key), $keys);
192 32
            },
193 32
            ARRAY_FILTER_USE_KEY
194
        );
195
    }
196
}
197