StreamWrapper::stream_write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Mpyw\StreamInterfaceResource\StreamWrapper;
4
5
use Mpyw\StreamInterfaceResource\Registry\StreamRegistrar;
6
use Mpyw\StreamInterfaceResource\Registry\StreamRegistrarInterface;
7
use Mpyw\StreamInterfaceResource\Unserializable;
8
use Psr\Http\Message\StreamInterface;
9
10
class StreamWrapper implements StreamWrapperInterface
11
{
12
    use Unserializable;
13
14
    /**
15
     * @var StreamInterface
16
     */
17
    protected $stream;
18
19
    /**
20
     * @var null|StreamRegistrar
21
     */
22
    public static $defaultRegistrar = null;
23
24
    /**
25
     * @var StreamRegistrarInterface
26
     */
27
    protected $registrar;
28
29 15
    public function __construct(?StreamRegistrarInterface $registrar = null)
30
    {
31 15
        $this->registrar = $registrar ?? static::$defaultRegistrar ?? StreamRegistrar::getInstance();
32
    }
33
34 15
    public function __destruct()
35
    {
36 15
        $this->registrar->remove($this->stream);
37
    }
38
39 15
    public function stream_close(): void
40
    {
41 15
        $this->stream->close();
42
    }
43
44 8
    public function stream_eof(): bool
45
    {
46 8
        return $this->stream->eof();
47
    }
48
49 15
    public function stream_open(string $path, string $mode, int $options, &$opened_path): bool
50
    {
51 15
        $this->stream = $this->registrar->streamFor($path);
52 15
        return true;
53
    }
54
55 8
    public function stream_read(int $count): string
56
    {
57 8
        return $this->stream->read($count);
58
    }
59
60 1
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
61
    {
62 1
        $this->stream->seek($offset, $whence);
63 1
        return true;
64
    }
65
66 5
    public function stream_stat(): array
67
    {
68
        return [
69 5
            'size' => $this->stream->getSize(),
70
        ];
71
    }
72
73 1
    public function stream_tell(): int
74
    {
75 1
        return $this->stream->tell();
76
    }
77
78 2
    public function stream_write(string $data): int
79
    {
80 2
        return $this->stream->write($data);
81
    }
82
}
83